MessageBox () Howto

Here is the basic code to making a messege box in a Win32 C App.



#include <windows.h>
int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow )
{
	MessageBox( NULL, "Hello, World!", "Hi!", MB_OK );
	return 0;
}


Lets begin to analyze this. In a win32 apps main is changed to WinMain and it takes over the variables listed in the parentheses "( )". Don't worry about those variables ... they are just what windows sends to the program when windows calls it (think of your program as a giant function that a giant program (windows) calls when the user of windows does something to call it.) Anyways, in this tutorial i'm showing you what the function MessageBox ( ) can do. MessageBox is defined in windows.h like this:




int WINAPI MessageBoxA( HWND hWnd, LPCSTR lpszText, LPCSTR lpszTitle, int nButtons );
#define MessageBox MessageBoxA

Now lets define what these things you send to MessageBox are...


1) 'hWnd' which is the handle of the parent window for this messege box.


2) 'lpszText' can be any character array or string. This is what will be displayed as the body of the window.


3) 'lpszTitle' - As you might have guessed is the title of the box (also any char array or string will work.)


4) 'nButtons' is the last thing you send over. This is what tells windows what buttons to have on your window (ex. OK, Cancel...) There are pre defined things you can send over. Lets take a look at what these are:



MB_OK

MB_YESNO

MB_OKCANCEL

MB_YESNO | MB_OKCANCEL


Those are the buttons that you can click but I bet some of you are asking "how do I make that big red round X that I see when windows fucks up?" You do this by placing them in the same place as you would have put those buttons. Here are those commands...

MB_ICONERROR


MB_ICONEXCLAMATION

MB_ICONINFORMATION

MB_ICONQUESTION

"ok ... so how do you make a box that has both an icon and button or two?" I bet your asking. You do this by combining each command with a single or sign '|'.


Heres an example for you who say "huh? Say what?"



MessageBox( NULL, "MB_YESNO | MB_OKCANCEL | MB_ICONERROR", "EXAMPLES", MB_YESNO | MB_OKCANCEL | MB_ICONERROR );

That previous example will makes this:


Also Note that this function will return a value. It returns the button pressed.

Here are the values it could return-

"IDOK" - if "OK" is pressed

"IDCANCEL" - if "Cancel" is pressed

"IDYES" - if "Yes" is pressed

"IDNO" - if "No" is pressed

"IDRETRY" - if "Retry" is pressed


Write to me if you have any Q's or have something for me to add to this