You are on page 1of 8

www.huahongquan.

com

Topic 1b Windows and Messages


Trong WIN32 API c hm CreateWindow to ra cc ca s (windows). Tuy nhin s dng c tt hm ny th chng ta cn mt s kin thc nn trc. Tng Quan: Mt ca s ( window ) th cha 1 thanh tiu ( title bar ) hin tn chng trnh, 1 menu , toolbar v scroll bar. Mt loi ca s khc na l dialog box ( c th c / khng c title bar ) Cc nt nhn (push button ), radio button, checkboxes, list boxes, scroll bars v cc vng text cng c th c gi l 1 window. Hay cn gi l child windows hay control windows hay child window controls. Cc ca s th tng tc vi ngi dng qua cc dng messages. Cc ca s cng dng cc messages giao tip vi cc ca s khc. V d: Khi bn gi gc ca 1 ca s v ko thay i rng ca ca s th ni dung trong ca s cng t thay i theo. Nhng lm th no m ng dng bit rng ngi dng thay i rng ca ca s? Windows gi 1 message ti cho ng dng. Ngha l Windows gi 1 hm trong ng dng m bn vit. Hm gi l WINDOW PROCEDURE Khi mt ng dng bt u c khi chy, th Windows s to mt hang i message ( queue message ) cha tt c cc message ca tt c cc ca s m ng dng s to. The HELLOWIN Program Chng ta s to mt chng trnh hin th mt ca s chnh v t Hello Windows chnh gia. Cc bn ti file cpp y chy th sau chng ta s i su vo chi tit trang sau: http://www.mediafire.com/?373eyv6e2t7ch5t

www.huahongquan.com Ch : dng PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ; Nu cc bn uncomment th khi chy chng trnh s c m thanh bo. Tuy nhin cn phi thm th vin WINMM.LIB. thm th vin, cc bn chn chut phi vo Project > properties . Linker > INPUT . Chn mc mi tn phi ca Additional Dependencies > Edit. V thm WINMM.LIB vo.

S lc v chng trnh:
thc hin mt ng dng WIN32 th cn ti thiu 5 bc sau: Register Class o Create Windows Show Windows Xy dng hm x l Vng lp x l message

Chi tit: Trc tin, cc bn lu l cc c php trn chng ta s khng cn thuc lng m sau ny khi cn dng ch vic copy v chnh sa thi :D. trn mnh ni l s hin th ch gia ca s nhng tht ra l gia vng hin th ( vng mu trng ) Nhn chung th chng trnh trn vn c hm WinMain tng t nh topic 1a nhng c thm hm na l WndProc. Hm WndProc ny chnh l hm WINDOW PROCEDURE cp trn. CH l trong hm WinMain ta c dng wndclass.lpfnWndProc = WndProc ; nn cn khai bo tn hm trn hm WinMain. TRC TIN CHNG TA CN MT S NH NGHA SAU: The Windows Function Calls Chng trnh trn dng mt s hm Windows sau:

LoadIcon Loads an icon for use by a program. LoadCursor Loads a mouse cursor for use by a program.

www.huahongquan.com GetStockObject Obtains a graphic object, in this case a brush used for painting the window's background. RegisterClass Registers a window class for the program's window. MessageBox Displays a message box. CreateWindow Creates a window based on a window class. ShowWindow Shows the window on the screen. UpdateWindow Directs the window to paint itself. GetMessage Obtains a message from the message queue. TranslateMessage Translates some keyboard messages. DispatchMessage Sends a message to a window procedure. PlaySound Plays a sound file. BeginPaint Initiates the beginning of window painting. GetClientRect Obtains the dimensions of the window's client area. DrawText Displays a text string. EndPaint Ends window painting. PostQuitMessage Inserts a "quit" message into the message queue. DefWindowProc Performs default processing of messages. Chng trnh cng dng mt s bin hng nh: CS_HREDRAW CS_VREDRAW CW_USEDEFAULT DT_CENTER DT_SINGLELINE DT_VCENTER IDC_ARROW IDI_APPLICATION MB_ICONERROR SND_ASYNC SND_FILENAME WM_CREATE WM_DESTROY WM_PAINT WS_OVERLAPPEDWINDOW

Trong th cc ch u tng ng cho: Prefix CS CW DT IDI IDC MB SND WM WS Constant Class style option Create window option Draw text option ID number for an icon ID number for a cursor Message box options Sound option Window message Window style

New Data Types Bn cnh cc bin hng trn th cn mt s bin khc c nh ngha trong cc header.

www.huahongquan.com V d: UINIT -> unsigned int PSTR l mt con tr char * WPARAM, LPARAM th phc tp hn 1 cht. Trong W l WORD, hay 16-bit unsigned short integer. L l LONG 32bit signed long integer. Trong 32bit Win th WPARAM ng vi UINT cn LPARAM ng vi LONG. LRESULT ->LONG Bn cnh cn c: MSG WNDCLASS PAINTSTRUCT RECT Message structure Window class structure Paint structure Rectangle structure

Handles Identifier HINSTANCE HWND HDC Meaning Handle to an "instance"the program itself Handle to a window Handle to a device context

