|
|
|
|
Lesson#9
|
Windows Creation and Message Handling
|
|
|
|
9.1 MULTIPLE INSTANCES 2
9.2 WINDOW CLASS 2
9.3 REGISTERING WINDOW CLASS 2
9.4 ELEMENTS OF A WINDOW CLASS 3
9.4.1 CLASS
NAME
5
9.4.2 WINDOW
PROCEDURE
ADDRESS
5
9.4.3 INSTANCE
HANDLE
5
9.4.4 CLASS
CURSOR
5
9.4.5 CLASS
ICONS
6
9.4.6 CLASS
BACKGROUND
BRUSH
6
9.4.7 CLASS
MENU
7
9.4.8 CLASS
STYLES
7
9.5 USING WINDOW CLASS (EXAMPLE) 9
9.6 ABOUT WINDOWS 11
9.6.1 CLIENT
AREA
11
9.6.2 NONCLIENT
AREA
11
9.7 PROTOTYPE OF CREATEWINDOW 12
9.7.1 CLASS
NAME
(LPCLASSNAME)
13
9.7.2 WINDOW
NAME
(LPWINDOWNAME).
13
9.7.3 WINDOW
STYLES
(DWSTYLE)
13
BITWISE INCLUSIVE-OR OPERATOR ‘|’ 16
9.7.4 HORIZONTAL
POSITION
OF THE WINDOW
(X)
16
9.7.5 VERTICAL
POSITION
OF THE WINDOW
(Y)
16
9.7.6 WIDTH OF THE
WINDOW
(NWIDTH)
16
9.7.7 HEIGHT OF THE
WINDOW
(NHEIGHT)
17
9.7.8 PARENT OF THE
WINDOW
(HWNDPARENT)
17
9.7.9 MENU OF THE
WINDOW
(HMENU)
17
9.7.10 INSTANCE
HANDLE
(HINSTANCE)
17
9.7.11 LONG
PARAM
(LPPARAM)
17
9.7.12 RETURN
VALUE
17
9.8 USING WINDOWS(EXAMPLE) 18
9.9 MESSAGES IN WINDOWS 18
9.9.1 MESSAGE
QUEUING
19
9.9.2 MESSAGE
ROUTING
19
9.10 WINDOW PROCEDURE 19
9.10.1 HANDLE
TO WINDOW(HWND)
19
9.10.2 MESSAGE
TYPE(UMSG)
20
9.10.3 MESSAGE’S
WPARAM(WPARAM)
20
9.10.4 MESSAGE’S
LPARAM(LPARAM)
20
9.10.5 RETURN
VALUE
20
The return value is the result of the message processing and depends on the
message sent. 20
9.11 GETTING MESSAGE FROM MESSAGE QUEUE 20
9.12 MESSAGE DISPATCHING 21
SUMMARY 21
EXERCISES 22
Windows Creation and Message
Handling 2
9.1 Multiple Instances
Every running application is an Application Instance. So if you open more
than one
application, more than one instance will be running simultaneously. If you
write a
program and run it, this running program will be known as a process running
in memory.
Whenever you press ALT-CONTROL-DELETE, you can open Task Manager to watch
all the processes present in task list, running under Windows. Each process
can have one
or more than one windows. Every process has at least one thread running,
which is UI
thread.
9.2 Window Class
Every window in Windows has
its own registered Window class. This window class has
set of attributes which are later used by windows. These attributes could be
windows
background brush, windows style, cursors, Icons, etc. So Windows class tells
the
Operating system about the characteristics and physical layout of its
windows. Window
Class is simply a structure named WNDCLASS or WNDCLASSEX that only contains
set of attributes for window.
Each window class has an associated window procedure shared by all windows
of the
same class. The window procedure processes messages for all windows of that
class and
therefore, controls their behavior and appearance. For more information, see
Window
Procedures.
A process must register a window class before it creates a window.
Registering a window
class associates a window procedure, class styles and other class attributes
particularly a
class name. When a process specifies a class name in the
CreateWindow or
CreateWindowEx function, the
system creates a window using a registered class name.
A window class defines the attributes of a window such as style, icon,
cursor, menu, and
window procedure. The first step in registering a window class is to fill a
WNDCLASS
structure. For more information, see Elements of a Window Class. Next step
is to pass the
structure to the RegisterClass function.
To register an application global class, specify the CS_GLOBALCLASS style in
the
style member of the WNDCLASSEX structure. When registering an
application local
class, do not specify the CS_GLOBALCLASS style.
If you register the window class using the ANSI version of
RegisterClassEx,
RegisterClassExA, the application requests that the system pass text
parameters of
messages to the windows of the created class using the ANSI character set;
if you register
the class using the Unicode version of RegisterClassEx,
RegisterClassExW, the
application requests that the system pass text parameters of messages to the
windows of
the created class using the Unicode character set. The
IsWindowUnicode function
enables
Windows Creation and Message Handling
3
applications to query the nature of each window. For more information on
ANSI and
Unicode functions, see Conventions for Function Prototypes in Microsoft help
documents.
The executable or DLL that registered the class is the owner of the class.
The system
determines class ownership from the hInstance member of the
WNDCLASSEX structure passed to the RegisterClassEx function when the class
is registered. For DLLs,
the hInstance member must be the handle to the .dll instance.
Windows 2000 or Above: The
class is not destroyed when the .dll that owns it is
unloaded. Therefore, if the system calls the window procedure for a window
of that class,
it will cause an access violation, because the .dll containing the window
procedure is no
longer in memory. The process must destroy all windows using the class
before the .dll is
unloaded and call the UnregisterClass function.
ATOM RegisterClass(
CONST WNDCLASS *lpWndClass
)
The complete description its parameters can be found from
Microsoft Developer Network
BOOL UnregisterClass(
LPCTSTR lpClassName,
HINSTANCE hInstance
);
The complete description its parameters can be found from
Microsoft Developer Network
This function inputs a pointer to CONST WNDCLASS structure and
returns ATOM.
ATOM is a unique identifier that will be returned from RegisterClass. ATOM
is unsigned
short value.
9.3 Elements of a Window Class
The elements of a window class define the default behavior of windows
belonging to the
class. The application that registers a window class assigns elements to the
class by
setting appropriate members in a WNDCLASSEX structure and passing the
structure to
the RegisterClassEx function. The
GetClassInfoEx and
GetClassLong functions retrieve
information about a given window class. The
SetClassLong function
changes elements of
a local or global class that the application has already registered.
Windows Creation and Message Handling
4
The Structure of Window Class is as follows.
WNDCLASS Structure
typedef struct _WNDCLASS {
LPCTSTR lpszClassName;
WNDPROC lpfnWndProc;
UINT style;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
} WNDCLASS, *PWNDCLASS;
Although a complete window class consists of many elements, the system
requires the
application which supplies a class name, the window-procedure address and an
instance
handle. Use the other elements to define default attributes for windows of
the class, such
as the shape of the cursor and the content of the menu for the window. You
must
initialize any unused members of the WNDCLASSEX structure to zero or
NULL. The
window class elements are as shown in the following table.
Element Purpose
Class Name Distinguishes the class from other registered classes.
Window
Procedure
Address
Pointer to the function that processes all messages sent to windows in
the class and defines the behavior of the window.
Instance Handle Identifies the application or .dll that registered the
class.
Class Cursor Defines the mouse cursor that the system displays for a window
of the
class.
Class Icons Defines the large icon and the small icon (Windows NT 4.0 and
later).
Class
Background
Brush.
Defines the color and pattern that fill the client area when the window is
opened or painted.
Class Menu Specifies the default menu for windows that do not explicitly
define a
menu.
Class Styles
Defines how to update the window after moving or resizing it, how to
process double-clicks of the mouse, how to allocate space for the device
context, and other aspects of the window.
Extra Class
Memory
Specifies the amount of extra memory, in bytes, that the system should
reserve for the class. All windows in the class share the extra memory
Windows Creation and Message Handling
5
and can use it for any application-defined purpose. The system
initializes this memory to zero.
Extra Window
Memory
Specifies the amount of extra memory, in bytes, that the system should
reserve for each window belonging to the class. The extra memory can
be used for any application-defined purpose. The system initializes this
memory to zero.
9.3.1 Class Name
Every window class needs a Class Name
to distinguish one class from another. Assign a
class name by setting the lpszClassName member of the WNDCLASSEX
structure to
the address of a null-terminated string that specifies the name. Because
window classes
are process specific, window class names need to be unique only within the
same process.
Also, because class names occupy space in the system's private ATOM table,
you should
keep class name strings as short a possible.
The GetClassName function
retrieves the name of the class to which a given window
belongs.
9.3.2 Window Procedure Address
Every class needs a window-procedure address to define the entry point of
the window
procedure used to process all messages for windows in the class. The system
passes
messages to the procedure when it requires the window to carry out tasks,
such as
painting its client area or responding to input from the user. A process
assigns a window
procedure to a class by copying its address to the lpfnWndProc member
of the
WNDCLASSEX structure. For more information, see Window Procedures.
9.3.3 Instance Handle
Every window class requires an instance handle to identify the application
or .dll that
registers the class. The system requires instance handles to keep track of
all modules. The
system assigns a handle to each copy of a running executable or .dll.
The system passes an instance handle to the entry-point function of each
executable. The
executable or .dll assigns this instance handle to the class by copying it
to the hInstance member of the WNDCLASSEX structure.
9.3.4 Class Cursor
The class cursor defines the
shape of the cursor when it is in the client area of a window
in the class. The system automatically sets the cursor to the given shape
when the cursor
enters the window's client area and ensures it keeps that shape while it
remains in the
client area. To assign a cursor shape to a window class, load a predefined
cursor shape by
using the LoadCursor
function and then assign the returned cursor handle to the hCursor Windows
Creation and Message Handling 6
member of the WNDCLASSEX structure. Alternatively, provide a custom
cursor
resource and use the LoadCursor
function to load it from the application's resources.
The system does not require a class cursor. If an application sets the
hCursor member of
the WNDCLASSEX structure to NULL, no class cursor is defined. The
system assumes
the window sets the cursor shape each time the cursor moves into the window.
A window
can set the cursor shape by calling the
SetCursor function whenever the window receives
the WM_MOUSEMOVE message.
9.3.5 Class Icons
A class icon is a picture
that the system uses to represent a window of a particular class.
An application can have two class icons — one large and one small. The
system displays
a window's large class icon
in the Task-switch window that appears when the user
presses ALT+TAB, and in the large icon views of the task bar and explorer.
The small
class icon appears in a window's title bar and in the small icon
views of the task bar and
explorer.
To assign a large and small icon to a window class, specify the handles of
the icons in the
hIcon and hIconSm members of the WNDCLASSEX structure. The
icon dimensions
must conform to required dimensions for large and small class icons. For a
large class
icon, you can determine the required dimensions by specifying the SM_CXICON
and
SM_CYICON values in a call to the
GetSystemMetrics function. For a small class icon,
specify the SM_CXSMICON and SM_CYSMICON values.
If an application sets the hIcon and hIconSm members of the
WNDCLASSEX structure
to NULL, the system uses the default application icon as the large and small
class icons
for the window class. If you specify a large class icon but not a small one,
the system
creates a small class icon based on the large one. However, if you specify a
small class
icon but not a large one, the system uses the default application icon as
the large class
icon and the specified icon as the small class icon.
You can override the large or small class icon for a particular window by
using the
WM_SETICON message. You can retrieve the current large or small class icon
by using
the WM_GETICON message.
9.3.6 Class Background Brush
A class background brush
prepares the client area of a window for subsequent drawing
by the application. The system uses the brush to fill the client area with a
solid color or
pattern, thereby removing all previous images from that location whether
they belong to
the window or not. The system notifies a window that its background should
be painted
by sending the WM_ERASEBKGND message to the window.
To assign a background brush to a class, create a brush by using the
appropriate GDI
functions and assign the returned brush handle to the hbrBackground
member of the
WNDCLASSEX structure.
Windows Creation and Message Handling
Instead of creating a brush, an application can set the hbrBackground
member to one of
the standard system color values. For a list of the standard system color
values, see
System Colors from
Microsoft Documentation.
To use a standard system color, the application must increase the
background-color value
by one. For example, COLOR_BACKGROUND + 1 are the system background color.
Alternatively, you can use the
GetSysColorBrush function to retrieve a handle to a brush
that corresponds to a standard system color, and then specify the handle in
the
hbrBackground member of the WNDCLASSEX structure.
The system does not require that a window class has a class background
brush. If this
parameter is set to NULL, the window must paint its own background whenever
it
receives the WM_ERASEBKGND message.
9.3.7 Class Menu
A class menu defines the
default menu to be used by the windows in the class if no
explicit menu is given when the windows are created. A menu is a list of
commands from
which a user can choose actions for the application to carry out.
You can assign a menu to a class by setting the lpszMenuName member
of the
WNDCLASSEX structure to the address of a null-terminated string that
specifies the
resource name of the menu. The menu is assumed to be a resource in the given
application. The system automatically loads the menu when it is needed. If
the menu
resource is identified by an integer and not by a name, the application can
set the
lpszMenuName member to that integer by applying the MAKEINTRESOURCE macro
before assigning the value.
The system does not require a class menu. If an application sets the
lpszMenuName member
of the WNDCLASSEX structure to NULL, window in the class has no menu
bar. Even if no class menu is given, an application can still define a menu
bar for a
window when it creates the window.
If a menu is given for a class and a child window of that class is created,
the menu is
ignored.
9.3.8 Class Styles
The class styles define additional elements of the window class. Two or more
styles can
be combined by using the bitwise OR (|) operator. To assign a style to a
window class,
assign the style to the style member of the WNDCLASSEX
structure. The class styles
are as follows.
Windows Creation and Message Handling
8
Style Action
CS_BYTEALIGNCLIENT
Aligns the window's client area on a byte boundary (in the
x direction). This style affects the width of the window and
its horizontal placement on the display.
CS_BYTEALIGNWINDOW
Aligns the window on a byte boundary (in the x direction).
This style affects the width of the window and its
horizontal placement on the display.
CS_CLASSDC
Allocates one device context to be shared by all windows
in the class. Because window classes are process specific, it
is possible for multiple threads of an application to create a
window of the same class. It is also possible for the threads
to attempt to use the device context simultaneously. When
this happens, the system allows only one thread to
successfully finish its drawing operation.
CS_DBLCLKS
Sends a double-click message to the window procedure
when the user double-clicks the mouse while the cursor is
within a window belonging to the class.
CS_DROPSHADOW
Windows XP: Enables the drop shadow effect on a
window. The effect is turned on and off through
SPI_SETDROPSHADOW. Typically, this is enabled for
small, short-lived windows such as menus to emphasize
their Z order relationship to other windows.
CS_GLOBALCLASS Specifies that the window class is an application global
class. For more information.
CS_HREDRAW Redraws the entire window if a movement or size
adjustment changes the width of the client area.
CS_NOCLOSE Disables Close on the window menu.
CS_OWNDC Allocates a unique device context for each window in the
class.
CS_PARENTDC
Sets the clipping rectangle of the child window to that of
the parent window so that the child can draw on the parent.
A window with the CS_PARENTDC style bit receives a
regular device context from the system's cache of device
contexts. It does not give the child the parent's device
context or device context settings. Specifying
CS_PARENTDC enhances an application's performance.
CS_SAVEBITS
Saves, as a bitmap, the portion of the screen image
obscured by a window of this class. When the window is
removed, the system uses the saved bitmap to restore the
screen image, including other windows that were obscured.
Therefore, the system does not send WM_PAINT
messages to windows that were obscured if the memory
used by the bitmap has not been discarded and if other
screen actions have not invalidated the stored image.
Windows Creation and Message Handling
9
This style is useful for small windows (for example, menus
or dialog boxes) that are displayed briefly and then
removed before other screen activity takes place. This style
increases the time required to display the window, because
the system must first allocate memory to store the bitmap.
CS_VREDRAW Redraws the entire window if a movement or size
adjustment changes the height of the client area.
9.4 Using Window Class (Example)
#include <windows.h>
// Declaration of Global variable
HINSTANCE hinst;
// Function prototypes.
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE
hInstPrev, LPSTR str, int cmd);
InitApplication(HINSTANCE);
InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM,
LPARAM);
// Application entry point.
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE
hInstPrev, LPSTR str, int cmd)
{
MSG msg;
if (!InitApplication(hinstance))
return FALSE;
return 0;
}
//Initialize Application by registering class
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASSEX wcx;
// Fill in the window
class structure with
//parameters
//
that describe the main
window.
Windows Creation and Message
Handling 10
wcx.cbSize = sizeof(wcx);
//
size of
structure
wcx.style = CS_HREDRAW |
CS_VREDRAW; //
redraw if
size changes
wcx.lpfnWndProc = MainWndProc;
//
points to
window procedure
wcx.cbClsExtra = 0;
//
no extra
class memory
wcx.cbWndExtra = 0;
//
no extra
window memory
wcx.hInstance = hinstance;
//
handle to
instance
wcx.hIcon = LoadIcon(NULL,
IDI_APPLICATION);
//
predefined
app. icon
wcx.hCursor = LoadCursor(NULL,
IDC_ARROW); //
predefined
arrow
wcx.hbrBackground =
GetStockObject(
WHITE_BRUSH); //
white
background brush
wcx.lpszMenuName = "MainMenu";
//
name of menu
resource
wcx.lpszClassName =
"MainWClass"; //
name of
window class
wcx.hIconSm =
LoadImage(hinstance,
//
small class
icon
MAKEINTRESOURCE(5),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
//
Register the window
class.
return RegisterClassEx(&wcx);
}
Windows Creation and Message
Handling 11
9.5 About Windows
Every graphical Microsoft® Windows®-based application creates at least one
window,
called the main window that
serves as the primary interface between the user and the
application. Most applications also create other windows, either directly or
indirectly, to
perform tasks related to the main window. Each window plays a part in
displaying output
and receiving input from the user.
When you start an application, the system also associates a taskbar button
with the
application. The taskbar button
contains the program icon and title. When the application
is active, its taskbar button is displayed in the pushed state.
An application window includes elements such as a title bar, a menu bar, the
window
menu (formerly known as the system menu), the minimize button, the maximize
button,
the restore button, the close button, a sizing border, a client area, a
horizontal scroll bar,
and a vertical scroll bar. An application's main window typically includes
all of these
components. The following illustration shows these components in a typical
main
window.
9.5.1 Client Area
The client area is the part
of a window where the application displays output, such as text
or graphics. For example, a desktop publishing application displays the
current page of a
document in the client area. The application must provide a function, called
a window
procedure, to process input to the window and display output in the client
area.
9.5.2 Nonclient Area
The title bars, menu bar, window menu, minimizes and maximize buttons,
sizing border,
and scroll bars are referred to collectively as the window's
nonclient area. The system
Windows Creation and Message Handling
12
manages most aspects of the nonclient
area, and the application manages the appearance
and behavior of its client area.
The title bar displays an
application-defined icon and line of text; typically, the text
specifies the name of the application or indicates the purpose of the
window. An
application specifies the icon and text when creating the window. The title
bar also makes
it possible for the user to move the window by using a mouse or other
pointing device.
Most applications include a menu bar
that lists the commands supported by the
application. Items in the menu bar represent the main categories of
commands. Clicking
an item on the menu bar typically opens a pop-up menu whose items correspond
to the
tasks within a given category. By clicking a command, the user directs the
application to
carry out a task.
The window menu is created
and managed by the system. It contains a standard set of
menu items that, when chosen by the users, sets a window’s size or position,
closes the
application, or performs tasks.
The buttons in the upper-right corner affect the size and position of the
window. When
you click the maximize button,
the system enlarges the window to the size of the screen
and positions the window, so it covers the entire desktop, minus the
taskbar. At the same
time, the system replaces the maximize button with the restore button. When
you click
the restore button, the
system restores the window to its previous size and position. When
you click the minimize button,
the system reduces the window to the size of its taskbar
button, positions the window over the taskbar button, and displays the
taskbar button in
its normal state. To restore the application to its previous size and
position, click its
taskbar button. When you click the
close button, application exits.
The sizing border is an area
around the perimeter of the window that enables the user to
size the window by using a mouse or other pointing device.
The horizontal scroll bar
and vertical scroll bar
convert mouse or keyboard input into
values that an application uses to shift the contents of the client area
either horizontally or
vertically. For example, a word-processing application that displays a
lengthy document
typically provides a vertical scroll bar to enable the user to page up and
down through the
document.
9.6 Prototype of CreateWindow
Here is a prototype of CreateWindow
Function.
HWND CreateWindow(
LPCTSTR lpClassName; //class name (identification)
LPCTSTR lpWindowName; //Window caption bar Name
DWORD dwStyle; // style of the windows
Int x; //starting X point of window on screen
Windows Creation and Message Handling
13
Int y; //starting Y point of window on screen
Int width; //Width of the window from starting point
Int height; //height of the window from starting Y point
HWND hWndParent; //handle the parent window if any
HMENU hMenu; // handle the Menu if any
HINSTANCE hInstance; //handle of the instance
LPVOID lpParam; //void parameter
);
//Documentation is described below
9.6.1 Class Name (lpClassName)
[in] Pointer to a null-terminated string or a class atom created by a
previous call to the
RegisterClass or RegisterClassEx function. The atom must be in
the low-order word of
lpClassName; the high-order
word must be zero.
If lpClassName is a string, it
specifies the window class name. The class name can be any
name registered with
RegisterClass or
RegisterClassEx,
provided that the module that
registers the class is also the module that creates the window. The class
name can also be
any of the predefined system class names
9.6.2 Window Name (lpWindowName)
[in] Pointer to a null-terminated string that specifies the window name.
If the window style specifies a title
bar, the window title pointed to by lpWindowName is
displayed in the title bar. When using
CreateWindow
to create controls, such as buttons,
check boxes, and static controls, use lpWindowName to specify the text of
the control.
When creating a static control with the SS_ICON style, use lpWindowName to
specify the
icon name or identifier. To specify an identifier, use the syntax "#num".
9.6.3 Window Styles (dwStyle)
[in] Specifies the style of the window being created. This parameter can
be a combination
of Window Styles. The following styles can be specified wherever a window
style is required.
Style Meaning
WS_BORDER Creates a window that has a thin-line border.
WS_CAPTION Creates a window that has a title bar (includes the
WS_BORDER style).
WS_CHILD Creates a child window. A window with this style
Windows Creation and Message Handling
14
cannot have a menu bar. This style cannot be used with
the WS_POPUP style.
WS_CHILDWINDOW Same as the WS_CHILD style.
WS_CLIPCHILDREN Excludes the area occupied by child windows when
drawing occurs within the parent window. This style is
used when creating the parent window.
WS_CLIPSIBLINGS Clips child windows relative to each other; that is, when
a particular child window receives a WM_PAINT
message, the WS_CLIPSIBLINGS style clips all other
overlapping child windows out of the region of the child
window to be updated. If WS_CLIPSIBLINGS is not
specified and child windows overlap, it is possible, when
drawing within the client area of a child window, to
draw within the client area of a neighboring child
window.
WS_DISABLED Creates a window that is initially disabled. A disabled
window cannot receive input from the user. To change
this after a window has been created, use
EnableWindow.
WS_DLGFRAME Creates a window that has a border of a style typically
used with dialog boxes. A window with this style cannot
have a title bar.
WS_GROUP Specifies the first control of a group of controls. The
group consists of this first control and all controls
defined after it, up to the next control with the
WS_GROUP style. The first control in each group
usually has the WS_TABSTOP style so that the user can
move from group to group. The user can subsequently
change the keyboard focus from one control in the group
to the next control in the group by using the direction
keys.
You can turn this style on and off to change dialog box
navigation. To change this style after a window has been
created, use SetWindowLong.
WS_HSCROLL Creates a window that has a horizontal scroll bar.
WS_ICONIC Creates a window that is initially minimized. Same as
the WS_MINIMIZE style.
WS_MAXIMIZE Creates a window that is initially maximized.
WS_MAXIMIZEBOX Creates a window that has a maximize button. Cannot be
combined with the WS_EX_CONTEXTHELP style.
The WS_SYSMENU style must also be specified.
WS_MINIMIZE Creates a window that is initially minimized. Same as
the WS_ICONIC style.
Windows Creation and Message Handling
15
WS_MINIMIZEBOX Creates a window that has a minimize button. Cannot be
combined with the WS_EX_CONTEXTHELP style.
The WS_SYSMENU style must also be specified.
WS_OVERLAPPED Creates an overlapped window. An overlapped window
has a title bar and a border. Same as the WS_TILED
style.
WS_OVERLAPPEDWINDOW Creates an overlapped window with the
WS_OVERLAPPED, WS_CAPTION,
WS_SYSMENU, WS_THICKFRAME,
WS_MINIMIZEBOX, and WS_MAXIMIZEBOX
styles. Same as the WS_TILEDWINDOW style.
WS_POPUP Creates a pop-up window. This style cannot be used
with the WS_CHILD style.
WS_POPUPWINDOW Creates a pop-up window with WS_BORDER,
WS_POPUP, and WS_SYSMENU styles. The
WS_CAPTION and WS_POPUPWINDOW styles must
be combined to make the window menu visible.
WS_SIZEBOX Creates a window that has a sizing border. Same as the
WS_THICKFRAME style.
WS_SYSMENU Creates a window that has a window menu on its title
bar. The WS_CAPTION style must also be specified.
WS_TABSTOP Specifies a control that can receive the keyboard focus
when the user presses the TAB key. Pressing the TAB
key changes the keyboard focus to the next control with
the WS_TABSTOP style.
You can turn this style on and off to change dialog box
navigation. To change this style after a window has been
created, use SetWindowLong.
WS_THICKFRAME Creates a window that has a sizing border. Same as the
WS_SIZEBOX style.
WS_TILED Creates an overlapped window. An overlapped window
has a title bar and a border. Same as the
WS_OVERLAPPED style.
WS_TILEDWINDOW Creates an overlapped window with the
WS_OVERLAPPED, WS_CAPTION,
WS_SYSMENU, WS_THICKFRAME,
WS_MINIMIZEBOX, and WS_MAXIMIZEBOX
styles. Same as the WS_OVERLAPPEDWINDOW
style.
WS_VISIBLE Creates a window that is initially visible.
This style can be turned on and off by using
ShowWindow or SetWindowPos
Windows Creation and Message Handling
16
WS_VSCROLL Creates a window that has a vertical scroll bar.
This is updated documents from Microsoft
Help Desk.
Bitwise Inclusive-OR Operator ‘|’
The bitwise inclusive OR ‘|’
operator compares the values (in binary format) of each
operand and yields a value whose bit pattern shows which bits in either of
the operands
has the value 1 (one). If both of the bits are 0 (zero), the result of the
comparison is 0
(zero); otherwise, the result is 1 (one).
9.6.4 Horizontal Position of the Window (x)
This member specifies the initial horizontal position of the window. For
an overlapped or
pop-up window, the x
parameter is the initial x-coordinate of the window's upper-left
corner, in screen coordinates. For a child window,
x is the x-coordinate of the
upper-left
corner of the window relative to the upper-left corner of the parent
window's client area.
If this parameter is set to
CW_USEDEFAULT, the system selects the default position for
the window's upper-left corner and ignores the y parameter. CW_USEDEFAULT is
valid
only for overlapped windows; if it is specified for a pop-up or child
window, the x and y
parameters are set to zero.
9.6.5 Vertical Position of the Window (y)
This member specifies the initial vertical position of the window. For
an overlapped or
pop-up window, the y
parameter is the initial y-coordinate of the window's upper-left
corner, in screen coordinates. For a child window,
y is the initial
y-coordinate of the
upper-left corner of the child window relative to the upper-left corner of
the parent
window's client area. For a list box, y
is the initial y-coordinate of the upper-left corner of
the list box's client area relative to the upper-left corner of the parent
window's client
area.
If an overlapped window is created with
the WS_VISIBLE style bit set and the x
parameter is set to CW_USEDEFAULT, the system ignores the y parameter.
9.6.6 Width of the Window (nWidth)
This specifies the width, in device units, of the window. For overlapped
windows, nWidth
is either the window's width, in screen coordinates, or
CW_USEDEFAULT. If nWidth is
CW_USEDEFAULT, the system selects a default width and height for the window;
the
default width extends from the initial x-coordinate to the right edge of the
screen, and the
default height extends from the initial y-coordinate to the top of the icon
area.
Windows Creation and Message Handling
17
CW_USEDEFAULT is valid only for overlapped windows; if CW_USEDEFAULT is
specified for a pop-up or child window,
nWidth and nHeight
are set to zero.
9.6.7 Height of the Window (nHeight)
This member specifies the height, in device units, of the window. For
overlapped
windows, nHeight is the
window's height, in screen coordinates.
If nWidth is set to CW_USEDEFAULT, the
system ignores nHeight.
9.6.8 Parent of the Window (hWndParent)
This member is a HANDLE to the parent or owner window of the window
being created.
To create a child window or an owned window, supply a valid window handle.
This
parameter is optional for pop-up windows.
9.6.9 Menu of the Window (hMenu)
This member is a HANDLE to a menu, or specifies a child-window
identifier depending
on the window style. For an overlapped or pop-up window,
hMenu identifies the menu to
be used with the window; it can be NULL if the class menu is to be used. For
a child
window, hMenu specifies the
child-window identifier, an integer value used by a dialog
box control to notify its parent about events. The application determines
the childwindow
identifier; it must be unique for all child windows with the same parent
window.
9.6.10 Instance Handle (hInstance)
This member is Application instance handle.
In Windows NT/2000 or later
This value is ignored.
9.6.11 Long Param (lpParam)
This member is a pointer to a value to be passed to the window through
the
CREATESTRUCT structure passed in the
lParam parameter the
WM_CREATE
message. If an application calls CreateWindow to create a
multiple document interface
(MDI) client window, lpParam
must point to a CLIENTCREATESTRUCT structure.
9.6.12 Return Value
If the CreateWindow
function is successful, then it returns a valid handle of the newly
created window. Otherwise it returns NULL.
Windows Creation and Message Handling
18
9.7 Using Windows (Example)
HINSTANCE hinst;
HWND hwndMain;
//
Create the main window.
hwndMain = CreateWindowEx(
0, //
no extended styles
"MainWClass",
//
class name
"Main Window",
//
window name
WS_OVERLAPPEDWINDOW |
//
overlapped window
WS_HSCROLL |
//
horizontal scroll bar
WS_VSCROLL,
//
vertical scroll bar
CW_USEDEFAULT,
//
default horizontal
position
CW_USEDEFAULT,
//
default vertical
position
CW_USEDEFAULT,
//
default width
CW_USEDEFAULT,
//
default height
(HWND) NULL,
//
no parent or owner
window
(HMENU) NULL,
//
class menu used
hinstance,
//
instance handle
NULL);
//
no window creation data
if (!hwndMain)
return FALSE;
Now Show the window using the flag specified by the program that
started the
application, and send the application WM_PAINT message.
ShowWindow(hwndMain, SW_SHOWDEFAULT);
UpdateWindow(hwndMain);
9.8 Messages in Windows
Unlike MS-DOS-based applications, Win32®-based applications are
event-driven. They
do not make explicit function calls (such as C run-time calls) to
obtain input.
Instead, they wait for the system to pass input to them. The system passes
all input for an
application to the various windows in the application. Each window has a
function, called
Windows Creation and Message Handling
19
a window procedure that the system calls whenever it has input for the
window. The
window procedure processes the input and returns control to the system.
Note: We are presenting here a brief
description of messages. Detailed discussion about
messages, message routing, message types, message filtering etc will be
given in next
lectures.
9.8.1 Message Queuing
• Operating system keeps the generated
messages in a queue.
• Every application has its own message queue.
Messages generated in a system first reside in System Message Queue, then
dispatch
to application message queue and to the windows procedure.
Windows programming is basically message driven programming.
9.8.2 Message Routing
The system uses two methods to route messages to a window procedure: posting
messages to a first-in, first-out queue called a message queue, a
system-defined memory
object that temporarily stores messages, and sending messages directly to a
window
procedure.
Messages posted to a message queue are called queued messages. They are
primarily the
result of user input entered through the mouse or keyboard.
9.9 Window Procedure
Every window has its procedure that is called windows procedure. All
messages that are
sent be DispatchMessage API
or SendMessage API will be
received by windows
procedure. So windows procedure is the particular address in memory that
receives
messages. Windows operating system gets this address through registered
window class
member lpfnWndProc. You have
to provide address or name of window procedure in
windows class.
Windows procedure receives four parameters,
LRESULT (CALLBACK *WNDPROC) (HWND hWnd,UINT message,WPARAM
wParam,LPARAM lParam);
9.9.1 Handle to Window(hWnd)
This member is a HANDLE to the window to which message was sent.
Windows Creation and Message Handling
20
9.9.2 Message Type(uMsg)
This member specifies the message type; the message could be a Mouse
message,
character message, keyboard message, etc. Message is unsigned 32bit number.
9.9.3 Message’s WPARAM(wParam)
This specifies additional message information. The contents of this
parameter depend on
the value of the uMsg
parameter e.g. key down message keeps the key pressed value in
this parameter.
9.9.4 Message’s LPARAM(lParam)
This specifies additional message information. The contents of this
parameter depend on
the value of the uMsg
parameter e.g. mouse down messages keep information of mouse
pointer’s x and y position in this parameter.
9.9.5 Return Value
The return value is the result of the message processing and depends on
the message sent
function.
9.10 Getting message from Message Queue
We can get message from message Queue by using
GetMessage or
PeekMessage APIs.
The GetMessage function
retrieves a message from the calling thread's message queue
and also removes the message from the queue. And then function dispatches
incoming
sent messages until a posted message is available for retrieval.
GetMessage inputs four
parameters,
BOOL GetMessage()
(
LPMSG lpMsg,
HWND hWnd,
UINT wMsgFilterMin,
UINT wMsgFilterMax
)
lpMsg
[out] Pointer to an MSG structure that receives message
information from the
thread's message queue.
hWnd
[in] Handle to the window whose messages are to be retrieved. The
window must
belong to the calling thread. The following value has a special meaning.
wMsgFilterMin
Windows Creation and Message
Handling 21
[in] Specifies the integer value of the lowest message value to be
retrieved. Use
WM_KEYFIRST to specify the first keyboard message or WM_MOUSEFIRST
to specify the first mouse message.
wMsgFilterMax
[in] Specifies the integer value of the highest message value to
be retrieved. Use
WM_KEYLAST to specify the first keyboard message or WM_MOUSELAST to
specify the last mouse message.
return Value
If the function retrieves a message other than WM_QUIT,
the return value is
nonzero. If the function retrieves the WM_QUIT message, the return
value is zero.
If there is an error, the return value is -1. For example, the function
fails if hWnd is an
invalid window handle or lpMsg
is an invalid pointer. To get extended error information,
use GetLastError function.
9.11 Message Dispatching
After getting message from message queue, message is dispatched to the
actual window
procedure. For dispatching messages to window procedure, we use
DispatchMessage
API.
DispatchMessage inputs one
argument that is pointer to MSG structure.
BOOL DispatchMessage
(
MSG *lpMsg
)
Summary
In this lecture, we learnt how to Register Window class using
RegisterClass API, and
how to set attributes of window class structure. After registration of
Window class we
learnt to use CreateWindow
API. CreateWindow API uses
window class name, caption
name, style of window, starting points, width and height of windows and
windows parent
handle or handle to owner mainly. We have window handle in variable of type
handle to
window. Using window handle we can send different types of messages to the
window.
Then we learnt how to get messages from application message queue. And after
getting
messages from application message queue, we dispatch messages to the windows
message handling procedure. Windows message procedure inputs four
parameters. These
parameters are also the members of MSG structure. These parameters are
important for
every one who is developing applications for Windows.
Windows Creation and Message Handling
22
Exercises
Write down a code which would create and destroy window successfully.
Further more
show message box when the user closes window.
|
|
|
|