-
December 31st, 2011, 06:46 AM
#1
Define the Images in Headerfile ?
Iam very new to Visual C++2010. Its my first project converting from C# to VC++2010.
Actually I have a headerfile for my strings and variables to use with my whole project. But I don't know, how to define or declare the similar for images...Iam facing the problem like the below...
For Strings....Below is good
Code:
#define MY_STR "My Value"
public:
System::Drawing::Image^ MyMainImge=System::Drawing::Image::FromFile("C:\\MyPrgCodes\\Images\\MyMenuImage.jpg");
error C3145: 'MyMainImge' : global or static variable may not have managed type
Any Ideas for me? Thanks
Last edited by ovidiucucu; December 31st, 2011 at 07:02 AM.
Reason: [CODE] tags added
-
December 31st, 2011, 07:00 AM
#2
Re: Define the Images in Headerfile ?
-
December 31st, 2011, 09:45 AM
#3
Re: Define the Images in Headerfile ?
The error message more or less says it all: You can't have global variables of managed types. However, "more or less" because OTOH they just can't be static and global at the same time; they very well may be static class members. In that respect the message is somewhat misleading.
So, the solution is to wrap data instances like that up in a (static-members-only) class:
Code:
public ref class GlobalData
{
public:
static System::String ^strMyString = "My Value";
static System::Drawing::Image ^imgMyMainImage = System::Drawing::Image::FromFile("C:\\MyPrgCodes\\Images\\MyMenuImage.jpg");
};
Note that I added your string to that class as well. As you experienced, making it a #define macro works, but usually this kind of macro should be avoided if possible.
You can then access the two data items as GlobalData::strMyString and GlobalData::imgMyMainImage.
Last edited by Eri523; January 2nd, 2012 at 12:39 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
January 1st, 2012, 06:12 PM
#4
Re: Define the Images in Headerfile ?
An additional hint: Perhaps it's not quite a good idea to load the external image like that. Initialization of statics occurs at a very early stage in program start-up, and in the not so unlikely case that Image::FromFile() throws an exception (for instance when the image file is not found), your app will die silently, merely posting a record about the exception to the Windows event log (in the Application category). The user will then not even notice that your app actually ever did start, and most ordinary users probably won't ever take a look at the event log at all.
A better alternative is to initiate the initialization of GlobalData members that may throw explicitly, so it's under your control when and where you do that. The class may be modified as follows to achieve this goal:
Code:
public ref class GlobalData
{
public:
static void SetUpImages()
{
imgMyMainImage = System::Drawing::Image::FromFile("C:\\MyPrgCodes\\Images\\MyMenuImage.jpg");
}
public:
static System::String ^strMyString = "My Value";
static System::Drawing::Image ^imgMyMainImage;
};
You then would explicitly call GlobalData::SetUpImages() to initialize the image member(s) of the global data class. If you call that function in your main form's Load event handler (or later), an exception will pop up the standard .NET exception dialog. That's not remarkably user-friendly either, but at least the user will know something went wrong that way.
Better yet is to catch potential exceptions being thrown during the initialization and provide your own error handling. You may then display a more user-fiendly error message and terminate your program, or you may take measures that allow your program to continue running despite the error that occurred. That way you may call the initialization function at an earlier stage like in the main form class' constructor. But be warned not to call Close() from inside the c'tor in order to terminate start-up of your program. The earliest stage at which you can safely do that is in the Load event handler.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
January 2nd, 2012, 01:58 AM
#5
Re: Define the Images in Headerfile ?
Mr. Eric,
Its my first project, also learning & converting the project from c# to Visual C++.
Really I search a answer like yours....
My sincere thanks to your kindness.
Thanks
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|