1 An Architectural Overview
1)Getting a good feel for messages is an import part of learning how to write programs for windows.
Windows send a message to your porgram means that Windows calls a function in your program .The parameter of this function describe the message that is being send.The function in your program is know as Window Procedure.
Windows send a message to window by calling window procedure ,The window procedure do some processing based on the message and return control to Windows.
More precisely , a Window is always createdd based on a "window class".The window class identifies the window procedure that precesses messages to the windows.The use of window class allow mutiple window to be based the same window class and hence use the same window procedure.
2) Requsteriing the Window Class
typedef struct
{
UINT style ;
WNDPROC lpfnWndProc ;
int cbClsExtra ;
int cbWndExtra ;
HINSTANCE hInstance ;
HICON hIcon ;
HCURSOR hCursor ;
HBRUSH hbrBackground ;
LPCTSTR lpszMenuName ;
LPCTSTR lpszClassName ;
}
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
2)Creating the Window
Window class define the general Characteristics of the a window.If you want to create window based on the same window class,you can use CreateWindwo which allow you to specify more detail imformation about the window.
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
3)Displaying the window
When thee CreateWindow function return ,windows create a window internal.What it means is that windows allocate a block memory to store the imformation about the window.If you want to show that ,you should call
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
4) The Message Loop
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
typedef struct tagMSG
{
HWND hwnd ;
UINT message ;// message identifier
WPARAM wParam ;
LPARAM lParam ;
DWORD time ; // the time the message is placed on the message queen
POINT pt ; // the mouse coordiante
}
typedef struct tagPOINT
{
LONG x ;
LONG y ;
}
POINT, * PPOINT;
the message filed of message retrived from the message queen is anyting except WM_QUITm ,GetMessage return a nonzero value
wM_QUIT cause GetMessage reuturn 0.
TranslateMessage (&msg) ;
passing the msg struct back to window for some keysboard translation
DispatchMessage (&msg) ;
pass the msessageback to window .Windwo then send the message to the appropriate window procedure for processing.After window
procedure processing the message ,it return control to the windows,which is still in serving the Dispatchmessage
5) The Window Procedure
a The window procedure determined the how the window display on the client area and how the window respose to user input
b LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam),the four parameter is idential to the first four filed of the message struct.
6) Processing the Message
switch (iMsg)
{
case WM_CREATE :
[process WM_CREATE message]
return 0 ;
case WM_PAINT :
[process WM_PAINT message]
return 0 ;
case WM_DESTROY :
[process WM_DESTROY message]
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
when a window procedure process the message ,it should return 0;
7) The WM_PAINT Message
WU_PAINT message is extremely import in window.it inform a program when part or all of the window 's client area are invalid and must be redraw or repaint.
WM_PAINT processing almost always begins with a call to BeginPaint:
hdc = BeginPaint (hwnd, &ps) ; //return a handle to device context
and ends with a call to EndPaint:
EndPaint (hwnd, &ps) ;
8)The WM_DESTROY Message
PostQuitMessage (0) ;
WNDCLASS, * PWNDCLASS ;
#include <windows.h>
/* the same as long _stdcall WndPro(long,unsign int,unsign int ,long)*/
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
/*int _stdcall WinMain(long hInstance,long hPrevInstance,char * szComdLine,int iCmdShow)*/
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
/*
all window createe based on this window class will completely repaint whenever horizational window sizd and vertial
window size change.
*/
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ; // set the window procedure for the window class
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ; // the handle of this program
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;// iCmdShow determine how the window is to be initially displayed on the screen.
UpdateWindow (hwnd) ; // cause the client area to paint
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;//return a handle to device context
GetClientRect (hwnd, &rect) ;// set the struct rect with the dimensions of the client area
DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;// insert a WM_QIUT in the message queue.
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
2 The Window Programming Hurdles
1)Queue and noqueue message
queue message are post the message queue and the noqueue message send to the window procdure directly.