<Previous Lesson

Visual Programming

Next Lesson>

Lesson#15

Windows Management

15.1 Z-ORDER 2
15.2 WINDOWS REVIEW 2
15.2.1 CREATEWINDOW 2
15.2.2 C
HILD WINDOWS 2
15.2.3 W
INDOW PROCEDURE 3
15.2.4 N
OTIFICATION CODE 3
15.2.5 WM_COMMAND N
OTIFICATION CODE 3
15.3 EXAMPLE APPLICATION 3
15.3.1 DESCRIPTION 3
15.3.2 O
BJECTIVES 4
15.3.3 W
INDOWS MANAGEMENT FUNCTIONS 4
15.3.4 W
INDOW CLASSES 5
15.3.4.1 Main Window class 5
15.3.4.2 Popup Window class 5
15.3.4.3 System Window classes 5
15.3.5 C
REATING MAIN WINDOWS 5
15.3.6 C
REATING CHILD WINDOWS 6
15.3.7 U
SER DEFINED MESSAGES 7
15.3.8 A
PPLICATIONS MAIN WINDOW PROCEDURE 7
15.3.8.1 Drawing in Popup Window- I 7
15.3.8.2 Drawing in Popup Window- II 8
15.3.8.3 Drawing in Popup Window- III 8
15.3.9 I
NFORMING BACK TO MAIN WINDOW 9
15.3.10 Q
UIT APPLICATION VIA CONTROL IN POPUP WINDOW 9
SUMMARY 10
EXERCISES 10
Windows Management 2

15.1 Z-Order

The Z order of a window indicates the window's position in a stack of overlapping
windows. This window stack is oriented along an imaginary axis, the z-axis,
extending outward from the screen.
The window at the top of the Z order overlaps all other windows.
The window at the bottom of the Z order is overlapped by all other windows.

15.2 Windows Review
15.2.1 CreateWindow

CreateWindow function have been discussing in our previous lectures. Much of its details
including styles, class name, parent handles, instance handle and coordinates, etc have
been discussed in chapter 11.
CreateWindow Function is used to create window. CreateWindow function can create
parent, child, popup and overlapped windows with dimensions x, y, width and height.
HWND CreateWindow
(
LPCTSTR lpClassName, // registered class name
LPCTSTR lpWindowName, // window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // menu handle or child identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // window-creation data
);

15.2.2 Child Windows

Following are the characteristics of child windows.
A child window always appears within the client area of its parent window.
Child windows are most often as controls.
A child window sends WM_COMMAND notification messages to its parent
window.
Windows Management 3
When a child window is created a unique identifier for that window is specified in
hMenu parameter of CreateWindow()

15.2.3 Window Procedure

LRESULT CALLBACK WindowProc
(
HWND hwnd, // handle to window
UINT uMsg, // WM_COMMAND
WPARAM wParam, // notification code and identifier
LPARAM lParam // handle to control (HWND)
);

15.2.4 Notification code

Common controls are normally taken as child windows that send notification messages to
the parent window when events, such as input from the user, occur in the control. The
application relies on these notification messages to determine what action the user wants
it to take. Except for trackbars, which use the WM_HSCROLL and WM_VSCROLL
messages to notify their parent of changes, common controls send notification messages
as WM_NOTIFY messages.

15.2.5 WM_COMMAND Notification code

The wParam parameter of Window Procedure contains the notification code and
control identifier.
low word: ID of the control n high word: notification code
BUTTON BN_CLICKED
EDIT EN_CHANGE etc

15.3 Example Application

For demonstration purpose we are going to create an example application.

15.3.1 Description

Our application will be parent-child window application. This application will consist of
three push buttons of names:
RECTANGLE
CIRCLE
MESSAGE”
Windows Management 4
And Edit child window or edit control in its client area.
Floating popup window with caption bar and one push button bearing a name”QUIT
APPLICATION”.

15.3.2 Objectives

Parent-child communication
Message Routing
Use of GDI function calls

15.3.3 Windows Management Functions

Building our application, we will use following windows management functions in our
application.

Windows management function - I

HWND GetParent
(
HWND hWnd // handle to child window
);
GetParent function returns the parent handle of the specified child. This function will be
useful when the parent of the child window to use.

Windows management function - II

HWND GetDlgItem
(
HWND hDlg, // handle to dialog box
int nIDDlgItem // control identifier
);
GetDlgItem function returns the handle of a dialog item. Using this function we can
easily get the handle of the edit control, displayed on dialog box.
HWND FindWindow
(
LPCTSTR lpClassName, // class name
LPCTSTR lpWindowName // window name
);
FindWindow function finds the window with the given class name or window name.
Windows Management 5

15.3.4 Window classes

The Window classes used in this application are:
mainWindowClass
popupWindowClass
System Window Classes

15.3.4.1 Main Window class

wc.lpfnWndProc = mainWindowProc;
wc.hInstance = hAppInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_UPARROW);
wc.hbrBackground= (HBRUSH)GetStockObject (GRAY_BRUSH);
wc.lpszClassName= "MainWindowClass";
if(!RegisterClass(&wc))
{
return 0;
}

15.3.4.2 Popup Window class

wc .lpfnWndProc = popupWindowProc;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.hCursor = LoadCursor(NULL, IDC_HELP);
wc.lpszClassName = "PopupWindowClass";
if(!RegisterClass(&wc))
{
return 0;
}

15.3.4.3 System Window classes

System window classes are pre-registered. They do not need to register in our
application. In this application, we will only used to create them not to register them.

15.3.5 Creating Main Windows

Create a Main Window of the Application.
hWndMain = CreateWindow("MainWindowClass",
"Virtual University",
Windows Management 6
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 400, 300,
NULL, NULL, hInstance, NULL
);
// check the returned handle, don’t need to proceed if the returned handle is NULL
if(!hWnd)
{
MessageBox(NULL,”Cannot Create Main Window”,”Error”,
MB_ICONHAND|MB_OK);
return 0;
}
Now create a popup window with caption and visible style bit on.
hWndPopup = CreateWindow("PopupWindowClass", //window name (optional)
"Popup Window", //class name
WS_POPUP | WS_CAPTION | WS_VISIBLE,
250, 250, 300, 250,
hWndMain, NULL, hInstance, NULL
);

15.3.6 Creating Child Windows

Create a button window bearing a text “Rectangle”.
hWndButton=CreateWindow("BUTTON",
"Rectangle",
WS_CHILD | WS_VISIBLE,
10, 10, 100, 50,
hWndMain, 5,
hInstance, NULL
);
Create an Edit Window bearing a text “Message”
CreateWindow("EDIT",
"Message",
WS_CHILD | WS_VISIBLE n | ES_LOWERCASE,
10, 190, 200, 25,
hWndMain, 8,
hInstance, NULL
);
Windows Management 7

15.3.7 User defined Messages

System defined messages are already defined in WINUSER.H
#define WM_LBUTTONDOWN 0x0201 (513)
#define WM_DESTROY 0x0002 (2)
#define WM_QUIT 0x0012 (18)
#define WM_USER 0x0400 (1024)
These message are already defined, user don’t need to define them again.
Here, we will define our own messages.
#define WM_DRAW_FIGURE WM_USER+786
//user defined message are in valid range, in our case it is WM_USER + 786

15.3.8 Application’s Main Window Procedure

