Thread: Message box / Structure question?

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    Message box / Structure question?

    First of all, it's late, so no laughing. I'm tired, so I tried to do something for fun. In Win32, you have several "defines" that you often have to memorize to use, or look at a reference constantly to see what options you have available. For instance, a message box's button. I should type down MB.MB_OK instead of the normal MB_OK. I tried this in a struct, but I got no luck with it. Obviously, I can't put defines in a struct, so I tried using its default data type and hexidecimal value. If this is somehow fixed, it'd be a great start to a helper library later. As I said, I'm tired. It's just an expimentation since I'm a C# vetinarian, and I got use to typing dots which really did help sort things out. Anyway, thanks for smirking.


    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include <windows.h>
    #include <windowsx.h>
    
    struct MB_Buttons
    {
    	UINT MB_OK = 0x00000000L;
    	UINT MB_OKCANCEL = 0x00000001L;
    	UINT MB_ABORTRETRYIGNORE = 0x00000002L;
    	UINT MB_YESNOCANCEL = 0x000000003;
    	UINT MB_YESNO = 0x000000004;
    	UINT MB_RETRYCANCEL = 0x000000005;
    } MB;
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int ncmdshow)
    { 
    	MessageBox(NULL, "Hello World!", "Win32 API Practice", MB.MB_OK | MB_ICONEXCLAMATION);
    	
    	MessageBeep(MB_ICONASTERISK);
    	return 0;
    }
    Last edited by dxfoo; 10-23-2005 at 02:36 AM. Reason: Fixing code format.

  2. #2
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Cool, I did it! It may be a waste in memory, but it couldn't hurt for clarity on the programmer's side. It's often nice to see my available options like an OO language would display, and we know that Win32 is utterly a mess that needs some cleaning! Although this is a short example, you can build onto it with ease. I'll probably make a complete header later that will make programming easier and more abstract. Anyway, enjoy the code, especially you C# die-hards. Just be sure to put this in a header so you don't clutter up the main source file.

    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include <windows.h>
    #include <windowsx.h>
    
    /* Message Box Buttons */
    struct Button
    {
    public:
    
    	UINT OK;
    	UINT OKCANCEL;
    	UINT ABORTRETRYIGNORE;
    	UINT YESNOCANCEL;
    	UINT YESNO;
    	UINT RETRYCANCEL;
    	UINT ABORTTRYAGAINCONTINUE;
    
    	Button()
    	{
    		MB.OK = 0;
    		MB.OKCANCEL = 1;
    		MB.ABORTRETRYIGNORE = 2;
    		MB.YESNOCANCEL = 3;
    		MB.YESNO = 4;
    		MB.RETRYCANCEL = 5;
    		MB.ABORTTRYAGAINCONTINUE = 6;
    	}
    } MB;
    
    /* Message Box Icons */
    struct Icon
    {
    public:
    
    	UINT ICONHAND;
    	UINT ICONQUESTION;
    	UINT ICONEXCLAMATION;
    	UINT ICONASTERISK;
    	UINT ICONWARNING;
    	UINT ICONERROR;
    	UINT ICONINFORMATION;
    	UINT DEFBUTTON1;
    	UINT DEFBUTTON2;
    	UINT DEFBUTTON3;
    
    	Icon()
    	{
    		ICON.ICONHAND = 0x00000010L;
    		ICON.ICONQUESTION = 0x00000020L;
    		ICON.ICONEXCLAMATION = 0x00000030L;
    		ICON.ICONASTERISK = 0x00000040L; 
    		ICON.ICONWARNING = ICONEXCLAMATION;
    		ICON.ICONERROR = ICONHAND;
    		ICON.ICONINFORMATION = ICONASTERISK;
    		ICON.DEFBUTTON1 = 0x00000000L;
    		ICON.DEFBUTTON2 = 0x00000100L;
    		ICON.DEFBUTTON3 = 0x00000200L; 
    	}
    } ICON;
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    { 	
    	MessageBox(NULL, "Hello Win32!", "Win32 API Practice", MB.OK | ICON.ICONEXCLAMATION);
    
    	return 0;
    }
    Last edited by dxfoo; 10-23-2005 at 12:09 PM.

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    you should probably use an enum instead, it's a pretty bad idea to use a struct for enum values.

  4. #4
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    True, however, you'd get these errrors...

    error C2653: 'MB' : is not a class or namespace name
    error C2228: left of '.ICONEXCLAMATION' must have class/struct/union type

    I know in C#, you could call it by MB.OK, but in C++, it's just OK, which kills the point of doing this.

    For example, in structures:
    MessageBox(NULL, "Hello Win32!", "Win32 API Practice", MB.OK | ICON.ICONEXCLAMATION);

    In enum: (kills the point)
    MessageBox(NULL, "Hello Win32!", "Win32 API Practice", OK | ICONEXCLAMATION);

    I know that in enums, you can create a variable instance of that enum. I still haven't seen how to use it though. For example, with MB its instance, calling MB.OK will have an error. If there's something I'm missing, I'd love to learn how

    Maybe like this? Seems a bit overkill, though.
    http://cpptips.hyperformix.com/cpptips/enum_as_class
    Last edited by dxfoo; 10-23-2005 at 12:43 PM.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Do you have pop-up code completion for struct/class types with your compiler? If so, then it can be pretty convenient here, however really all your needed defined values are just a glance in the documentation.

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    yea and don't forget about Ctrl+Space, just type MB_ then hit Ctrl+Space and it will popup the intellisense list of the MB_ constants.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. message box LPCWSTR manipulation and keyword question
    By stanlvw in forum Windows Programming
    Replies: 11
    Last Post: 05-27-2008, 12:36 PM
  2. Message loop question.
    By antex in forum Windows Programming
    Replies: 6
    Last Post: 04-03-2007, 10:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  5. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM