Windows Programming Using C

The Role of a Message Box

Often we are required to display certain results on the screen during the course of execution of a program. We do this to ascertain whether we are getting the results as per our expectations. In a sequential DOS based program we can easily achieve this using printf( ) statements. Under Windows screen is a shared resource. So you can imagine what chaos would it create if all running applications are permitted to write to the screen. You would not be able to make out which output is of what application. Hence no Windows program is permitted to write anything directly to the screen. That’s where a message box enters the scene. Using it we can display intermediate results during the course of execution of a program. It can be dismissed either by clicking the ‘close button’ in its title bar or by clicking the OK button present in it. There are numerous variations that you can try with the MessageBox( ). Some of these are given below

MessageBox ( 0, “Are you sure”, “Caption”, MB_YESNO ) ;
MessageBox ( 0, “Print to the Printer”, “Caption”, MB_YESNO CANCEL) ; 
MessageBox ( 0, “icon is all about style”, “Caption”, MB_OK | 

You can put the above statements within WinMain( ) and see the results for yourself. Though the above message boxes give you flexibility in displaying results, button, icons, there is a limit to which you can stretch them. What if we want to draw a free hand drawing or display an image, etc. in the message box. This would not be possible. To achieve this we need to create a full-fledged window. The next section discusses how this can be done.

Here Comes the window…

Before we proceed with the actual creation of a window it would be a good idea to identify the various elements of it.

Windows Programming Using C
Windows Programming Using C

Note that every window drawn on the screen need not necessarily have every element shown in the above figure. For example, a window may not contain the minimize box, the maximize box, the scroll bars and the menu

Let us now create a simple program that creates a window on the screen. Here is the program…

#include <windows.h> 
int _stdcall WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
 LPSTR lpszCmdLine, int nCmdShow ) 
 HWND h ; 
h = CreateWindow ( “BUTTON”, “Hit Me”, WS_OVERLAPPEDWINDOW, 
 10, 10, 150, 100, 0, 0, i, 0 ) ; 
ShowWindow ( h, nCmdShow ) ; 
MessageBox ( 0, “Hi!”, “Waiting”, MB_OK ) ; 
return 0 ; 

Here is the output of the program…

Leave a Comment