Friday, November 4, 2011

A Real-World Window in C programming

Suppose we wish to create a window and draw a few shapes in it. For creating such a window there is no standard window class available. Hence we would have to create our own window class, register it with Windows OS and then create a window on the basis of it. Instead of straightway jumping to a program that draws
shapes in a window let us first write a program that creates a window using our window class and lets us interact with it. Here is the program…
#include <windows.h>
#include "helper.h"
void OnDestroy ( HWND ) ;
int __stdcall WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                     LPSTR lpszCmdline, int nCmdShow )
{
MSG m ;
/* perform application initialization */
InitInstance ( hInstance, nCmdShow, "title" ) ;
/* message loop */
while ( GetMessage ( &m, 0, 0, 0 ) )
DispatchMessage ( &m ) ;
return 0 ;
}
LRESULT CALLBACK WndProc ( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
switch ( message )
{
case WM_DESTROY :
OnDestroy ( hWnd ) ;
break ;
default :
return DefWindowProc ( hWnd, message, wParam, lParam ) ;
}
return 0 ;
}
void OnDestroy ( HWND hWnd )
{
PostQuitMessage ( 0 ) ;
}

On execution of this program the window shown in Figure 17.4 appears on the screen. We can use minimize and the maximize button it its title bar to minimize and maximize the window. We can stretch its size by dragging its boundaries. Finally, we can close the window by clicking on the close window button in the title bar.






Let us now try to understand this program step by step

No comments:

Post a Comment