Before we proceed with the actual creation of a window it would be a good idea to identify the various elements of it. These are
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>
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,
{
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 ;
}
ShowWindow ( h, nCmdShow ) ;
MessageBox ( 0, “Hi!”, “Waiting”, MB_OK ) ;
return 0 ;
}
Here is the output of the program…
Let us now understand the program. Every window enjoys certain properties—background color, shape of cursor, shape of icon, etc. All these properties taken together are known as ‘window class’. The meaning of ‘class’ here is ‘type’. Windows insists that a window class should be registered with it before we attempt to create windows of that type. Once a window class is registered we can create several windows of that type. Each of these windows would enjoy the same properties that have been registered through the window class. There are several predefined window classes. Some of these are BUTTON, EDIT, LISTBOX, etc. Our program has created one such window using the predefined BUTTON class.
To actually create a window we need to call the API function CreateWindow( ). This function requires several parameters starting with the window class. The second parameter indicates the text that is going to appear on the button surface. The third parameter specifies the window style. WS_OVERLAPPEDWINDOW is a commonly used style. The next four parameters specify the window’s initial position and size—the x and y screen coordinates of the window’s top left corner and the window’s width and height in pixels. The next three parameters specify the handles to the parent window, the menu and the application instance respectively. The last parameter is the pointer to the window-creation data.
We can easily devote a section of this book to CreateWindow( ) and its parameters. But don’t get scared of it. Nobody is supposed to remember all the parameters, their meaning and their order. You can always use MSDN (Microsoft Developer Network) help to understand the minute details of each parameter. This help is available as part of VC++ 6.0 product. It is also available on the net at http://www.msdn.microsoft.com/library.
Note that CreateWindow( ) merely creates the window in memory. We still are to display it on the screen. This can be done using the ShowWindow( ) API function. CreateWindow( ) returns handle of the created window. Our program uses this handle to refer to the window while calling ShowWindow( ). The second parameter passed to ShowWindow( ) signifies whether the window would appear minimized, maximized or normal. If the value of this parameter is SW_SHOWNORMAL we get a normal sized window, if it is SW_SHOWMINIMIZED we get a minimized window and if it is SW_SHOWMINIMIZED we get a maximized window. We have passed nCmdShow as the second parameter. This variable contains SW_SHOWNORMAL by default. Hence our program displays a normal sized window.
We can easily devote a section of this book to CreateWindow( ) and its parameters. But don’t get scared of it. Nobody is supposed to remember all the parameters, their meaning and their order. You can always use MSDN (Microsoft Developer Network) help to understand the minute details of each parameter. This help is available as part of VC++ 6.0 product. It is also available on the net at http://www.msdn.microsoft.com/library.
Note that CreateWindow( ) merely creates the window in memory. We still are to display it on the screen. This can be done using the ShowWindow( ) API function. CreateWindow( ) returns handle of the created window. Our program uses this handle to refer to the window while calling ShowWindow( ). The second parameter passed to ShowWindow( ) signifies whether the window would appear minimized, maximized or normal. If the value of this parameter is SW_SHOWNORMAL we get a normal sized window, if it is SW_SHOWMINIMIZED we get a minimized window and if it is SW_SHOWMINIMIZED we get a maximized window. We have passed nCmdShow as the second parameter. This variable contains SW_SHOWNORMAL by default. Hence our program displays a normal sized window.
The WS_OVERLAPPEDWINDOW style is a collection of the following styles:
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
As you can make out from these macros they essentially control the look and feel of the window being created. All these macros are #defined in the ‘Windows.h’ header file.
On executing this program a window and a message box appears on the screen as shown in the Figure 17.2. The window and the message box disappear as soon as we click on OK. This is because on doing so execution of WinMain( ) comes to an end and moreover we have made no provision to interact with the window.
You can try to remove the call to MessageBox( ) and see the result. You would observe that no sooner does the window appear it disappears. Thus a call to MessageBox( ) serves the similar purpose as getch( ) does in sequential programming.
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
As you can make out from these macros they essentially control the look and feel of the window being created. All these macros are #defined in the ‘Windows.h’ header file.
On executing this program a window and a message box appears on the screen as shown in the Figure 17.2. The window and the message box disappear as soon as we click on OK. This is because on doing so execution of WinMain( ) comes to an end and moreover we have made no provision to interact with the window.
You can try to remove the call to MessageBox( ) and see the result. You would observe that no sooner does the window appear it disappears. Thus a call to MessageBox( ) serves the similar purpose as getch( ) does in sequential programming.
No comments:
Post a Comment