Message send to main window will be received and processed in mainWndProc function.
In windows procedure we will process WM_COMMAND message. In
WM_COMMAND message we check the LOWORD and HIWORD parameters of the
messages. In LOWORD we have the control ID, always and in HIWORD we have
notification code. Using control id we identify a window.
case WM_COMMAND:
wControlID = LOWORD(wParam);
wNotificationCode = HIWORD(wParam);
if(wNotificationCode == BN_CLICKED)
{
switch(wControlID)
{
case 5:
SendMessage(hWndPopup, WM_DRAW_FIGURE, RECTANGLE, 0);
break;
case 6:
SendMessage(hWndPopup, WM_DRAW_FIGURE, CIRCLE, 0); break;
case 7: SendMessage(hWndPopup, WM_DRAW_FIGURE,
TEXT_MESSAGE, 0); break;
}

15.3.8.1 Drawing in Popup Window- I

Windows Management 8
here we check the button if button rectangle is pressed then draw a rectangle with Light
Gray stock brush.
case WM_DRAW_FIGURE:
hDC = GetDC(hWndPopup);
switch(wParam)
{
case RECTANGLE:
SelectObject(hDC,GetStockObject(
LTGRAY_BRUSH));
Rectangle(hDC, 50, 10, 230, 150);
break;
}

15.3.8.2 Drawing in Popup Window- II

In case of Circle, we create hatch brush, we created a hatch brush which has a style of
diagonal cross lines. After creating a hatch brush we select it on device context to draw a
figure. After drawing, brush must be deleted.
case CIRCLE:
hBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(170, 150, 180));
SelectObject(hDC, hBrush);
Ellipse(hDC, 70, 10, 210, 150);
DeleteObject(hBrush);
break;

15.3.8.3 Drawing in Popup Window- III

By pressing the button a text must be display with the stock object Ansi variable fonts
and background brush. Text are displayed using TextOut GDI function.
case TEXT_MESSAGE:
{
TextOut(hDC, 50, 100, "Virtual University", 18);
SelectObject(hDC, GetStockObject(
ANSI_VAR_FONT));
SetBkColor(hDC, RGB(10, 255, 20));
TextOut(hDC, 50, 115, "knowledge Beyond Boundaries", 27);
break;
}
ReleaseDC(hWndPopup, hDC);
Windows Management 9

15.3.9 Informing back to Main Window

case WM_DRAW_FIGURE:
hDC = GetDC(hWndPopup);
hwndEdit = GetDlgItem(GetParent(hWnd), 8);
switch(wParam)
{
case RECTANGLE:
SelectObject(hDC,GetStockObject(LTGRAY_BRUSH));
Rectangle(hDC, 50, 10, 230, 150);
SendMessage(hwndEdit, WM_SETTEXT, 0, "Rectangle DrAwN!");
break;
}

15.3.10 Quit Application via control in Popup window

Main window is destroyed through button’s notification message BN_CLICKED. Main
Window can be destroyed using DestroyWindow Function.
WM_CREATE:
CreateWindow("BUTTON",
"Quit Application",
WS_CHILD | WS_VISIBLE, n 75, 155, 150, 40, hWnd, 1234,
hAppInstance, NULL);
break;
case WM_COMMAND:
wControlID = LOWORD(wParam);
wNotificationCode = HIWORD(wParam);
if(wNotificationCode == BN_CLICKED)
{
switch(wControlID)
{
case 1234:
DestroyWindow(GetParent(hWnd));
break;
}
}
Windows Management 10

Summary

This chapter uses Windows management functions whose details have been
discussed in our previous lectures. These functions are very helpful to interact with
windows and hierarchy of windows and also with windows handling, windows
manipulation and windows management. Our main objective in this application was to
create a full fledge application. Before continue, we overviewed all the functions that we
had to use. Function includes GetParent, GetDlgItem, CreateWindow and notification
codes that are sent to window by controls or other child windows. Controls are normally
considered are child windows, because these can be placed in any windows and become
the part of the window but controls can be main window. Notification messages are
considered to transfer informations to parent window by child windows. Child windows
can send notification message to parent windows which aim only to inform about some
events to parent window. The notification events could be e.g. in case of edit control is
selection change i.e. EN_SELCHANGE or EN_CLICKED in case of button. Finally we
wrote a code for our application. This code displays a window and three child windows
including button that contains text like rectangle, messages etc. we also make a popup
window, popup window is not a child window. Popup windows are very useful when to
show message on screen or working in full screen modes in Microsoft Windows
Operating systems.

Exercises

1. Create a full screen popup window and then create another popup window with a
caption box, system menu and with no close style.
2. Create your own status bar and show it in a window at its proper location. This
status bar should display current time.

<Previous Lesson

Visual Programming

Next Lesson>

Home

Lesson Plan

Topics

Go to Top

Next Lesson
Previous Lesson
Lesson Plan
Topics
Home
Go to Top