Menu
Win32 Tutorials

Win32

Message Boxes



Tutorials > Win32 > Message Boxes

View Full Source

Introduction

Message boxes are seen everywhere in windows. If you have ever received a popup box notifying you of some action or error, you have witnessed a message box.

As creating a message box is not a highly complex task, it will be one of the first things we look at in Win32 as it is very useful to display information regarding the state of the program.

To create a message box, you can use the MessageBox function. This function takes 4 parameters :

HWND hwnd - An HWND is a handle to a window. In this case it is the handle to the window that will own the message box. This is useful as the owner (parent) window will deactivate and lie behind the message box. If you pass NULL for this parameter, the message box will have no parent.

LPCTSTR lpText - An LPCTSTR is a long pointer to a constant character string. The LP stands for "long pointer" and the C stands for "Constant". This is the text that will be displayed within the message box.

LPCTSTR lpCaption - This is the text that will appear in the title (bar at the top of the box) bar of the message box.

UINT uType - A UINT is an unsigned int. This defines what type of message box will appear.

Firstly you can specify what buttons you want to appear on the message box. The most common ones are shown below :

Flag Description
MB_OK 1 OK Button
MB_OKCANCEL 1 OK Button and 1 Cancel Button
MB_RETRYCANCEL 1 Retry Button and 1 Cancel Button
MB_YESNO 1 Yes Button and 1 No Button
MB_YESNOCANCEL 1 Yes, 1 No and 1 Cancel Button
MB_ABORTRETRYIGNORE 1 Abort, 1 Retry and 1 Ignore Button

You can also specify what icon must be displayed in the message box. The most common ones are shown below :

Flag Description
MB_ICONEXCLAMATION Exclamation Icon
MB_ICONWARNING Warning Icon
MB_ICONINFORMATION Icon with I on it
MB_ICONQUESTION Icon with a question mark on it
MB_ICONERROR Red error symbol

An MB_DEFBUTTONX can also be added where X is a number from 1 to 4. This will indicate which button will be the default button ie. which button will be selected when the message box first appears.

If you wish to add more than one of these flags, you can separate each one with a | (or) operator.

The return value indicates which button has been pressed. The most commonly returned values are given below :

Return Value
IDABORT
IDCANCEL
IDIGNORE
IDNO
IDOK
IDRETRY
IDYES

Contents of main.cpp :


#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		LPSTR lpCmdLine, int nShowCmd)
{

We first create a simple message box with an OK button saying "Hello World".

	MessageBox(NULL, "Hello World", 
		"Title", MB_OK | MB_ICONINFORMATION);

The second message box has a YES and a NO button. If you click YES, another message box will appear.

	if (MessageBox(NULL, "Do you?", "Do You?",
		MB_YESNO | MB_ICONQUESTION) == IDYES)
		MessageBox(NULL, "Yes", "Yes", MB_OK);
	
	return 0;
}

You should now be able to all sorts of different kinds of message boxes. Play around with the message box flags to see what each looks like and how it reacts.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++

< Tutorial 03 - WinMain Function Tutorial 05 - Creating Windows >

Back to Top


All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer

Links