Define the Images in Headerfile ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Define the Images in Headerfile ?

  1. #1
    Join Date
    Dec 2011
    Posts
    53

    Cool 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 06:02 AM. Reason: [CODE] tags added

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Define the Images in Headerfile ?

    [ Moved thread ]
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    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 11:39 AM.
    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.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    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.

  5. #5
    Join Date
    Dec 2011
    Posts
    53

    Thumbs up 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center