Mt Handle n gin l mt con s 32 bit ch ti mt i tng. Gi tr ca handle th khng quan trng i vi chng trnh ca chng ta nhng cn Windows bit cch tham chiu ti i tng. By gi chng ta cng xem qua phn code. Registering the Window Class Mi ca s window u c to da trn window class. V trc khi to ra mt ng dng window th ta cn phi REGISTER window class bng cch gi hm RegisterClass. RegisterClass (&wndclass) Hm ny ch cn mt con tr ti kiu d liu WNDCLASS. Kiu d liu ny c 2 phin bn l ASCII v UNICODE.
ASCII version, WNDCLASSA: typedef struct tagWNDCLASSA { UINT style ; WNDPROC lpfnWndProc ; int cbClsExtra ; int cbWndExtra ; HINSTANCE hInstance ;

www.huahongquan.com
HICON hIcon ; HCURSOR hCursor ; HBRUSH hbrBackground ; LPCSTR lpszMenuName ; LPCSTR lpszClassName ;

} WNDCLASSA, * PWNDCLASSA, NEAR * NPWNDCLASSA, FAR * LPWNDCLASSA ; The Unicode version : typedef struct tagWNDCLASSW { UINT style ; WNDPROC lpfnWndProc ; int cbClsExtra ; int cbWndExtra ; HINSTANCE hInstance ; HICON hIcon ; HCURSOR hCursor ; HBRUSH hbrBackground ; LPCWSTR lpszMenuName ; LPCWSTR lpszClassName ; } WNDCLASSW, * PWNDCLASSW, NEAR * NPWNDCLASSW, FAR * LPWNDCLASSW ;

n gin th ta xem WNDCLASS c nh ngha nh sau: typedef struct { UINT style ; WNDPROC lpfnWndProc ; int cbClsExtra ; int cbWndExtra ; HINSTANCE hInstance ; HICON hIcon ; HCURSOR hCursor ; HBRUSH hbrBackground ; LPCTSTR lpszMenuName ; LPCTSTR lpszClassName ; } WNDCLASS, * PWNDCLASS ; Nh vy, trong hm WinMain ta ch cn khai bo: WNDCLASS wndclass ;

Sau chng ta s khi gn gi tr cho 10 trng ca struct v gi hm RegisterClass. Trong 2 trng quan trng nht l lpfnWndProc v lpszClassName. lpfnWndProc: chnh l a ch ca WINDOW PROCEDURE m chng ta khai bo trn. lpszClassName: chnh l tn ca window class (window class name)

www.huahongquan.com By gi chng ta s xem xt cc trng cn li theo th t trn xung. Cc trng ny dng m t v cc c im ca window class. Cc bn mun c k hn th xem link sau: http://msdn.microsoft.com/en-us/library/aa925944.aspx wndclass.style m t kiu ca ca s cbClsExtra v cbWndExtra thit lp cc byte dnh ring. hInstance , hIcon , HCursor, hbrBackground l cc Handle lpszMenuName do Chng trnh trn khng c thanh Menu nn gi tr = NULL

Creating the Window Sau khi RegisterClass chng ta s to ca s window m chng ta cn hin th Hello, Windows. thc hin vic to ca s, ta gi hm CreateWindow vi cc i s nh sau: 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 Trong i s u tin( szAppName ), i s ny trng vi i s class name trong window class nn ca s ny s tng ng vi window class to trn. Ca s chng ta to ln vi style l WS_OVERLAPPEDWINDOW s c title bar trn, System Menu bn tri title bar v 3 nt minimize, maximize v close bn phi cng vi 1 border dy xung quanh ca s. Chng ta c th tm thy nh ngha ca style ny trong WINUSER.H nh sau: #define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED |\ WS_CAPTION |\ WS_SYSMENU |\ WS_THICKFRAME |\ WS_MINIMIZEBOX | \ WS_MAXIMIZEBOX) Cc i s ng vi initial x, y position v x, y size dng thit lp v tr ca ca s cng vi kch thc ca n. i s parent window handle c thit lp l NULL v trong chng trnh ch c 1 ca s v khng c cc ca s con lin quan khc. Nu c cc ca s con (child) th cc ca s con ny s hin ln trn ca cc ca s cha (parent). i s window menu handle l NULL v trong chng trnh ny chng ta khng c thanh MENU

www.huahongquan.com i s program instance handle c thit lp l gi tr hInstance c truyn vo trong hm WinMain i s creation parameters dng tr n d liu m ta mun truy xut trong chng trnh sau. Hin ti chng ta set l NULL Hm CreateWindow s tr v mt handle v chng ta lu vo bin hwnd. Bin hwnd c kiu d liu l HWND (handle to a window). Mi ca s c mt handle ring. Chng ta dng handle ny truy xut ti ca s mong mun.

Displaying the Window


Sauk hi ca s c to ra th chng ta cn phi hin ca s ln thong qua hm
ShowWindow (hwnd, iCmdShow) ;

Hm ny nhn 2 i s l handle ti ca s v tnh cht ca s (iCmdShow) nh normal, minimized hay maximized Hm UpdateWindow (hwnd) ; s gi lnh v (paint) li ca s

The Message Loop


Sauk hi ca s c hin ln th ng dng s lun chy vng lp nhn cc message t ngi dng hoc h thng v s gi hm WINDOW PROCEDURE tng ng
while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; }

The Window Procedure


Cc message cn c x l s c lp trnh y. Mt hm WINDOW PROCEDURE lun c dng sau:
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

Trong , CALLBACK ngha l hm ny s khng c gi trc tip trong hm WinMain m s c window gi. Trong chng trnh trn th chng ta x l 3 message l WM_CREATE, WM_PAINT v WM_DESTROY. Th cu trc ca switch s nh sau:
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) ;

Trong return DefWindowProc (hwnd, iMsg, wParam, lParam) ; l lun phi c

www.huahongquan.com MT CHNG TRNH KHC CC BN THAM KHO L: http://www.mediafire.com/?dwgvj05zvevof81

TRONG TOPIC TIP THO MINH S NOI V MSSAG WM_PAINT VA SCROLLBAR

You might also like