giao trinh lap trinh c for winform - smith.n studio
TRANSCRIPT
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
1/69
Mc lc
Trang
Bi 1: GII THIU CHUNG .................................................................. 21. Mu ......................................................................................................... 22. Cc th vin lp trnh ca Windows ............................................................. 33. Cc khi nim c bn ................................................................................... 44. Lp trnh s kin (Even driven programming)............................................... 55. Cc thnh phn giao din ha (GUI) ....................................................... 66. Cu trc chng trnh C for Win................................................................. 107. Qui trnh hot ng ca chng trnh ng dng ........................................ 108. Mt s quy c t tn............................................................................... 119. V d ........................................................................................................... 1110. Ti nguyn ca ng dng (Resources) ................................................... 1811. Mt s kiu d liu mi ........................................................................... 1912. Phn tch, tm hiu source code ca project ........................................... 19
Bi 2: PAINT V REPAINT................................................................. 241. Gii thiu .................................................................................................... 242. Tng quan v GDI (Graphics Device Interface) .......................................... 253. Mt s hm ha c s ........................................................................... 284. Kt lun....................................................................................................... 30
Bi 3: CC THIT B NHP LIU ...................................................... 31U
1. Bn phm .................................................................................................... 312. Thit b chut .............................................................................................. 383. Timer........................................................................................................... 41
Bi 4: HP THOI VIU KHIN ................................................... 451. Hp thoi..................................................................................................... 452. Menu........................................................................................................... 57
Bi 5: X L VN BN ...................................................................... 621. Hin th vn bn .......................................................................................... 622. nh dng vn bn...................................................................................... 643. S dng font ............................................................................................... 65
Ti liu tham kho ............................................................................... 69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
2/69
Bi 1:Gii thiu chung Trn Minh Thi
Bi 1: GII THIU CHUNG
Phn bthi lng:- Stit ginglp: 6 tit- Stit thc nh: 6 tit- Stit ci t chng trnh nh: 12 tit
1. Mu
Cc ng dng ca Windows rt d s dng, nhng rt kh i vi ngi
to lp ra chng. t c tnh d dng i hi ngi lp trnh phi b
ra rt nhiu cng sc ci t.
L p trnh trn Windows kh v phc tp hn nhiu so vi l p trnh trnDOS. Tuy nhin lp trnh trn Windows s gii quyt c mt s vn
kh khn trong mi trng DOS nh xy dng giao din ngi dng, qun
l b nho, c lp thit b vo ra, thm nhp Internet, kh nng chia s
ti nguyn, ...
Windows cung cp cc hm ngi lp trnh thm nhp cc c trng ca
hiu hnh gi l giao din lp trnh ng dng (Application Programming
Interface API). Nhng hm ny c t trong cc th vin lin kt ng
(Dynamic Link Library DLL). Cc chng trnh ng dng s dng chng
thng qua cc li gi hm v ch chia sc khi trong my c ci t
Windows.
Vi im khc bit gia lp trnh Windows v DOS:
Windows DOS
Lp trnh s kin, da vo thng ip
(message)Thc hin tun t theo chnh
Multi-tasking Single task
Multi-CPU Single CPU
Tch hp sn Multimedia Phi dng cc th vin Multimedia ring
H tr32 bits hay hn na ng dng 16 bits
H trnhiu cng ngh DLL, OLE,
DDE, COM, OpenGL, DirectX,
Khng c
Bi ging: Lp trnh C for Win ............................................................. .................................. Trang 2/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
3/69
Bi 1:Gii thiu chung Trn Minh Thi
2. Cc th vin lp trnh ca Windows
SDK Software Development Kit
L b th vin lp trnh nn tng ca HH Windows.
Cung cp tt c cc cng c cn thit xy dng 1 ng dng trn
Windows.
c s dng nh l th vin cs to ra nhng th vin cao cp hn
trong nhng ngn ng lp trnh. VD: OWL ca BorlandC, MFC ca Visual
C++,
Mt s thnh phn cbn ca SDK:
Win32 API. GDI/GDI+.
Windows Multimedia.
OpenGL.
DirectX.
COM/COM+.
ADO (ActiveX Data Object).
OLE DB.
(Xem thm MSDN/PlatForm SDK Documentation/Getting
started/Content of Platform SDK).
OWL Object Windows Library:
L b th vin hng i tng ca BorlandC++.
MFC Microsoft Foundation Classes:
L b th vin hng i tng ca Visual C++.
Mt ng dng trn Windows c thc vit bng:
Th vin SDK.
Mt th vin khc (OWL, MFC,) phi hp vi SDK.
Cc loing dng:
Win32 Console: ng dng 32 bits, vi giao din dng DOS command
line.
Bi ging: Lp trnh C for Win ............................................................. .................................. Trang 3/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
4/69
Bi 1:Gii thiu chung Trn Minh Thi
Win32 (SDK): ng dng 32 bits, ch s dng th vin SDK.
Win32 DLL: ng dng 32 bits, dng th vin lin kt ng (Dynamic
Linked Library), s dng SDK.
Win32 LIB: ng dng 32 bits, dng th vin lin kt tnh (Static
Linked Library).
MFC EXE: ng dng 32 bits, s dng th vin Microsoft Foundation
Class.
MFC DLL: ng dng 32 bits, dng th vin lin kt ng (Dynamic
Linked Library), s dng MFC.
3. Cc khi nim cbn
Message:
Trao i thng tin gia chng trnh ng dng v hiu hnh.
Th tc Window:
c gi bi h thng x l cc Message nhn c.
Hng i Message:Mi chng trnh c 1 hng i Message cha cc Message. Mi
chng trnh c vng lp Message.
Handle:
Mt gi tr 32 bits khng du (unsigned) do HH to ra lm nh danh
cho mt i tng (ca s, file, vng nh, menu,).
ID (Identifier):
Mt gi tr nguyn do ng dng to ra lm nh danh cho 1 i tng
(menu item, control).
Instance:
Mt gi tr nguyn do HH to ra nh danh 1 th hin ang thc thi ca
ng dng.
Callback:
Thuc tnh ca 1 hm/ th tc sc gi bi HH, khng phi bi ngdng.
Bi ging: Lp trnh C for Win ............................................................. .................................. Trang 4/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
5/69
Bi 1:Gii thiu chung Trn Minh Thi
4. Lp trnh s kin (Even driven programming)
USER.EXEMouseDriver
KeyboardDriver
Pht sinh cc s kin v thng ip
Qui trnh x l thng ip
SystemQueue
SystemQueue
ApplicationQueue
GetMessage()TranslateMessage()DispatchMessage()Device driverKeyboard
DefWindowProc()
WindowProc()
Virtual Key & Scan code
Bi ging: Lp trnh C for Win ............................................................. .................................. Trang 5/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
6/69
Bi 1:Gii thiu chung Trn Minh Thi
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
5. Cc thnh phn giao din ha (GUI)
GUI: Graphics User Interface.
Cc dng GUI cbn:
SDI Single Document Interface:
Mt ca s lm vic.
Cho php thay i kch thc ca s (Resizeable).
Khng c cc ca s con.
V d: NotePad, Paint,
MDI Multi Document Interface:
Mt ca s lm vic chnh (Frame window) v nhiu ca s con
(Child window).
Bi ging: Lp trnh C for Win ............................................................. .................................. Trang 6/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
7/69
Bi 1:Gii thiu chung Trn Minh Thi
Cho php thay i kch thc ca s (Resizeable).
Cho php Maximize/Minimize/Close cc ca s con.
V d: Word, Excel, VC++,
Dialog:
Mt ca s lm vic.
Thng c kch thc cnh.
Thng khng c menu bar.
Thng c cc button, edit box, list-box,
V d: Calculator, CD Player,
Bi ging: Lp trnh C for Win ............................................................. .................................. Trang 7/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
8/69
Bi 1:Gii thiu chung Trn Minh Thi
Ca s:
nh ngha:
L 1 vng ch nht trn mn hnh.
Dng hin th kt qu output.
V nhn cc input t ngi dng
Cng vic u tin ca 1 ng dng GUI l to 1 ca s lm vic.
Nguyn tc qun l:
M hnh phn cp: mi ca su c 1 ca s cha (parent
window), ngoi tr ca s nn Desktop.
Ti mi thi im, ch c 1 ca s nhn input t user
(Active/Focused window).
Phn loi:
Ca s Desktop.
Ca s tiu chun.
Ca s hp thoi (Dialog box).
Cc control.DesktopWindow
AppWindow
Parent
Window
ChildWindow
Control
Dialogbox
Bi ging: Lp trnh C for Win ............................................................. .................................. Trang 8/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
9/69
Bi 1:Gii thiu chung Trn Minh Thi
Window iconWindow caption
Title barMinimized,Restored,
Maximized button
menu bar
scroll bar
Resizedborder
status bar
Client area
tool barstandard bar
Bi ging: Lp trnh C for Win ............................................................. .................................. Trang 9/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
10/69
Bi 1:Gii thiu chung Trn Minh Thi
6. Cu trc chng trnh C for Win
7. Qui trnh hot ng ca chng trnh ng dng
Ca sc hin th ln mn hnh.
Windows chca s gi thng ip.
Cc thng ip c Windows gi tr li chng trnh ng dng thng qua
li gi hm ca chng trong chng trnh ng dng.
Khi nhn c thng ip, chng trnh ng dng gi cc hm API v hm
ca ring chng thc hin cng vic mong mun.
*.CPP file
Compiler
*.OBJ file
Linker
Definition file(*.DEF)
Resource file(*.RES)
ResourceCompiler
Window Application(*.EXE, *.DLL)
Library file(*.LIB)
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 10/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
11/69
Bi 1:Gii thiu chung Trn Minh Thi
Lp trnh trn Windows l lp trnh trn c s thng ip, qu trnh trao i
thng tin v iu khin da trn thng ip. C rt nhiu thng ip c pht
sinh ngu nhin nh nhn phm hay chut, chn menu, ...
Tng tc ca ng dng vi ngi s dng thng qua mt hay nhiu ca s,to lp cc ca s khi cn thit v qun l thng tin trong .
8. Mt s quy c t tn
a. Tn hng
Ch ci vit hoa, nn phn loi cc hng theo nhm. Thng thng gm c
2 phn: Phn u l loi nhm v phn sau l tn hng. Loi nhm v tn
hng cch nhau bng du gch ni.
V d: WM_DESTROY (Hng ny c nh ngha trong windows.h, WM
cho ta bit hng DESTROY thuc nhm thng ip ca s Windows
Message)
b. Tn bin
Tn bin bt u bng k t thng cho bit kiu d liu.
V d: iTong cho bit bin Tong c kiu int.
Cc tin t thng dng khc: c(char), l (long), p (poiter), d (WORD), dw
(DWORD), h (ch s).
9. V d
Xy dng chng trnh hin th mt ca s nh sau:
#include
Lap trinh C for Win
Vi du mo dau
1
23 LRESULT CALLBACK XulyMessage (HWND,UNIT,WPARAM,LPARAM);char szAppName [ ] = Vidu;
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 11/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
12/69
Bi 1:Gii thiu chung Trn Minh Thi
int WINAPI WinMain (HANDLE hInst, HANDLE hPrevInst,456789
10111213141516171819202122232425262728293031323334353637
38394041424344454647
4849
LPSTR lpszCmdLine, int nCmdShow){
HWND hwnd; MSG msg;WNDCLASSEX wndclass;wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;wndclass.lpfnWndProc = XulyMessage;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hInstance = hInst;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;wndclass.lpszClassName = szAppName;wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd = CreateWindow(szAppName,Vi du mo dau,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,HWND_DESKTOP,NULL,
hInst,NULL);
ShowWindow (hwnd, nCmdShow);UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0)){
TranslateMessage (&msg);
DispatchMessage (&msg);}
return msg.wParam;}LRESULT CALLBACK XulyMessage (HWND hwnd, UINT iMsg,
WPARAM wParam, LPARAM lParam){
HDC hdc;
PAINTSTRUCT ps;RECT rect;switch (iMsg){
case WM_PAINT:hdc = BeginPaint (hwnd, &ps);
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 12/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
13/69
Bi 1:Gii thiu chung Trn Minh Thi
GetClientRect (hwnd, &rect);50515253545556575859606162
DrawText (hdc, Lap trinh C for Win, -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint (hwnd, &ps);break;
case WM_DESTROY:
PostQuitMessage(0);break;
default:return DefWindowProc (hwnd, iMsg, wParam, lParam);
}
return 0;}Ta s kho st v d trn nm c nguyn l hot ng ca chng. Trn
y l on chng trnh n gin trn Windows, chng trnh ch hin th 1khung ca s v 1 dng ch nhng c rt nhiu lnh m c php rt kh nh.
Do vy, nguyn tc lp trnh trn Windows ch yu l sao chp v chnh sa
nhng ni cn thit da vo mt chng trnh mu c sn.
a. Hm WinMain() c thc hin u tin hay cn gi l im vo ca
chng trnh.
Ta thy hm ny c 4 tham s:
o hInst, hPrevinst: Ch s chng trnh khi chng ang chy. V
Windows l hiu hnh a nhim, c th c nhiu bn ca cng
mt chng trnh cng chy vo cng mt thi im nn phi qun
l cht ch chng. hInst l ch s bn chng trnh va khi ng,
hPrevinst l ch s ca bn c khi ng trc v chng
lun c gi tr NULL.
o
lpszCmdLine: cha a chu ca xu k t cc i s dng lnh.o nCmdShow: Cho bit cch thc hin th ca s khi chng trnh
khi ng. Windows c th gn gi tr SW_SHOWNORMAL hay
SW_SHOWMINNOACTIVE.
Cc tham s trn do hiu hnh truyn vo.
nh ngha lp ca s v ng k vi Windows
o Lp ca s (window class):
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 13/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
14/69
Bi 1:Gii thiu chung Trn Minh Thi
L mt tp cc thuc tnh m HH Windows s dng lm khun
mu (template) khi to lp ca s.
Mi lp ca s c c trng bng 1 tn (class-name) dng
chui. Phn loi class:
- Lp ca s ca h thng (System class):
c nh ngha trc bi HH Windows.
Cc ng dng khng th hy b.
Class Description
Button The class for a button
ComboBox The class for a combo box
Edit The class for an edit control
ListBox The class for a list box
MDIClientThe class for a MDI client
window
ScrollBar The class for a scroll bar
Static The class for a static control- Lp ca s do ng dng nh ngha:
c ng k bi ng dng.
C th hy b khi khng cn s dng na.
Lp ton cc ca ng dng (Application global class).
Lp cc b ca ng dng (Application local class).
- Mi ca su thuc mt lp xc nh.
o Khi ln u chy, ng dng phi nh ngha v ng k lp vi ca
s (Window Class). y l cu trc d liu m t tnh cht ca ca
s, ln lt ta gn cc gi tr ban u cho cc thnh phn ca cu
trc lp ca s, bao gm: Kch thc, kiu, a ch hm x l thng
ip ca s, nh ngha hnh dng cho con tr chut (cursor) v biu
tng (Icon), mu nn, tn lp ca s.
Macro Mu nn ca s
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 14/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
15/69
Bi 1:Gii thiu chung Trn Minh Thi
BLACK_BRUSH en
DKGRAY_BRUSH Xm en
HOLLOW_BRUSH Khng t
LTGRAY_BRUSH Xm nht
WHITE_BRUSH Trng
struct WNDCLASSEX {
UINT cbSize;
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;LPCTSTR lpszClassName;
HICON hIconSm;
};
o Sau khi nh ngha cc thnh phn lp ca s ta phi ng k lp
ca s vi hiu hnh (RegisterClassEX).
ATOM RegisterClassEx (CONST WNDCLASSEX *lpWClass);
vi: Kiu gi tr ca ATOM c nh ngha trong window.h l
WORD; lpWClass l con trn cu trc lp ca s; hm ny tr v
ch s ca lp ca s.
o C hai nguyn nhn dn n vic ng k ca s tht bi:
Trng tn gia cc ng dng trong hiu hnh.
Khng b nh.
To lp ca s lm vic (Frame Window)
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 15/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
16/69
Bi 1:Gii thiu chung Trn Minh Thi
o Sau khi ng k thnh cng ta c th to lp ca s thng qua hm
CreateWindow().
HWND CreateWindow (
LPCSTR lpClassName,
LPCSTR lpWinName,
DWORD dwStyle,
int X, int Y,
int Width, int Height,
HWND hParent,
HMENU hMenu,
HINSTANCE hInst,
LPVOID lpszAdditional);
Kiu M t
WS_MAXIMIZEBOX Ca s c phm dn to trn thanh tiu
WS_MINIMIZEBOX Ca s c phm co nh trn thanh tiu
WS_OVERLAPPED Ca s maximize v khng c ca s cha
WS_SYSMENU Ca s c hp thc n h thngWS_VSCROLL Ca s c thanh trt dc
WS_HSCROLL Ca s c thanh trt ngang
o Gi hm ShowWindow() hin th ca s
BOOL ShowWindow (HWND hwnd, int nShow);
vi: hwnd ch s ca s cn hin th.
nShow cch thc hin th ca ca s, tham s ny c nhn gi tr
ln u tin ca hm WinMain(), chng c th nhn cc gi tr sau:
Macro Cch thc hin th
SW_HIDE Du ca s
SW_MINIMIZE Thu nh ca s
SW_MAXIMIZE Phng to ca s ton mn hnh
SW_RESTORE Trli kch thc thng thng
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 16/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
17/69
Bi 1:Gii thiu chung Trn Minh Thi
o thng bo cho ng dng bit l phi v li vng lm vic ca ca
s, ta phi gi hm UpdateWindow() yu cu Windows gi thng
ip n hm x l thng ip ca s.
Vng lp thng ipo Khi nhn phm hay chut, Windows chuyn i s kin ny thnh
cc thng ip v t vo hng i thng ip. Vng lp thng ip
c nhim v nhn v x l cc thng ip trong hng i.
o TranslateMessage: Dch thng ip sang dng tiu chun.
o DispatchMessage: Phn phi thng ip n hm x l thng ip
tng ng.
b. Th tc x l thng ip
Nhn v x l thng ip ca chng trnh.
Mt chng trnh c th c nhiu th tc window.
Mt lp ca s s khai bo 1 th tc window.
Cc thng ip sau khi x l nn tr v gi tr 0.
Dng tng qut:
LRESULT CALLBACK WndProc(
HWND hWnd, //handle ca window nhn messageUINT message, //ID ca thng ip (tn thng ip)WPARAM wParam, //thams th nht ca message (WORD)LPARAM lParam) //thams th hai ca message (LONG)
{
switch (message)
{
case WM_COMMAND:
return 0;
case WM_PAINT:return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam,
lParam);
}
}
Thng ip WM_PAINT:Cp nht li thng tin v trn mn hnh.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 17/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
18/69
Bi 1:Gii thiu chung Trn Minh Thi
Cc trng thi xut hin thng ip WM_PAINT:
i. To ca s Hin th Cp nht
CreateWindow ShowWindow UpdateWindow
ii. Xut hin hp thoi (Dialog box), thng bo (Message box) lm chemt phn hoc ton b ca s, khi cc hp thoi ny ng i th phi
gi WM_PAINT v li ca s.
iii. Khi thay i kch thc ca s
WS_HREDRAW | WS_VREDRAW
iv. Ca sang minimize maximize
HDC: (Handle to a device context) chn 1 ng cnh thit b gm thit
b phn cng v trnh iu khin thit b.
BeginPaint: Ly ng cnh thit b.
EndPaint: Gii phng ng cnh thit b.
Thng ip WM_DESTROY:
Xut hin khi ngi dng chn nt close trn ca s hoc nhn Alt+F4.
Nhim v PostQuitMessage t thng ip WM_QUIT vo hng i.
10. Ti nguyn ca ng dng (Resources)
L 1 i tng (object) c s dng trong ng dng (VD: menu bar,
dialog, bitmap, icon, cursor, ).
c nh ngha bn ngoi v c thm vo trong file thi hnh ca
ng dng khi bin dch (linking).
Cc dng resource:
Accelerator Bng m t phm tt (hot-key).Bitmap nh bitmap.
Caret Con tr vn bn.
Cusor Con tr chut.
Dialog box Khung hp thoi.
Enhance metafileTp h p cc cu trc lu nh (picture) theo nh
dng c lp thit b (Device-Independent format).
Font Font ch.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 18/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
19/69
Bi 1:Gii thiu chung Trn Minh Thi
Icon Biu tng.
Menu Menu.
String-table entry Bng m t cc chui k t.
Version information Bng m t thng tin phin bn.
11. Mt s kiu d liu mi
Stt Kiu d liu Ch thch
1 HANDLE
(nguyn khng du 16 bit) dng nh danh i tng
HWND : window
HMENU : menu
HCURSOR : cursor
2 HBRUSH (brush) mu t: solid, dash, dot, cross,
3 HPALLETE (pallete) bng mu
4 HFONT (font) Facename, size, style
5 HBITMAP bitmap
6 HICON icon
7 HPEN Nt v: solid, dot, dash, size, color8 HINSTANCE Instance
9 HDC Device context
10 LTSTR (long pointer string) con trn chui k t
11WPARAM
LPARAM(word) cc tham si km message.
12 LRESULT (long) kiu tr v ca hm x l Message.
13 LPVOID Con trn kiu d liu bt k.
12. Phn tch, tm hiu source code ca project
// bt1.cpp : Defines the entry point for the application.12345
67
#include "stdafx.h"
#include "resource.h"#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instanceTCHAR szTitle[MAX_LOADSTRING]; // The title bar text
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 19/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
20/69
Bi 1:Gii thiu chung Trn Minh Thi
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text89
1011121314151617181920212223242526272829303132333435363738394041
42434445464748495051
5253
// Foward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{// TODO: Place code here.MSG msg;
HACCEL hAccelTable;// Initialize global stringsLoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_BT1, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance);
// Perform application initialization:if (!InitInstance (hInstance, nCmdShow))
{return FALSE;
}hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BT1);
// Main message loop:while (GetMessage(&msg, NULL, 0, 0)){
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){
TranslateMessage(&msg);DispatchMessage(&msg);
}}return msg.wParam;
} // FUNCTION: MyRegisterClass()
// PURPOSE: Registers the window class. // COMMENTS:// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'// function that was added to Windows 95. It is important to call this function// so that the application will get 'well formed' small icons associated
// with it.ATOM MyRegisterClass(HINSTANCE hInstance){
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_HREDRAW | CS_VREDRAW;
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 20/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
21/69
Bi 1:Gii thiu chung Trn Minh Thi
wcex.lpfnWndProc = (WNDPROC)WndProc;54555657585960616263646566676869707172737475767778798081828384858687
88899091929394959697
98
wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BT1);wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = (LPCSTR)IDC_BT1;wcex.lpszClassName = szWindowClass;wcex.hIconSm = LoadIcon(wcex.hInstance,(LPCTSTR)IDI_SMALL);return RegisterClassEx(&wcex);
} // FUNCTION: InitInstance(HANDLE, int)// PURPOSE: Saves instance handle and creates main window
// COMMENTS: // In this function, we save the instance handle in a global variable and // create and display the main program window.BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{HWND hWnd;hInst = hInstance; // Store instance handle in our global variablehWnd = CreateWindow(szWindowClass,
szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
0,CW_USEDEFAULT,0,
NULL,
NULL,hInstance,
NULL);
if (!hWnd){
return FALSE;}ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);return TRUE;
}
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)// PURPOSE: Processes messages for the main window.// WM_COMMAND - process the application menu // WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 21/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
22/69
Bi 1:Gii thiu chung Trn Minh Thi
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAMwParam, LPARAM lParam)
99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
{
int wmId, wmEvent,x,y;PAINTSTRUCT ps;HDC hdc;
TCHAR szHello[MAX_LOADSTRING];LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);switch (message){
case WM_COMMAND:
wmId = LOWORD(wParam);wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId){
case IDM_ABOUT:DialogBox(hInst,(LPCTSTR)IDD_ABOUTBOX,hWnd, (DLGPROC)About);
break;
case IDM_EXIT:DestroyWindow(hWnd);break;
default:return DefWindowProc(hWnd, message,wParam, lParam);
}
break;case WM_LBUTTONDOWN:
hdc = GetDC(hWnd);// TODO: Add any drawing code here...
x=LOWORD(lParam);y=HIWORD(lParam);TextOut(hdc,x,y,(LPCTSTR)szHello, strlen(szHello));
break;case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...
RECT rt;GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);break;
case WM_DESTROY: PostQuitMessage(0); break;
default:
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 22/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
23/69
Bi 1:Gii thiu chung Trn Minh Thi
return DefWindowProc(hWnd, message, wParam, lParam);145146147148149150151152153154155156157158159160161162163164165
}return 0;
}// Mesage handler for about box.LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam,
LPARAM lParam){
switch (message){
case WM_INITDIALOG: return TRUE;
case WM_COMMAND:if (LOWORD(wParam)== IDOK||LOWORD(wParam)==
IDCANCEL)
{EndDialog(hDlg, LOWORD(wParam));return TRUE;
}break;
}return FALSE;
}
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 23/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
24/69
Bi 2:Paint v repaint Trn Minh Thi
Bi 2: PAINT V REPAINT
Phn bthi lng:- Stit ginglp: 6 tit- Stit thc nh: 6 tit- Stit ci t chng trnh nh: 12 tit
1. Gii thiu
Windows khng gi li nhng g chng hin th trn vng lm vic ca ca s,
cho nn chng trnh ng dng phi hin th ni dung ca s khi cn thit. V li
ni dung ca s khi:
Dng hm ScrollWindow: D liu hin th thay i cp nht li. Hm InvalidateRect: Lm bt hp l 1 phn hay ton b vng lm vic.
Menu chng trnh bt xung lm che khut mt phn ca s.
Di chuyn chut, di chuyn icon.
Vng hnh ch nht hp l v bt hp l thng qua li gi hm
BOOL InvalidateRect(HWND hwnd, CONST RECT *lpRect, BOOL bErase);
vi: bErase = TRUE th t li nn, FALSE th gi nguyn.
Hm BeginPaint() s lm hp l li vng bt hp l.
Trong ca s cha PAINTSTRUCT, mc ch l s t hp li 2 hay nhiu
vng bt hp l chng ln nhau.
typedef struct tagPAINTSTRUCT{
HDC hdc;
BOOL fErase;RECT rcPaint;
BOOL fRestore;BOOL flncUpdate;BYTE rgReserved[32];
}PAINTSTRUCT;
Vi: fErase = FALSE: Khng xo m ghi ch
rcPaintcha to vng bt hp l.
TRUE: xo vng hnh ch nht bt hp l.ng ln.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 24/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
25/69
Bi 2:Paint v repaint Trn Minh Thi
typedef tagRECT{
LONG left, top;LONG right, bottom;
}RECT;
2. Tng quan v GDI (Graphics Device Interface)
ng dngWindows
Ng cnhthit b GDI
Trnh iukhin thit b
Thixu
t bt
a) Lm vic vi ng cnh thit b
hdc cha cc thng tin nn cn thit cho vic v ln mn hnh, tng
giao tip vi phn cng.
C nhiu cch nhn v gii phng hdc.
o BeginPaint() v EndPaint() : Cp hm ny ch yu c dng trong
phn WM_PAINT.
HDC BeginPaint(HWND hwnd, LPPAINTSTRUCT lpPS);
BOOL EndPaint(HWND hWnd, CONST PAINTSTRUCT *lpPaint);
o GetDC() v ReleaseDC() : Khng lm hp l bt c vng bt hp lno.
HDC GetDC(HWND hwnd);
int ReleaseDC(HWND hwnd, HDC hdc);
tr vTRUE nu gii phngc hdc.
Vic ly v gii phng hdc ch nn c tin hnh bn trong phn x l
1 message.
Ngoi ra, cn c th nhn v device context ca ton mn hnh bng
hm: hDC = CreateDC( "DISPLAY", NULL, NULL, NULL);
ly to v kch thc ca ca s lm vic ta dng hmBOOL GetClientRect(HWND hWnd, LPRECT lpRect);
tr vgi tr khc khng nu thnh cng, ngc li tr v0. Hin th s ln mn hnh
wsprintf(s, %d + %d= %d, a, b, a+b);
TextOut(hdc, x, y, s, wsprintf());
b) Ch nh x
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 25/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
26/69
Bi 2:Paint v repaint Trn Minh Thi
V tr hin th k t TextOut() l ta tng i trong ca s (ta
logic).
Windows s nh xn v ny thnh pixel khi hin th k t.
ch mc nh ta logic pixel.c) M hnh mu RGB (Red Green Blue)
Byte 3 Byte 2 Byte 1 Byte 0
0 Blue Green Red
C gi tr t 0 255
(0, 0, 0) en (255, 255, 255) trng
Cc hm API lin quan n mu u s dng m hnh RGB.
nh ngha mu COLORREF RGB (int red, int green, int blue).
V d 1 : V hnh ch nht
HDC hDC;HPEN hPen, oldHPen;
hDC=GetDC(hWnd);hPen=CreatePen(PS_SOLID, 5, RGB(0, 0, 255));oldHPen=(HPEN)SelectObject(hDC, hPen);
Rectangle(hDC, 20, 20, 100, 100);
SelectObject(hDC, oldHPen);DeleteObject(hPen);ReleaseDC(hWnd, hDC);
d) To lp v gii phng memory device context
Memory device context (MDC) l mt device context o khng gn vi
mt thit b xut c th no. Mun kt qu kt xut ra thit b vt l ta
phi chp MDC ln mt device context tht s(device context c lin
kt vi thit b vt l). MDC thng c dng nh mt device context
trung gian v trc khi thc s xut ra thit b, nhm gim s chp
git nu thit b xut l window hay mn hnh.
to MDC tng thch vi mt hDC c th, s dng hm
CreateCompatibleDC:
HDC hMemDC;
hMemDC = CreateCompatibleDC(hDC);
n gin hn, c tht NULL vo v tr hDC, Windows s to mt
device context tng thch vi mn hnh.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 26/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
27/69
Bi 2:Paint v repaint Trn Minh Thi
Hy MDC bng hm DeleteDC.
MDC c b mt hin th nh mt thit b tht. Tuy nhin, b mt hin
th ny lc u rt nh, ch l mt pixel n sc. Khng th lm g vi
mt b mt hin th ch gm 1 bit nh vy. Do cn lm cho b mthin th ny rng hn bng cch chn mt i tng bitmap GDI vo
MDC:
SelectObject(hMemDC, hBitmap);
Ch c th chn i tng bitmap vo MDC, khng th chn vo mt
device context c thc.
Sau khi chn mt i tng bitmap cho MDC, c th dng MDC nh
mt device context tht s.
Sau khi c hon tt trong MDC, nh c a ra device context tht
s bng hm BitBlt:
BitBlt(hDC, xDest, yDest, nWidth, nHeight, hMemDC, xSource,
ySource);
V d : Chun bnh trc khi a ra mn hnh, trnh gy chp mn
hnh trong thng ip WM_PAINT.
case WM_PAINT:hdc = BeginPaint(hWnd, &ps);
// Ly vkch thc vng client ca ca shin hnhRECT rect;GetClientRect(hWnd, &rect);
// To MDC tng thch vi DC ca ca sHDC hMemDC;hMemDC = CreateCompatibleDC(hdc);
// Chn mti tng bitmap mrng vng hin th cho MDC
HBITMAP bitmap,oBitmap;bitmap = CreateCompatibleBitmap(hdc, rect.right, rect.bottom);oBitmap = (HBITMAP)SelectObject(hMemDC, bitmap);
// Vli nn MDCFillRect(hMemDC, &rect, HBRUSH (GetBkColor(hMemDC)));
// Xut hnh nh, text ra MDCSetPixel(hMemDC, 0, 0, RGB(255,0,0));MoveToEx(hMemDC, 50, 50, NULL);LineTo(hMemDC, 100, 100);Rectangle(hMemDC, 10, 10, 100, 100);
TextOut(hMemDC, 15 ,15, "Testing MDC", 11);
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 27/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
28/69
Bi 2:Paint v repaint Trn Minh Thi
If(!BitBlt(hdc, 0, 0, rect.right, rect.bottom, hMemDC, 0, 0,SRCCOPY))
MessageBox(hWnd, "Failed to transfer bit block", "Error",MB_OK);// Phc hi li bitmap c cho MDCSelectObject(hMemDC, oBitmap);
// Gii phng MDC, bitmap toDeleteDC(hMemDC);DeleteObject(bitmap);EndPaint(hWnd, &ps);break;
3. Mt s hm ha cs
a)Nhm hm v
COLORREF GetPixel(HDC hDC, int nXPos, int nYPos);Ly v gi tr mu ti v tr (nXPos, nYPos) ca hDC, tr v -1 nu im
ny nm ngoi vng hin th.
COLORREF SetPixel(HDC hDC, int nXPos, int nYPos,
COLORREF clrRef);
V mt im mu clrRef ti v tr (nXPos, nYPos) ln hDC. Gi tr tr
v l mu ca im (nXPos, nYPos) hoc -1 nu im ny nm ngoi
vng hin th.
DWORD MoveToEx(HDC hDC, int x, int y);
Di chuyn bt vn ta (x, y) trn hDC. Gi tr tr v l ta c
ca bt v, x = LOWORD, y = HIWORD.
BOOL LineTo(HDC hDC, int xEnd, int yEnd);
V on thng t v tr hin hnh n v tr (xEnd, yEnd) trn hDC.
Hm tr v TRUE nu thnh cng, FALSE nu tht bi.
BOOL Polyline(HDC hDC, const POINT FAR *lpPoints, int
nPoints);
Vng gp khc ln hDC bng cc on thng lin tip, snh lnPoints vi ta cc nh c xc nh trong lpPoints. Hm tr vTRUE nu thnh cng, FALSE nu tht bi.
BOOL Polygon(HDC hDC, const POINT FAR *lpPoints, int
nPoints);
Va gic c nPoints nh, ta cc nh c xc nh bi lpPoints.Hm tr v TRUE nu thnh cng, FALSE nu tht bi.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 28/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
29/69
Bi 2:Paint v repaint Trn Minh Thi
BOOL Rectangle(HDC hDC, int left, int top, int right, intbottom);V hnh ch nht c ta l left, top, right, bottom ln hDC.
HPEN CreatePen(int penStyle, int penWidth, COLORREFpenColor);
To bt v c kiu penStyle, dy nt v l penWidth, mu penColor.Hm tr v handle ca bt v nu thnh cng v tr v NULL nu tht
bi. Cc gi tr ca penStyle nh sau :
Gi tr Gii thch
PS_SOLID
PS_DASH
PS_DOT
PS_DASHDOT
PS_DASHDOTDOT
PS_NULL Khng hin th
PS_INSIDEFRAME
Cc kiu bt v penStyleV d : To bt v mi v dng bt v ny v mt sng cs.
HDC hDC;
POINT PointArr[3];HPEN hPen, hOldPen;hDC = GetDC(hWnd);PointArr[0].x = 50;
PointArr[0].y = 10;
PointArr[1].x = 250;
PointArr[1].y = 50;
PointArr[2].x = 125;
PointArr[2].y = 130;
Polyline(hDC, PointArr, 3);
hPen = (HPEN)CreatePen(PS_SOLID, 1, RGB(0, 0, 255));hOldPen = SelectObject(hDC, hPen);MoveToEx(hDC, 100, 100, NULL);LineTo(hDC, 200, 150);SelectObject(hDC, hOldPen);DeleteObject(hPen);ReleaseDC(hWnd, hDC);
b)Nhm hm min
HBRUSH CreateSolidBrush(COLORREF cRef);
To mu t c vi mu cRef.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 29/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
30/69
Bi 2:Paint v repaint Trn Minh Thi
HBRUSH CreateHatchBrush(intbStyle, COLORREF cRef);
To mu t dng li kiu bStyle vi mu cRef.Cc kiu bStyle :HS_HORIZONTAL
HS_VERTICALHS_FDIAGONAL
HS_BDIAGONAL
HS_CROSSHS_DIAGCROSS
BOOL FloodFill(HDC hDC, int xStart, int yStart, COLORREF
cRef);
T mu mt vng kn, mu ng bin l cRef. BOOL ExtFloodFill(HDC hDC, int xStart, int yStart, COLORREF
cRef, UINT fillStyle);T mu mt vng kn, fillStyle quytnh cch t :o FLOODFILLBORDER : T mu vng c mu ng bin l cRef.
o FLOODFILLSURFACE : T vng c mu cRef.V d : S dng cc mu c sn v to cc mu t mi t.
HDC hDC;HPEN hPen;HBRUSH hBrush, hOldBrush;hDC = GetDC(hWnd);
//Vhai hnh chnht vi bt vBlackhPen = (HPEN)GetStockObject(BLACK_PEN);SelectObject(hDC, hPen);Rectangle(hDC, 10, 10, 50, 50);
Rectangle(hDC, 100, 100, 200, 200);// Dng mt trong cc mu t c sn t hnhhBrush = (HBRUSH)GetStockObject(GRAY_BRUSH);SelectObject(hDC, hBrush);FloodFill(hDC, 30, 30, RGB(0,0,255));
// To mu t mi t hnh thhaihBrush = (HBRUSH)CreateHatchBrush(HS_DIAGCROSS, RGB(0,255, 255));
hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);FloodFill(hDC, 150, 150, RGB(0, 0, 0));
SelectObject(hDC, hOldBrush);//Xa mu t v gii phng hDCDeleteObject(hBrush);ReleaseDC(hWnd, hDC);
4. Kt lun
WM_PAINT l message c u tin thp. Khi WM_PAINT trong hng chv
c mt s Window Message khc th Windows x l WM khc ri mi x l
WM_PAINT.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 30/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
31/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
Bi 3: CC THIT B NHP LIU
Phn bthi lng:- Stit ginglp: 15 tit- Stit thc nh: 15 tit- Stit ci t chng trnh nh: 30 tit
1. Bn phm
a. Chng trnh iu khin bn phm (Keyboard.drv)
Windows c np Keyboard.drv khi khi ng v x l phm. Sau
keyboard.drv chuyn cho USER bin phm nhn thnh message v a
vo hng i (Hng i h thng v hng i chng trnh).b. Ca s c focus
Khi ca s c focus th pht sinh thng ip WM_SETFOCUS.
Ngc li pht sinh WM_KILLFOCUS.
c. Thng ip phm
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Thng i p Nguyn nhn pht sinh
WM_ACTIVATE Thng ip ny cng c gi n cc ca s bkch hot v ca s khng b kch hot. Nu ccca s ny cng mt hng i nh p liu, ccthng i p ny s c truyn mt cch ng
b, u tin th tc Windows ca ca s trncng b mt kch hot, sau n th tc caca s trn cng c kch hot. Nu cc ca sny khng nm trong cng mt hng i ththng ip sc gi mt cch khng ng b,do ca s sc kch hot ngay lp tc.
WM_APPCOMMAND Thng bo n ca s rng ngi dng tomt s kin lnh ng dng, v d khi ngi dngkch vo button s dng chut hay nh vo mt
k t kch hot mt lnh ca ng dng.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 31/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
32/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
WM_CHAR Thng ip ny c gi ti ca s c s quantm khi thng i p WM_KEYDOWN cdch t hm TranslateMessage. Thng ipWM_CHAR c cha m k t ca phm c
nhn.
WM_DEADCHAR Thng ip ny c gi ti ca s c s quantm khi thng ip WM_KEYUP c x lt hm TranslateMessage. Thng i p ny xcnhn m k t khi mt phm dead key cnhn. Phm dead key l phm kt hp to ra kt ngn ng khng c trong ting anh (xut hintrong bn phm h tr ngn ng khc tingAnh).
WM_GETHOTKEY ng dng gi thng i p ny xc nh mt phm nng lin quan n mt ca s. githng ip ny th dng hm SendMessage.
WM_HOTKEY Thng i p ny c gi khi ngi dng nhnmt phm nng c ng k trongRegisterHotKey.
WM_KEYDOWN Thng ip ny c gi cho ca s nhn cs quan tm khi ngi dng nhn mt phm trn
bn phm. Phm ny khng phi phm h thng(Phm khng c nhn phm Alt).
WM_KEYUP
Thng ip ny c gi cho ca s nhn cs quan tm khi ngi dng nh mt phm c nhn trc .Phm ny khng phi phmh thng (Phm khng c nhn phm Alt).
WM_KILLFOCUS Thng i p ny c gi ti ca sang nhnc s quan tm trc khi n mt quyn ny.
WM_SETFOCUS Thng ip ny c gi ti ca s sau khi cas nhn c s quan tm ca Windows
WM_SETHOTKEY ng dng s gi thng ip ny n ca s linquan n phm nng, khi ngi dng nhn mt
phm nng th ca s tng ng lin quan tiphm nng ny sc kch hot.
WM_SYSCHAR Thng i p ny s c gi ti ca s nhnc s quan tm khi hm TranslateMesage xl xong thng i p WM_SYSKEYDOWN.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 32/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
33/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
Thng ip WM_SYSCHAR cha m ca phmh thng. Phm h thng l phm c cha phmAlt v t hp phm khc.
WM_SYSDEADCHAR Thng ip ny c gi ti ca s nhn cs quan tm khi mt thng ipWM_SYSKEYDOWN c bin dch tronghm TranslateMessage. Thng i p ny xcnhn m k t ca phm h thng deadkey cnhn.
WM_SYSKEYDOWN Thng ip ny c gi ti ca s nhn cs quan tm khi ngi dng nhn phm hthng.
d. V d#define BUFSIZE 655351
23456789
101112131415161718
19202122232425262728
2930
#define SHIFTED 0x8000
LONG APIENTRY MainWndProc(HWND hwndMain, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
HDC hdc; // handle to device context
TEXTMETRIC tm; // structure for text metrics
static DWORD dwCharX; // average width of characters
static DWORD dwCharY; // height of characters
static DWORD dwClientX; // width of client area
static DWORD dwClientY; // height of client area
static DWORD dwLineLen; // line length
static DWORD dwLines; // text lines in client area
static int nCaretPosX = 0; // horizontal position of caret
static int nCaretPosY = 0; // vertical position of caret
static int nCharWidth = 0; // width of a character
static int cch = 0; // characters in buffer
static int nCurChar = 0; // index of current characterstatic PTCHAR pchInputBuf; // input buffer
int i, j; // loop counters
int cCR = 0; // count of carriage returns
int nCRIndex = 0; // index of last carriage return
int nVirtKey; // virtual-key code
TCHAR szBuf[128]; // temporary buffer
TCHAR ch; // current character
PAINTSTRUCT ps; // required by BeginPaint
RECT rc; // output rectangle for DrawText
SIZE sz; // string dimensionsCOLORREF crPrevText; // previous text color
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 33/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
34/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
COLORREF crPrevBk; // previous background color31323334353637383940414243444546474849505152535455565758596061626364
65666768697071727374
7576
switch (uMsg)
{
case WM_CREATE:
// Get the metrics of the current font.
hdc = GetDC(hwndMain);
GetTextMetrics(hdc, &tm);
ReleaseDC(hwndMain, hdc);
// Save the average character width and height.
dwCharX = tm.tmAveCharWidth;
dwCharY = tm.tmHeight;
// Allocate a buffer to store keyboard input.
pchInputBuf = (LPTSTR) GlobalAlloc(GPTR,
BUFSIZE * sizeof(TCHAR));
return 0;
case WM_SIZE:
// Save the new width and height of the client area.
dwClientX = LOWORD(lParam);
dwClientY = HIWORD(lParam);
// Calculate the maximum width of a line and the
// maximum number of lines in the client area.
dwLineLen = dwClientX - dwCharX;
dwLines = dwClientY / dwCharY;
break;
case WM_SETFOCUS:
// Create, position, and display the caret when the
// window receives the keyboard focus.
CreateCaret(hwndMain, (HBITMAP) 1, 0, dwCharY);
SetCaretPos(nCaretPosX, nCaretPosY * dwCharY);
ShowCaret(hwndMain);
break;
case WM_KILLFOCUS:
// Hide and destroy the caret when the window loses the
// keyboard focus.
HideCaret(hwndMain);DestroyCaret();
break;
case WM_CHAR:
switch (wParam)
{
case 0x08: // backspace
case 0x0A: // linefeed
case 0x1B: // escape
MessageBeep((UINT) -1);
return 0;case 0x09: // tab
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 34/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
35/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
// Convert tabs to four consecutive spaces.7778798081828384858687888990919293949596979899
100101102103104105106107108109110
111112113114115116117118119120
121122
for (i = 0; i < 4; i++)
SendMessage(hwndMain, WM_CHAR, 0x20, 0);
return 0;
case 0x0D: // carriage return
// Record the carriage return and position the
// caret at the beginning of the new line.
pchInputBuf[cch++] = 0x0D;
nCaretPosX = 0;
nCaretPosY += 1;
break;
default: // displayable character
ch = (TCHAR) wParam;
HideCaret(hwndMain);
// Retrieve the character's width and output
// the character.
hdc = GetDC(hwndMain);
GetCharWidth32(hdc, (UINT) wParam, (UINT) wParam,
&nCharWidth);
TextOut(hdc, nCaretPosX, nCaretPosY * dwCharY,
&ch, 1);
ReleaseDC(hwndMain, hdc);
// Store the character in the buffer.
pchInputBuf[cch++] = ch;
// Calculate the new horizontal position of the
// caret. If the position exceeds the maximum,
// insert a carriage return and move the caret
// to the beginning of the next line.
nCaretPosX += nCharWidth;
if ((DWORD) nCaretPosX > dwLineLen)
{
nCaretPosX = 0;
pchInputBuf[cch++] = 0x0D;
++nCaretPosY;
}nCurChar = cch;
ShowCaret(hwndMain);
break;
}
SetCaretPos(nCaretPosX, nCaretPosY * dwCharY);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_LEFT: // LEFT ARROW// The caret can move only to the beginning of
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 35/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
36/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
// the current line.123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
157158159160161162163164165166
167168
if (nCaretPosX > 0)
{
HideCaret(hwndMain);
// Retrieve the character to the left of
// the caret, calculate the character's
// width, then subtract the width from the
// current horizontal position of the caret
// to obtain the new position.
ch = pchInputBuf[--nCurChar];
hdc = GetDC(hwndMain);
GetCharWidth32(hdc, ch, ch, &nCharWidth);
ReleaseDC(hwndMain, hdc);
nCaretPosX = max(nCaretPosX - nCharWidth, 0);
ShowCaret(hwndMain);
}
break;
case VK_RIGHT: // RIGHT ARROW
// Caret moves to the right or, when a carriage
// return is encountered, to the beginning of
// the next line.
if (nCurChar < cch)
{
HideCaret(hwndMain);
// Retrieve the character to the right of
// the caret. If it's a carriage return,
// position the caret at the beginning of
// the next line.
ch = pchInputBuf[nCurChar];
if (ch == 0x0D)
{
nCaretPosX = 0;
nCaretPosY++;
}
// If the character isn't a carriage// return, check to see whether the SHIFT
// key is down. If it is, invert the text
// colors and output the character.
else
{
hdc = GetDC(hwndMain);
nVirtKey = GetKeyState(VK_SHIFT);
if (nVirtKey & SHIFTED)
{
crPrevText = SetTextColor(hdc,RGB(255, 255, 255));
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 36/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
37/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
crPrevBk = SetBkColor(hdc,169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
203204205206207208209210211212
213214
RGB(0,0,0));
TextOut(hdc, nCaretPosX,
nCaretPosY * dwCharY,
&ch, 1);
SetTextColor(hdc, crPrevText);
SetBkColor(hdc, crPrevBk);
}
// Get the width of the character and
// calculate the new horizontal position of the caret.
GetCharWidth32(hdc, ch, ch, &nCharWidth);
ReleaseDC(hwndMain, hdc);
nCaretPosX = nCaretPosX + nCharWidth;
}
nCurChar++;
ShowCaret(hwndMain);
break;
}
break;
case VK_UP: // UP ARROW
case VK_DOWN: // DOWN ARROW
MessageBeep((UINT) -1);
return 0;
case VK_HOME: // HOME
// Set the caret's position to the upper left
// corner of the client area.
nCaretPosX = nCaretPosY = 0;
nCurChar = 0;
break;
case VK_END: // END
// Move the caret to the end of the text.
for (i=0; i < cch; i++)
{
// Count the carriage returns and save the
// index of the last one.if (pchInputBuf[i] == 0x0D)
{
cCR++;
nCRIndex = i + 1;
}
}
nCaretPosY = cCR;
// Copy all text between the last carriage
// return and the end of the keyboard input// buffer to a temporary buffer.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 37/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
38/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
for (i = nCRIndex, j = 0; i < cch; i++, j++)215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
249250251252253254255
szBuf[j] = pchInputBuf[i];
szBuf[j] = TEXT('\0');
// Retrieve the text extent and use it
// to set the horizontal position of the
// caret.
hdc = GetDC(hwndMain);
GetTextExtentPoint32(hdc, szBuf, lstrlen(szBuf), &sz);
nCaretPosX = sz.cx;
ReleaseDC(hwndMain, hdc);
nCurChar = cch;
break;
default:
break;
}
SetCaretPos(nCaretPosX, nCaretPosY * dwCharY);
break;
case WM_PAINT:
if (cch == 0) // nothing in input buffer
break;
hdc = BeginPaint(hwndMain, &ps);
HideCaret(hwndMain);
// Set the clipping rectangle, and then draw the text
// into it.
SetRect(&rc, 0, 0, dwLineLen, dwClientY);
DrawText(hdc, pchInputBuf, -1, &rc, DT_LEFT);
ShowCaret(hwndMain);
EndPaint(hwndMain, &ps);
break;
// Process other messages.
case WM_DESTROY:
PostQuitMessage(0);
// Free the input buffer.
GlobalFree((HGLOBAL) pchInputBuf);
UnregisterHotKey(hwndMain, 0xAAAA);break;
default:
return DefWindowProc(hwndMain, uMsg, wParam, lParam);
}
return NULL;
}
2. Thit b chut
a. Kim tra thit b chut
int GetSystemMetrics(
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 38/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
39/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
int nIndex // system metric or configuration setting
);
fMouse = GetSystemMetrics( SM_MOUSEPRESENT );
Gi tr tr v fMouse l TRUE (1) nu c thit b chut c ci t, v
ngc li bng FALSE (0) nu thit b chut khng c ci t vo
my.
b. Trong lp ca s ta nh ngha con tr chut cho ng dng
wndclass.hCursor = LoadCursor ( NULL, IDC_ARROR);
wndclass.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
Vi thit b chut ta c th c cc hnh ng nh sau:
Kch chut: nhn v th mt nt chut.
Kch p chut: nhn v th chut nhanh (nhn 2 ln nhanh).
Ko : di chuyn chut trong khi vn nm gi mt nt.
c. Thng ip chut trong vng lm vic
Nt Nhn Th Nhn p
Tri WM_LBUTTONDOWN WM_LBUTTONUP WM_LBUTTONDBLCLK
Gia WM_MBUTTONDOWN WM_MBUTTONUP WM_MBUTTONDBLCLK
Phi WM_RBUTTONDOWN WM_MBUTTONUP WM_RBUTTONDBLCLK
d. Gi tr wParam s cho bit trng thi ca nt nhn, phm Shift, v
phm Ctrl.
MK_LBUTTON Nt chut tri nhn
MK_MBUTTON Nt chut gia nhn
MK_RBUTTON Nt chut phi nhn
MK_SHIFT Phm Shift c nhn
MK_CONTROL Phm Ctrl c nhn
e. Gi tr lParam s cho bit v tr chut ti thi im pht sinh message.
2 bytes thp: ta x
2 bytes cao: ta y
f. V d
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 39/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
40/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
123456789
10111213141516171819202122232425262728293031323334
35363738394041424344
45
{
HDC hdc;
static POINT oldPoint;
static int iC;
int WIDTH_PEN = 2;
HPEN oPen,pen;
COLORREF Col [ ] ={ RGB (0, 0, 0) , RGB (255 ,0 ,0),
RGB (0, 255, 0), RGB (0, 0, 255), RGB (255, 255, 0)};
POINT point;
TCHAR str [255];
switch ( message )// Xl thngip{
case WM_LBUTTONDOWN:
/* Vng thng tv tr trc n v tr chut hin ti*/hdc = GetDC ( hWnd );
pen = CreatePen ( PS_SOLID,WIDTH_PEN,Col [
iC] );
oPen = ( HPEN ) SelectObject ( hdc,pen );
point.x = LOWORD ( lParam );
point.y = HIWORD ( lParam );
MoveToEx ( hdc, oldPoint.x, oldPoint.y, NULL );
LineTo ( hdc, point.x, point.y );
oldPoint = point;
/* Chn li bt vtrc v hy bt vva to*/SelectObject ( hdc, oPen );
DeleteObject ( pen );
ReleaseDC ( hWnd, hdc );
break;
case WM_RBUTTONDOWN:
/* Chuyn index ca bng mu sang v tr tip theo, nucui bng mu th quay li mu u tin*/
iC = ( iC+1 ) % ( sizeof ( Col ) / sizeof (
COLORREF ) );break;
case WM_MOUSEMOVE:
/* Xut to chut hin thi ln thanh tiu */sprintf ( str,"Toa do chuot x = %d, To do y = %d",
LOWORD(lParam), HIWORD(lParam));
SetWindowText ( hWnd, str );
/* Kim tra xem c giphm chut tri hay khng*/if ( wParam & MK_LBUTTON )
{
hdc = GetDC ( hWnd );
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 40/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
41/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
pen = CreatePen (
PS_SOLID,WIDTH_PEN,Col [ iC ] );
4647484950515253545556575859606162636465666768
oPen = ( HPEN ) SelectObject ( hdc, pen );
point.x = LOWORD ( lParam );
point.y = HIWORD ( lParam );
MoveToEx ( hdc, oldPoint.x, oldPoint.y,
NULL );
LineTo ( hdc, point.x, point.y );
oldPoint = point;
SelectObject ( hdc, oPen );
DeleteObject ( pen );
ReleaseDC ( hWnd, hdc );
}
break;
case WM_DESTROY:
PostQuitMessage ( 0 );
break;
default:
return DefWindowProc ( hWnd, message, wParam,
lParam );
}
return 0;
}
3. Timer
a. Khi to
UINT_PTR SetTimer( HWND hWnd, UINT_PTR nIDEvent, UINT
uElapse, TIMERPROC lpTimerFunc );
hWnd : nh danh ca ca s khai bo dng bnh thi gian.
nIDEvent : nh danh ca bnh thi gian.
nElapse : L khong thi gian ngh gia hai ln gi thng ip
lpTimerFunc : Hm s x l khi thng i p WM_TIMER pht
sinh, nu chng ta khai bo l NULL th Windows s gi thng
ip WM_TIMER vo hng i thng ip ca ca s tng ng.
b. Hy
BOOL KillTimer( HWND hWnd, UINT_PTR uIDEvent );
hWnd : nh danh ca ca s dng bnh thi gian
uIDEvent : nh danh ca bnh thi gian.
c. V d 1#include 1
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 41/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
42/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
#include "stdio.h"23456789
1011121314151617181920212223242526272829303132333435
36373839404142434445
4647
#define MAX_POINT 10000
#define IDT_TIMER1 1
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static int NumCir = 0;
static POINT point [ MAX_POINT ];
int r = 5, i;
HPEN pen, oldPen;
RECT rc;
TCHAR str [255];
/* Xl thngip*/switch ( message )
{
case WM_CREATE:
SetTimer(hWnd, IDT_TIMER1, 500,
(TIMERPROC) NULL);
srand ( (unsigned) time( NULL ) );
break;
case WM_PAINT:
hdc = BeginPaint ( hWnd, &ps );
pen = CreatePen ( PS_SOLID, 2, RGB (255,0,0) );
oldPen = (HPEN) SelectObject ( hdc, pen );
for( i=0; i < NumCir; i++ )
Arc ( hdc, point[i].x-r, point[i].y-r,
point[i].x+r, point[i].y+r, point[i].x+r,
point[i].y,point[i].x+r,point[i].y);
SelectObject ( hdc, oldPen );
DeleteObject ( pen );
EndPaint ( hWnd, &ps );
break;
case WM_TIMER:GetClientRect ( hWnd, &rc );
point [NumCir].x = rand( ) % (rc.right - rc.left);
point [NumCir].y = rand( ) % (rc.bottom - rc.top);
NumCir++;
sprintf ( str,"So vong tron : %d", NumCir);
SetWindowText ( hWnd, str );
InvalidateRect ( hWnd, &rc, FALSE);
break;
case WM_DESTROY:
KillTimer ( hWnd, IDT_TIMER1 );PostQuitMessage ( 0 );
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 42/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
43/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
break;48495051525354
123456789
101112131415161718192021222324
25262728293031323334
3536
default:
return DefWindowProc ( hWnd, message, wParam,
lParam );
}
return 0;
}
d. V d 2
#include
#include "stdio.h"
#define IDT_TIMER1 1
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
/* Khai bo bin lu cc gi tr khng gian*/struct tm *newtime;
time_t CurTime;
TCHAR str [255];
RECT rc;
/* Bin LOGFONTto font mi*/LOGFONT lf;
HFONT oldFont, font;
COLORREF color = RGB (255, 0, 0), oldColor;
switch ( message )
{
case WM_CREATE:
/* khi to bnh thi gian, v khai bo hm xl Timer*/SetTimer ( hWnd, IDT_TIMER1, 1000, ( TIMERPROC )
TimerProc );
break;
case WM_PAINT:hdc = BeginPaint ( hWnd, &ps );
time( &CurTime );
newtime = localtime ( &CurTime );
GetClientRect ( hWnd, &rc );
sprintf(str,"Gio hien tai : %d gio: %d phut: %d giay",
newtime->tm_hour,newtime->tm_min, newtime-
>tm_sec);
oldColor = SetTextColor ( hdc, color );
memset ( &lf, 0, sizeof ( LOGFONT ) );
lf.lfHeight = 50;strcpy ( lf.lfFaceName, "Tahoma" );
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 43/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
44/69
Bi 3:Cc thit b nhp liu Trn Minh Thi
font = CreateFontIndirect ( &lf );37383940414243444546474849505152535455565758596061
oldFont = ( HFONT ) SelectObject ( hdc,font );
DrawText ( hdc, str, strlen(str), &rc, DT_CENTER |
DT_VCENTER | DT_SINGLELINE );
SetTextColor ( hdc,oldColor );
SelectObject ( hdc,oldFont );
DeleteObject ( font );
EndPaint ( hWnd, &ps );
break;
case WM_DESTROY:
PostQuitMessage ( 0 );
break;
default:
return DefWindowProc ( hWnd, message, wParam,
lParam );
}
return 0;
}
VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg,
UINT_PTR idEvent, DWORD dwTime)
{
RECT rc;
GetClientRect ( hwnd, &rc );
InvalidateRect ( hwnd, &rc, TRUE );
}
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 44/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
45/69
Bi 4: Hp thai v iu khin Trn Minh Thi
Bi 4: HP THOI V IU KHIN
Phn bthi lng:- Stit ginglp: 12 tit- Stit thc nh: 12 tit- Stit ci t chng trnh nh: 24 tit
1. Hp thoi
Hp thoi phi hp gia ngi s dng vi chng trnh bng mt s phn
t iu khin m cc phn t ny nhn nhim v thu nhn thng tin t
ngi dng v cung cp thng tin n ngi dng khi ngi dng tc ng
n cc phn tiu khin. Cc phn tiu khin ny nhn ca s cha lmt h p thoi. Cc phn t iu khin thng l cc Button, List Box,
Combo Box, Check Box, Radio Button, Edit Box, Scroll Bar, Static.
Hp thoi trng thi (modal).
Hp thoi khng trng thi (modeless).
Hp thoi thng dng (common dialog)
a) Thit k hp thai
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 45/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
46/69
Bi 4: Hp thai v iu khin Trn Minh Thi
V d:
IDD_DIALOG1 DIALOG DISCARDABLE 0, 0, 196, 102STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTIONCAPTION "Logon"
FONT 8, "MS Sans Serif"
BEGIN DEFPUSHBUTTON "OK",IDOK,24,81,50,14
PUSHBUTTON "Cancel",IDCANCEL,109,81,50,14 LTEXT "User name",IDC_STATIC,7,23,40,15 LTEXT "Password",IDC_STATIC,7,50,40,16 EDITTEXT DC_EDT_NAME,52,19,137,16,ES_AUTOHSCROLL
EDITTEXT IDC_EDT_PASSWORD, 52, 48, 137, 16, ES_AUTOHSCROLL
END
Kiu iu khinLp
ca sKiu
PUSHBUTTON Button BS_PUSHBUTTON
DEFPUSHBUTTON Button BS_DEFBUSHBUTTON |
WS_TABSTOP
CHECKBOX Button BS_CHECKBOX | WS_TABSTOP
RADIOBUTTON Button BS_RADIOBUTTON |
WS_TABSTOP
GROUPBOX Button BS_GROUPBOX | WS_TABSTOP
LTEXT Static SS_LEFT | WS_GROUP
CTEXT Static SS_CENTER | WS_GROUP
RTEXT Static SS_RIGHT | WS_GROUP
ICON Static SS_ICON
EDITTEXT Edit ES_LEFT | WS_BORDER |
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 46/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
47/69
Bi 4: Hp thai v iu khin Trn Minh Thi
WS_STABSTOP
SCROLLBAR Scrollbar SBS_HORZ
LISTBOX Listbox LBS_NOTIFY | WS_BORDER |
WS_VSCROLL
COMBOBOX Combobox CBS_SIMPLE | WS_TABSTOP
Cc kiu iu khin
Cc kiu iu khin c khai bo trong resource script c dng nh
sau, ngoi tr kiu iu khin LISTBOX, COMBOBOX,
SCROLLBAR,EDITTEXT.
Control-type "text", id, xPos, yPos, xWidth, yHeight, iStyle
Cc kiu iu khin LISTBOX, COMBOBOX, SCROLLBAR,
EDITTEXTc khai bo trongresource scriptvi cu trc nh trn
nhng khng c trng "text".
Thm thuc tnh cho cc kiu iu khin bng cch thay i tham s
iStyle. V d ta mun to radio button vi chui din t nm bn tri
ca nt th ta gn trng iStyle bngBS_LEFTTEXTc thnhsau.
RADIOBUTTONRadio1",IDC_RADIO1,106,10,53,15,BS_LEFTTEXT
b) Th tc x l hp thai
c im
Mi hp thai cn c mt th tc x l ring.
Cc thng ip khng c gi ti hm x l ca s chnh.
L mt hm x l ca s.
Mu hm
BOOL CALLBACK Tn hm (HWND, UINT, WPARAM,
LPARAM) ;
C nhiu thng ip khc nhau.
Khng cn x l WM_PAINT v WM_DESTROY.
X l thng i p no th tr v TRUE, nu khng tr v
FALSE.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 47/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
48/69
Bi 4: Hp thai v iu khin Trn Minh Thi
Thng phi x l hai thng ip chnh: WM_INITDIALOG v
WM_COMMAND: LOWORD(WPARAM) cha ID cc iu
khin.
V d:LRESULT CALLBACK WndProc (HWND, UINT, WPARAM,
LPARAM);
123456789
101112131415161718
19202122232425262728
29303132333435363738
3940
BOOL CALLBACK DialogProc (HWND, UINT, WPARAM,
LPARAM) ;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInstance ;
switch (message)
{
case WM_CREATE :
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
return 0 ;
case WM_COMMAND :
switch (LOWORD (wParam))
{
case IDC_SHOW :
DialogBox (hInstance, TEXT ("DIALOG1"),
hwnd, DialogProc) ;break;
}
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
/*----------------------hm xl thngip hp thoi----------------------*/BOOL CALLBACK DialogProc (HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG: return TRUE ;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDOK :EndDialog (hDlg, 0) ;
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 48/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
49/69
Bi 4: Hp thai v iu khin Trn Minh Thi
return TRUE ;414243444546
}
break ;
}
return FALSE ;
}
c) Hp thoi trng thi
Hin th hp thoi
INT_PTR DialogBox(
HINSTANCEhInstance, // handle to moduleLPCTSTRlpTemplate, // dialog box templateHWND hWndParent, // handle to owner window
DLGPROClpDialogFunc // dialog box procedure
);V d:
DialogBox (hInstance, TEXT ("DIALOG1"), hwnd, DialogProc) ;
Gi thng ip n hm WndProc yu cu x l ngay c khi hp
thoi ang mnhhm SendMessage:
SendMessage(GetParent(hDlg), message, wParam, lParam);
Thm tiu cho hp thoi:
SetWindowText(hDlg,TEXT("Hello Dialog")); trong x lthng ip WM_INITDIALOG
ng hp thoi
BOOL EndDialog(
HWNDhDlg, // handle to dialog boxINT_PTRnResult // value to return
);
V d
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 49/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
50/69
Bi 4: Hp thai v iu khin Trn Minh Thi
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);123456789
10111213141516171819202122232425262728293031323334
35363738394041424344
4546
BOOL CALLBACK DialogProc (HWND, UINT, WPARAM, LPARAM);
int iCurrentColor = IDC_BLACK, iCurrentFigure = IDC_RECT;
int iCurrenBrush = IDC_HS_BDIAGONAL;
void PaintWindow(HWND hwnd, int iColor, int iFigure, int iBrush)
{
static COLORREF crColor[8] = { RGB(0, 0, 0), RGB(0, 0, 255),
RGB(0, 255, 0), RGB(0, 255, 255), RGB(255, 0, 0), RGB(255, 0, 255),
RGB(255, 255, 0), RGB(255, 255, 255) } ;
HBRUSH hBrush,hbrush;
HDC hdc ;
RECT rect ;
hdc = GetDC (hwnd) ;
GetClientRect (hwnd, &rect) ;
if(iBrush==IDC_HS_BDIAGONAL)
hbrush=CreateHatchBrush(HS_BDIAGONAL,
crColor[iColor-IDC_BLACK]);
if(iBrush == IDC_HS_CROSS)
hbrush=CreateHatchBrush(HS_CROSS,
crColor[iColor - IDC_BLACK]);
if(iBrush == IDC_HS_DIAGCROSS)
hbrush=CreateHatchBrush(HS_DIAGCROSS,
crColor[iColor - IDC_BLACK]);
if(iBrush == IDC_HS_FDIAGONAL)
hbrush=CreateHatchBrush(HS_FDIAGONAL,
crColor[iColor - IDC_BLACK]);
if(iBrush == IDC_HS_HORIZONTAL)
hbrush=CreateHatchBrush(HS_HORIZONTAL,
crColor[iColor - IDC_BLACK]);
if(iBrush == IDC_HS_VERTICAL)
hbrush=CreateHatchBrush(HS_BDIAGONAL,
crColor[iColor - IDC_BLACK]);
hBrush = (HBRUSH) SelectObject (hdc, hbrush) ;
if (iFigure == IDC_RECT)
Rectangle (hdc, rect.left, rect.top, rect.right, rect.bottom) ;else
Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom) ;
DeleteObject (SelectObject (hdc, hBrush)) ;
ReleaseDC (hwnd, hdc) ;
}
void PaintTheBlock(HWND hCtrl, int iColor, int iFigure, int iBrush)
{
InvalidateRect (hCtrl, NULL, TRUE) ;
UpdateWindow (hCtrl) ;
PaintWindow (hCtrl, iColor, iFigure,iBrush) ;}
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 50/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
51/69
Bi 4: Hp thai v iu khin Trn Minh Thi
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM
wParam, LPARAM lParam)
47484950515253545556575859606162636465666768697071727374757677787980
81828384858687888990
91
{
static HINSTANCE hInstance ;
PAINTSTRUCT ps ;
switch (message)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
return 0 ;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDC_SHOW:
if (DialogBox (hInstance, TEXT ("DIALOG"),
hwnd, DialogProc))
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
}
break;
case WM_PAINT:
BeginPaint (hwnd, &ps) ;
EndPaint (hwnd, &ps) ;
PaintWindow (hwnd, iCurrentColor, iCurrentFigure,
iCurrenBrush) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
BOOL CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM
wParam, LPARAM lParam)
{ static HWND hCtrlBlock ;
static int iColor, iFigure,iBrush;
switch (message)
{
case WM_INITDIALOG:
iColor = iCurrentColor ;
iFigure = iCurrentFigure ;
iBrush = iCurrenBrush;
CheckRadioButton(hDlg,IDC_BLACK,IDC_WHITE,
iColor);
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 51/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
52/69
Bi 4: Hp thai v iu khin Trn Minh Thi
CheckRadioButton(hDlg,IDC_RECT,IDC_ELLIPSE,iFig
ure);CheckRadioButton (hDlg, IDC_HS_BDIAGONAL,
IDC_HS_VERTICAL, iBrush);
9293949596979899
100101102103104105106107108109110111112113114115116117118119120121122123124125
126127128129130131132133134135
136137
hCtrlBlock = GetDlgItem (hDlg, IDC_PAINT) ;
SetFocus (GetDlgItem (hDlg, iColor)) ;
return FALSE ;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDOK:
iCurrentColor = iColor ;
iCurrentFigure = iFigure ;
iCurrenBrush = iBrush;
EndDialog (hDlg, TRUE) ;
return TRUE ;
case IDCANCEL:
EndDialog (hDlg, FALSE) ;
return TRUE ;
case IDC_BLACK:
case IDC_RED:
case IDC_GREEN:
case IDC_YELLOW:
case IDC_BLUE:
case IDC_MAGENTA:
case IDC_CYAN:
case IDC_WHITE:
iColor = LOWORD (wParam) ;
CheckRadioButton (hDlg, IDC_BLACK,
IDC_WHITE, LOWORD (wParam)) ;
PaintTheBlock (hCtrlBlock, iColor,
iFigure,iBrush);
return TRUE ;
case IDC_RECT:
case IDC_ELLIPSE:
iFigure = LOWORD (wParam) ;CheckRadioButton (hDlg, IDC_RECT,
IDC_ELLIPSE, LOWORD (wParam)) ;
PaintTheBlock (hCtrlBlock, iColor,
iFigure,iBrush);
return TRUE ;
case IDC_HS_BDIAGONAL:
case IDC_HS_CROSS:
case IDC_HS_DIAGCROSS:
case IDC_HS_FDIAGONAL:
case IDC_HS_HORIZONTAL:case IDC_HS_VERTICAL:
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 52/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
53/69
Bi 4: Hp thai v iu khin Trn Minh Thi
iBrush = LOWORD (wParam)138139140141142143144145146147148149150151
CheckRadioButton(hDlg,IDC_HS_BDIAGONAL,
IDC_HS_VERTICAL, LOWORD (wParam)) ;
PaintTheBlock (hCtrlBlock, iColor,
iFigure,iBrush);
return TRUE ;
}
break;
case WM_PAINT:
PaintTheBlock (hCtrlBlock, iColor, iFigure,iBrush) ;
break ;
}
return FALSE ;
}
d) Hp thoi khng trng thi
Hin th hp thoi
HWND hDlgModeless=CreateDialog(hInstance, szTemplate,hwndParent, DialogProc);
ShowWindow(hDlgModeless,SW_SHOW);
while(GetMessage(&msg, NULL, 0, 0)){
if(hDlgModeless==0 || !IsDialogMessage
(hDlgModeless, &msg);{TranslateMessage(&msg);DispatchMessage(&msg);
}}
while(GetMessage(&msg, NULL, 0, 0)){
if(hDlgModeless==0 || !IsDialogMessage(hDlgModeless,
&msg);{
if(TranslateAccelerator (hwnd, hAccel, &msg){
TranslateMessage(&msg);DispatchMessage(&msg);
}}
} ng hp thoi
t hDlgModeless v gi tr 0.
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 53/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
54/69
Bi 4: Hp thai v iu khin Trn Minh Thi
BOOL DestroyWindow(
HWNDhWnd // handle to window to destroy
);
V d
void PaintWindow (HWND hwnd, int iColor[], int iFigure)123456789
10111213141516171819202122232425262728
{
HBRUSH hBrush ;
HDC hdc ;
RECT rect ;
hdc = GetDC(hwnd) ;
GetClientRect (hwnd, &rect) ;
hBrush = CreateSolidBrush(RGB(iColor[0], iColor[1],
iColor[2]));
hBrush = (HBRUSH) SelectObject (hdc, hBrush) ;
if (iFigure == IDC_RECT)
Rectangle (hdc, rect.left, rect.top, rect.right,
rect.bottom) ;
else
Ellipse(hdc, rect.left, rect.top, rect.right,
rect.bottom) ;
DeleteObject (SelectObject (hdc, hBrush)) ;
ReleaseDC (hwnd, hdc) ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT
message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
PaintTheBlock(hwnd, iColor, iFigure) ;
return 0 ;
case WM_DESTROY :
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 54/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
55/69
Bi 4: Hp thai v iu khin Trn Minh Thi
DeleteObject((HGDIOBJ)SetClassLong(hw
nd, GCL_HBRBACKGROUND,(LONG)
GetStockObject (WHITE_BRUSH))) ;
29303132333435363738394041424344454647484950515253545556575859606162
63646566676869707172
7374
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam,
lParam);
}
void PaintTheBlock (HWND hCtrl, int iColor[], int iFigure)
{
InvalidateRect (hCtrl, NULL, TRUE);
UpdateWindow (hCtrl) ;
PaintWindow (hCtrl, iColor, iFigure) ;
}
BOOL CALLBACK ColorScrDlg (HWND hDlg, UINT
message, WPARAM wParam, LPARAM lParam)
{
HWND hwndParent, hCtrl ;
static HWND hCtrlBlock ;
int iCtrlID, iIndex ;
switch (message)
{
case WM_INITDIALOG :
hCtrlBlock = GetDlgItem (hDlg,
IDC_PAINT) ;
for (iCtrlID = 10 ; iCtrlID < 13 ; iCtrlID++)
{
hCtrl = GetDlgItem (hDlg, iCtrlID) ;
PaintTheBlock (hCtrlBlock, iColor,
iFigure) ;
PaintTheBlock (hwndParent, iColor,
iFigure) ;
SetScrollRange (hCtrl, SB_CTL, 0,
255, FALSE) ;SetScrollPos(hCtrl, SB_CTL, 0,
FALSE) ;
}
return TRUE ;
case WM_COMMAND:
{
switch( LOWORD(wParam))
{
case IDC_RECT:
case IDC_ELLIPSE:iFigure = LOWORD(wParam);
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 55/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
56/69
Bi 4: Hp thai v iu khin Trn Minh Thi
hwndParent =
GetParent(hDlg);
75767778798081828384858687888990919293949596979899
100101102103104105106107108
109110111112113114115116117118
119120
CheckRadioButton(hDlg,
IDC_RECT, IDC_ELLIPSE,
LOWORD (wParam)) ;
PaintTheBlock(hCtrlBlock,
iColor, iFigure) ;
PaintTheBlock (hwndParent,
iColor, iFigure) ;
return TRUE ;
}
break;
}
case WM_VSCROLL :
hCtrl = (HWND) lParam ;
iCtrlID = GetWindowLong (hCtrl,
GWL_ID) ;
iIndex = iCtrlID - 10 ;
hwndParent = GetParent (hDlg) ;
PaintTheBlock (hCtrlBlock, iColor, iFigure);
PaintTheBlock (hwndParent, iColor,
iFigure) ;
switch (LOWORD (wParam))
{
case SB_PAGEDOWN :
iColor[iIndex] += 15 ;
case SB_LINEDOWN :
iColor[iIndex] = min (255,
iColor[iIndex] + 1) ;
break;
case SB_PAGEUP :
iColor[iIndex] -= 15 ;
case SB_LINEUP :
iColor[iIndex] = max (0,
iColor[iIndex] - 1);break;
case SB_TOP :
iColor[iIndex] = 0 ;
break;
case SB_BOTTOM :
iColor[iIndex] = 255 ;
break;
case SB_THUMBPOSITION :
case SB_THUMBTRACK :
iColor[iIndex] = HIWORD(wParam) ;
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 56/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
57/69
Bi 4: Hp thai v iu khin Trn Minh Thi
break;121122123124125126127128129130131132133134135136137138139
default :
return FALSE ;
}
SetScrollPos(hCtrl, SB_CTL, iColor[iIndex],
TRUE) ;
SetDlgItemInt (hDlg, iCtrlID + 3, iColor[iIndex],
FALSE) ;
InvalidateRect(hwndParent,NULL,TRUE);
DeleteObject ( (HGDIOBJ)SetClassLong(
hwndParent, GCL_HBRBACKGROUND, (LONG)
CreateSolidBrush( RGB(iColor[0], iColor[1],
iColor[2]) ) ) ) ;
return TRUE ;
case WM_PAINT:
PaintTheBlock(hCtrlBlock, iColor, iFigure) ;
break;
}
return FALSE ;
2. Menu
a) To Menu
MENUDEMO MENU DISCARDABLE
BEGINPOPUP "&File"
BEGIN
MENUITEM "&New", IDM_FILE_NEW
MENUITEM "&Open", IDM_FILE_OPEN
MENUITEM "&Save", IDM_FILE_SAVE
MENUITEM "Save &As...", IDM_FILE_SAVE_AS
MENUITEM SEPARATOR
MENUITEM "E&xit", IDM_APP_EXIT
END
POPUP "&Edit"BEGIN
MENUITEM "&Undo", IDM_EDIT_UNDO
MENUITEM SEPARATOR
MENUITEM "C&ut", IDM_EDIT_CUT
MENUITEM "&Copy", IDM_EDIT_COPY
MENUITEM "&Paste", IDM_EDIT_PASTE
MENUITEM "De&lete", IDM_EDIT_CLEAR
END
POPUP "&Background"
BEGIN
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 57/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
58/69
Bi 4: Hp thai v iu khin Trn Minh Thi
MENUITEM "&White", IDM_BKGND_WHITE,
CHECKED
MENUITEM "&Light Gray", IDM_BKGND_LTGRAY
MENUITEM "&Gray", IDM_BKGND_GRAY
MENUITEM "&Dark Gray", IDM_BKGND_DKGRAY
MENUITEM "&Black", IDM_BKGND_BLACK
END
POPUP "&Help"
BEGIN
MENUITEM "&Help...", IDM_APP_HELP
MENUITEM "&About ...", IDM_APP_ABOUT
ENDEND
b) Thit lp Menu
wndclass.lpszMenuName = "MENU1";
hoc:
hMenu = LoadMenu ( hInstance, TEXT("MENU1") );
hwnd = CreateWindow ( TEXT("MyClass"), TEXT("Window
Caption"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, hMenu, hInstance, NULL );
SetMenu(hWnd, hMenu);
LOWORD(WPARAM) cha ID cc iu khin.
c) V d
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM,
LPARAM);
1234
56789
1011121314
1516
/* Khai bo tn dng chung cho ccti nguyn trong chng trnh.*/TCHAR szAppName[] = TEXT ("MenuDemo") ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCEhPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 58/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
59/69
Bi 4: Hp thai v iu khin Trn Minh Thi
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);17181920212223242526272829303132333435363738394041424344454647484950
51525354555657585960
6162
wndclass.hbrBackground =
(HBRUSH)GetStockObject(WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox(NULL, TEXT("This program requires
Windows "), szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT("Menu
Demonstration"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
/* Khao bo danh sch cc mu chi t, cc hng ny c nhngha trong file WINGDI.H */static int idColor[5] = { WHITE_BRUSH, LTGRAY_BRUSH,
GRAY_BRUSH, DKGRAY_BRUSH, BLACK_BRUSH } ;
static int iSelection = IDM_BKGND_WHITE ;
HMENU hMenu ;
switch (message){
case WM_COMMAND:
hMenu = GetMenu (hwnd) ;// Ly nh danh ca menuswitch (LOWORD (wParam))//Kim tra nh danh mc chn{
case IDM_FILE_NEW:
case IDM_FILE_OPEN:
case IDM_FILE_SAVE:
case IDM_FILE_SAVE_AS:
MessageBeep(0) ;//Pht ra ting ku bpreturn 0 ;
Bi ging: Lp trnh C for Win ............................................................. ................................ Trang 59/69
Smith Nguyen Studio.
-
8/3/2019 Giao Trinh Lap Trinh C for Winform - Smith.N Studio
60/69
Bi 4: Hp thai v iu khin Trn Minh Thi
case IDM_APP_EXIT:63646566676869707172737475767778798081828384858687888990919293949596
979899
100101102103104105106
107108
/*Gi thngip ngng dng li*/SendMessage (hwnd, WM_CLOSE, 0, 0) ;
return 0 ;
case IDM_EDIT_UNDO:
case IDM_EDIT_CUT:
case IDM_EDIT_COPY:
case IDM_EDIT_PASTE:
case IDM_EDIT_CLEAR:
MessageBeep (0) ;
return 0 ;
case IDM_BKGND_WHITE:
case IDM_BKGND_LTGRAY:
case IDM_BKGND_GRAY:
case IDM_BKGND_DKGRAY:
case IDM_BKGND_BLACK:
/* B check ca mc chn trc */CheckMenuItem(hMenu,iSelection,
MF_UNCHECKED);
iSelection = LOWORD (wParam) ;/*Ly IDmc mi*/
/* Check mc chn mi*/CheckMenuItem (hMenu, iSelection,
MF_CHECKED) ;
/* Thit lp mu tngng vi mc chnmi*/SetClassLong(hwnd,GCL_HBRBACKGRO
UND, (LONG)
Ge