CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2014
    Location
    Austin, TX
    Posts
    9

    Declaring member variables in a windows form app

    Hi All,

    You'll have to excuse me because I'm coming from a more traditional C/C++ background (like a WIN32 project with a standard WndProc), and I've just started learning about Windows Forms. I have created a windows form project in visual studio so that I can use a windows form to interact with the game class that I'm creating, but I'm running into some problems.

    For one thing, I would like to be able to call Image::FromFile() one time only during initialization, and store the result in a member variable (called mBGImage). From what I can tell, the variable needs to be of type String^ (this caret symbol is new to me, but I understand it is the "managed code" version of the standard pointer, which would look like String*). When I try to compile the following code (located in my header file) I get the error "error C3265: cannot declare a managed 'mBGImage' in an unmanaged 'BSG::BSGame'".

    What am I doing wrong and how can I store the result of Image::FromFile() permanently in my class?

    Oh, and also, when I try to declare a global variable of type "Image^", I get "error C3145: global or static variable may not have managed type System:rawing::Image ^"

    ----------------------------

    #include "stdafx.h"

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System:ata;
    using namespace System:rawing;

    namespace BSG
    {
    const Point kGridSize(400, 400); //size of grid (width, height)
    const Point kGridTiles(10, 10); //total number of tiles (x,y)

    public class BSGame
    {
    public:
    BSGame(void); //constructor
    ~BSGame(void); //destructor

    void Init(void); //Initialize the game
    void DrawScreen(Graphics^ gfxObj, Rectangle rect);

    private:
    bool mInited; //is this object initialized?
    Image^ mBGImage;
    };
    }

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Declaring member variables in a windows form app

    Quote Originally Posted by spaceb View Post
    For one thing, I would like to be able to call Image::FromFile() one time only during initialization, and store the result in a member variable (called mBGImage). From what I can tell, the variable needs to be of type String^ [...].
    No. Of course the variable would need to be of type Image ^ (or of one of the two derived types Bitmap ^ or Metafile ^, if you're sure the image you're loading is of the respective type and want to use features specific to the derived classes - in which case you need to cast the return value of Image::FromFile() to the appropriate type), which you got right in the code snippet you posted.

    When I try to compile the following code (located in my header file) I get the error "error C3265: cannot declare a managed 'mBGImage' in an unmanaged 'BSG::BSGame'".
    That's because of just what the error message says: The first line of your class needs to be

    Code:
    public ref class BSGame
    in order to define a managed reference class.

    What am I doing wrong and how can I store the result of Image::FromFile() permanently in my class?

    Oh, and also, when I try to declare a global variable of type "Image^", I get "error C3145: global or static variable may not have managed type System::Drawing::Image ^"
    Well, this error message is rather clear as well: You can't have global/static variables of managed types. That said, the message is a bit misleading in that it's concrete meaning is you can't have a global or static global variables. Static class member variables are very well possible and would have the same storage duration as a global static variable, i.e. application instance lifetime. Design-wise I'd recommend you, however, to rethink whether you really need that or could as well go ahead with an ordinary class member variable.

    Please use code tags when positing code.
    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.

  3. #3
    Join Date
    Sep 2014
    Location
    Austin, TX
    Posts
    9

    Re: Declaring member variables in a windows form app

    Thanks for the information - like I said, I'm brand new to this managed code stuff. Sorry about saying String^, I meant Image^. I will try adding the ref keyword and see what happens.

    I was also wondering if I could use the regular Win32 API's LoadImage() function in my windows form app. When I try to include the standard gdi headers I get a ton of errors (for instance, it doesn't recognize the type UINT that's used in the gdi headers). Is this because I'm using the Windows Form project type and not a regular WndProc() based C++ app?

    Thanks Again!

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

    Re: Declaring member variables in a windows form app

    Quote Originally Posted by spaceb View Post
    I was also wondering if I could use the regular Win32 API's LoadImage() function in my windows form app. When I try to include the standard gdi headers I get a ton of errors (for instance, it doesn't recognize the type UINT that's used in the gdi headers). Is this because I'm using the Windows Form project type and not a regular WndProc() based C++ app?
    You actually could use the Win32 LoadImage() in you managed app, but why would you want to? It would dramatically complicate things without gaining you any reward, compared to simply using what the .NET framework readily offers you.

    There are, however, situations where combining managed and native code actually can be of advantage. Quite a few of such interop scenarios have been discussed around here over time, some including downloadable demo projects to play with. Here are two examples:

    http://forums.codeguru.com/showthrea...pture-a-window
    http://forums.codeguru.com/showthrea...-a-hack-though

    BTW, something I forgot to mention in my last post: Another thing you can't do in a managed class is declaring member functions and variables const. At least for the member variabes you have the keyword initonly as an alternative, which means mostly the same.
    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
    Sep 2014
    Location
    Austin, TX
    Posts
    9

    Re: Declaring member variables in a windows form app

    Quote Originally Posted by Eri523 View Post
    You actually could use the Win32 LoadImage() in you managed app, but why would you want to? It would dramatically complicate things without gaining you any reward, compared to simply using what the .NET framework readily offers you.
    Well, ideally I would like this class to eventually be cross-platform. If the "ref" keyword is something specific to the .NET framework or C++/CLI, then getting the code to compile on a non-Windows OS (like the Mac) is a problem. Of course, I will need to hide other Windows-specific types, like the Graphics or Rect objects, but I can do this using #ifdef statements. However, I would like the "skeleton" of the class to remain intact and be compilable across all platforms.

    When I've written cross-platform apps or classes in the past, I have the Mac and Windows versions share the same header file, which uses #ifdef statements to hide anything that's OS-specific. Then I use separate .cpp files for the actual functions, so #ifdef statements aren't necessary. That is my eventual goal here.

    Of course, right now this is a Windows-only app, so I'm not worried about it at the moment. I just want to pick your brain for information if I can.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Declaring member variables in a windows form app

    If your goal ist platform independence, you'd typically either use a cross-platform framework like Qt or KDE, or you'd write your own abstraction/adapter layers that isolate your platform-unaware business logic code from the concrete platform. The latter, essentially, would mean you'd be writing your own application-specific cross-platform library.

    At any rate, using Win32 API functions wouldn't be a good idea in this scenario, unless you're going to write a Win32 emulation layer for any Non-Windows platform you want your program to run on. This is possible, but it would be tending against the recommended design goal of keeping the abstraction layer as lean as possible. This approach would also bear the risk of your code unintentionally relying on some details of Win32 API behaviour that you'll simply forget to emulate because you're just not aware that your code is relying on them, leading to hard-to-find bugs in the Non-Windows versions of your code.

    In principle, using .NET framework library APIs here would even be a tiny bit better than using Win32, since .NET (more precisely: the Common Language Runtime, CLR, which is part of .NET) isn't even strictly MS-only. Still .NET is far from being a cross-platform framework and I wouldn't recommend that approach.

    I strongly doubt that, in practice, it's possible to write maintainable C++ code that compiles as both managed and unmanaged just by using some preprocessor magic. So the approach I'd recommend is to write the business logic of your program that you want to be platform-independent in native (i.e. unmanaged) C++ and wrap that into a platform-aware GUI (plus a few more abstraction modules) that may or may not be .NET-based.
    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.

  7. #7
    Join Date
    Sep 2014
    Location
    Austin, TX
    Posts
    9

    Re: Declaring member variables in a windows form app

    Quote Originally Posted by Eri523 View Post
    If your goal ist platform independence, you'd typically either use a cross-platform framework like Qt or KDE, or you'd write your own abstraction/adapter layers that isolate your platform-unaware business logic code from the concrete platform. The latter, essentially, would mean you'd be writing your own application-specific cross-platform library.
    Exactly, and the latter is the approach that I want to use. I guess my ultimate question was whether a Windows Forms app can be written with completely unmanaged code (as I'm new to these c++/cli types), and it sounds like the answer is no. I definitely like the fact that windows forms allow you to visually create a window and generate events easily.

    Either way, I appreciate the help and I think you've given me enough info to be able to write a windows form app that will work with my Game class.

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Declaring member variables in a windows form app

    You're right, writing a Windows Forms app without involving .NET is impossible, simply because Windows Forms is part of the .NET Framework Library.

    Howerver, there's a few .NET-less alternative approaches to visual GUI design under Windows. The MS flavor of native visual GUI design is the MFC library plus the associated development tools, available in the Visual Studio Professional edition and up. The cross-platform frameworks I mentioned should offer something similar as well; at least as far as Qt is concerned, I'm rather sure ut does.
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured