














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth explanation of creating windows and handling messages in the context of windows application development. Topics covered include registering window classes, setting window attributes, creating windows using the createwindow api, and message dispatching. Students will gain a solid understanding of the role of window classes and procedures in managing graphical user interfaces.
Typology: Study notes
1 / 22
This page cannot be seen from the preview
Don't miss anything!















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.
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
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
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.
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.
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.
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.
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
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.
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.
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.
Style Action
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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
[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
[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".
[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
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.
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).
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.
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.
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.
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.
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.
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.
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 child-
window identifier; it must be unique for all child windows with the same parent window.
This member is Application instance handle.
In Windows NT/2000 or later This value is ignored.
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.
If the CreateWindow function is successful, then it returns a valid handle of the newly
created window. Otherwise it returns NULL.
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.
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.
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.
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);
This member is a HANDLE to the window to which message was sent.
This member specifies the message type; the message could be a Mouse message,
character message, keyboard message, etc. Message is unsigned 32bit number.
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.
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.
The return value is the result of the message processing and depends on the message sent
function.
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