Professional Documents
Culture Documents
Bai Giang Lap Trinh C Trong Window PDF
Bai Giang Lap Trinh C Trong Window PDF
Trang
M u ......................................................................................................... 2
Cc th vin lp trnh ca Windows ............................................................. 3
Cc khi nim c bn ................................................................................... 4
Lp trnh s kin (Even driven programming)............................................... 5
Cc thnh phn giao din ha (GUI) ....................................................... 6
Cu trc chng trnh C for Win................................................................. 10
Qui trnh hot ng ca chng trnh ng dng ........................................ 10
Mt s quy c t tn............................................................................... 11
V d ........................................................................................................... 11
Ti nguyn ca ng dng (Resources) ................................................... 18
Mt s kiu d liu mi ........................................................................... 19
Phn tch, tm hiu source code ca project ........................................... 19
Bi 2: PAINT V REPAINT................................................................. 24
1.
2.
3.
4.
1.
2.
3.
Bn phm .................................................................................................... 31
Thit b chut .............................................................................................. 38
Timer........................................................................................................... 41
Hp thoi..................................................................................................... 45
Menu ........................................................................................................... 57
Bi 5: X L VN BN ...................................................................... 62
1.
2.
3.
Hin th vn bn .......................................................................................... 62
nh dng vn bn ...................................................................................... 64
S dng font ............................................................................................... 65
1.
M u
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.
Lp trnh trn Windows kh v phc tp hn nhiu so vi lp trnh trn
DOS. 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 nh o, 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
h iu 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 s c khi trong my c ci t
Windows.
Vi im khc bit gia lp trnh Windows v DOS:
Windows
DOS
Thc hin tun t theo ch nh
Multi-tasking
Single task
Multi-CPU
Single CPU
Tch hp sn Multimedia
H tr 32 bits hay hn na
ng dng 16 bits
Khng c
2.
(Xem
thm
MSDN/PlatForm
SDK
Documentation/Getting
Cc loi ng dng:
Win32 Console: ng dng 32 bits, vi giao din dng DOS command
line.
Bi ging: Lp trnh C for Win ...............................................................................................Trang 3/69
3.
Cc khi nim c bn
Message:
Trao i thng tin gia chng trnh ng dng v h iu 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 s c gi bi HH, khng phi bi ng
dng.
4.
System
Queue
Keyboard
Driver
USER.EXE
Mouse
Driver
System
Queue
Application
Queue
Keyboard
Device driver
GetMessage()
TranslateMessage()
DispatchMessage()
WindowProc()
DefWindowProc()
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
5.
Dialog:
9 Mt ca s lm vic.
9 Thng c kch thc c nh.
9 Thng khng c menu bar.
9 Thng c cc button, edit box, list-box,
9 V d: Calculator, CD Player,
Ca s:
9 nh ngha:
L 1 vng ch nht trn mn hnh.
Dng hin th kt qu output.
V nhn cc input t ngi dng
9 Cng vic u tin ca 1 ng dng GUI l to 1 ca s lm vic.
9 Nguyn tc qun l:
M hnh phn cp: mi ca s u 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).
9 Phn loi:
Ca s Desktop.
Ca s tiu chun.
Ca s hp thoi (Dialog box).
Cc control.
Desktop
Window
App
Window
Control
Dialog
box
Parent
Window
Child
Window
Window icon
Window caption
Title bar
Minimized,
Restored,
Maximized button
menu bar
standard bar
tool bar
scroll bar
Client area
status bar
Resized
border
6.
Resource file
(*.RES)
Compiler
Resource
Compiler
Definition file
(*.DEF)
*.OBJ file
Linker
Window Application
(*.EXE, *.DLL)
Library file
(*.LIB)
7.
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:
Vi du mo dau
1
2
3
#include <windows.h>
LRESULT CALLBACK XulyMessage (HWND,UNIT,WPARAM,LPARAM);
char szAppName [ ] = Vidu;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Class
Description
Button
ListBox
MDIClient
ScrollBar
Static
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 s u thuc mt lp xc nh.
Mu nn ca s
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 h iu hnh (RegisterClassEX).
ATOM RegisterClassEx (CONST WNDCLASSEX *lpWClass);
vi: Kiu gi tr ca ATOM c nh ngha trong window.h l
WORD; lpWClass l con tr n 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 h iu hnh.
Khng b nh.
To lp ca s lm vic (Frame Window)
M t
WS_OVERLAPPED
WS_SYSMENU
Ca s c hp thc n h thng
WS_VSCROLL
Ca s c thanh trt dc
WS_HSCROLL
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
SW_HIDE
Du ca s
SW_MINIMIZE
Thu nh ca s
To ca s
Hin th
CreateWindow
ShowWindow
Cp nht
UpdateWindow
ii. Xut hin hp thoi (Dialog box), thng bo (Message box) lm che
mt 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 s ang minimize maximize
HDC: (Handle to a device context) ch n 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.
Accelerator
Bitmap
nh bitmap.
Caret
Con tr vn bn.
Cusor
Con tr chut.
Dialog box
Khung hp thoi.
Enhance metafile
Font
Icon
Biu tng.
Menu
Menu.
String-table entry
Bng m t cc chui k t.
Version information
11.
Stt
Mt s kiu d liu mi
Kiu d liu
Ch thch
(nguyn khng du 16 bit) dng nh danh i tng
HANDLE
HWND : window
HMENU : menu
HCURSOR : cursor
HBRUSH
HPALLETE
(pallete) bng mu
HFONT
HBITMAP
bitmap
HICON
icon
HPEN
HINSTANCE Instance
HDC
Device context
10
LTSTR
11
LPARAM
12
LRESULT
13
LPVOID
12.
1
2
3
4
5
6
7
WPARAM
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
wcex.lpfnWndProc = (WNDPROC)WndProc;
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 variable
hWnd = 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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:
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
Bi 2:Paint v repaint
Bi 2: PAINT V REPAINT
Phn b thi lng:
- S tit ging lp: 6 tit
- S tit t hc nh: 6 tit
- S tit 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 =
Bi 2:Paint v repaint
typedef tagRECT
{
LONG left, top;
LONG right, bottom;
}RECT;
Ng cnh
thit b GDI
Trnh iu
khin thit b
Thit b
xut
Bi 2:Paint v repaint
Byte 2
Byte 1
Byte 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 th t NULL vo v tr hDC, Windows s to mt
device context tng thch vi mn hnh.
Bi 2:Paint v repaint
Bi 2:Paint v repaint
3. Mt s hm ha c s
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 v n 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);
V ng gp khc ln hDC bng cc on thng lin tip, s nh l
nPoints vi ta cc nh c xc nh trong lpPoints. Hm tr v
TRUE nu thnh cng, FALSE nu tht bi.
BOOL Polygon(HDC hDC, const POINT FAR *lpPoints, int
nPoints);
V a 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
Bi 2:Paint v repaint
BOOL Rectangle(HDC hDC, int left, int top, int right, int bottom);
V hnh ch nht c ta l left, top, right, bottom ln hDC.
HPEN CreatePen(int penStyle, int penWidth, COLORREF
penColor);
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 penStyle
V d : To bt v mi v dng bt v ny v mt s ng c s.
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 2:Paint v repaint
4. Kt lun
WM_PAINT l message c u tin thp. Khi WM_PAINT trong hng ch v
c mt s Window Message khc th Windows x l WM khc ri mi x l
WM_PAINT.
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
c. Thng ip phm
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Thng ip
WM_ACTIVATE
Thng ip ny cng c gi n cc ca s b
kch hot v ca s khng b kch hot. Nu cc
ca s ny cng mt hng i nhp liu, cc
thng ip ny s c truyn mt cch ng
b, u tin th tc Windows ca ca s trn
cng b mt kch hot, sau n th tc ca
ca s trn cng c kch hot. Nu cc ca s
ny khng nm trong cng mt hng i th
thng ip s c gi mt cch khng ng b,
do ca s s c kch hot ngay lp tc.
WM_APPCOMMAND
WM_CHAR
Thng ip ny c gi ti ca s c s quan
tm khi thng ip WM_KEYDOWN c
dch t hm TranslateMessage. Thng ip
WM_CHAR c cha m k t ca phm c
nhn.
WM_DEADCHAR
Thng ip ny c gi ti ca s c s quan
tm khi thng ip WM_KEYUP c x l
t hm TranslateMessage. Thng ip ny xc
nhn m k t khi mt phm dead key c
nhn. Phm dead key l phm kt hp to ra k
t ngn ng khng c trong ting anh (xut hin
trong bn phm h tr ngn ng khc ting
Anh).
WM_GETHOTKEY
ng dng gi thng ip ny xc nh mt
phm nng lin quan n mt ca s. gi
thng ip ny th dng hm SendMessage.
WM_HOTKEY
WM_KEYDOWN
WM_KEYUP
WM_KILLFOCUS
WM_SETFOCUS
WM_SETHOTKEY
WM_SYSCHAR
Thng ip ny s c gi ti ca s nhn
c s quan tm khi hm TranslateMesage x
l xong thng ip WM_SYSKEYDOWN.
Thng ip ny c gi ti ca s nhn c
s quan tm khi ngi dng nhn phm h
thng.
d. V d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
COLORREF crPrevBk;
// previous background color
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
crPrevBk = SetBkColor(hdc,
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.
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
}
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(
Nhn
Th
Nhn p
Tri
WM_LBUTTONDOWN
WM_LBUTTONUP
WM_LBUTTONDBLCLK
Gia
Phi
WM_RBUTTONDOWN
WM_MBUTTONUP WM_RBUTTONDBLCLK
MK_MBUTTON
MK_RBUTTON
MK_SHIFT
MK_CONTROL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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;
SelectObject ( hdc, oPen );
DeleteObject ( pen );
ReleaseDC ( hWnd, hdc );
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
}
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 b nh thi gian.
nIDEvent : nh danh ca b nh thi gian.
nElapse : L khong thi gian ngh gia hai ln gi thng ip
lpTimerFunc : Hm s x l khi thng ip 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 b nh thi gian
uIDEvent : nh danh ca b nh thi gian.
c. V d 1
#include <time.h>
Bi ging: Lp trnh C for Win .............................................................................................Trang 41/69
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stdio.h"
#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];
/* X l thng ip*/
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 );
48
49
50
51
52
53
54
break;
default:
return DefWindowProc ( hWnd, message, wParam,
lParam );
}
return 0;
}
d. V d 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <time.h>
#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 LOGFONT to font mi*/
LOGFONT lf;
HFONT oldFont, font;
COLORREF color = RGB (255, 0, 0), oldColor;
switch ( message )
{
case WM_CREATE:
/* khi to b nh thi gian, v khai bo hm x l 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" );
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Bi 4: Hp thai v iu khin
Bi 4: HP THOI V IU KHIN
Phn b thi lng:
- S tit ging lp: 12 tit
- S tit t hc nh: 12 tit
- S tit 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 t iu khin. Cc phn t iu khin ny nhn ca s cha l
mt hp thoi. Cc phn t iu khin thng l cc Button, List Box,
Combo Box, Check Box, Radio Button, Edit Box, Scroll Bar, Static.
a)
Thit k hp thai
Bi 4: Hp thai v iu khin
V d:
Lp
ca s
Kiu
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 4: Hp thai v iu khin
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,
EDITTEXT c khai bo trong resource script vi 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 bng BS_LEFTTEXT c th nh sau.
RADIOBUTTON Radio1",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 ip no th tr v TRUE, nu khng tr v
FALSE.
Bi 4: Hp thai v iu khin
Bi 4: Hp thai v iu khin
return TRUE ;
41
42
43
44
45
46
}
break ;
}
return FALSE ;
}
c)
Hin th hp thoi
INT_PTR DialogBox(
HINSTANCE hInstance, // handle to module
LPCTSTR lpTemplate, // dialog box template
HWND hWndParent, // handle to owner window
DLGPROC lpDialogFunc // dialog box procedure
);
V d:
DialogBox (hInstance, TEXT ("DIALOG1"), hwnd, DialogProc) ;
ng hp thoi
BOOL EndDialog(
HWND hDlg,
// handle to dialog box
INT_PTR nResult // value to return
);
V d
Bi 4: Hp thai v iu khin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Bi 4: Hp thai v iu khin
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Bi 4: Hp thai v iu khin
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
CheckRadioButton(hDlg,IDC_RECT,IDC_ELLIPSE,iFig
ure);CheckRadioButton (hDlg, IDC_HS_BDIAGONAL,
IDC_HS_VERTICAL, iBrush);
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 4: Hp thai v iu khin
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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);
}
}
Bi 4: Hp thai v iu khin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
BOOL DestroyWindow(
HWND hWnd // handle to window to destroy
);
V d
Bi 4: Hp thai v iu khin
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
DeleteObject((HGDIOBJ)SetClassLong(hw
nd, GCL_HBRBACKGROUND,(LONG)
GetStockObject (WHITE_BRUSH))) ;
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 4: Hp thai v iu khin
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
hwndParent =
GetParent(hDlg);
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 4: Hp thai v iu khin
break;
default :
return FALSE ;
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
}
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
BEGIN
POPUP "&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 4: Hp thai v iu khin
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
V d
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM,
LPARAM);
/* Khai bo tn dng chung cho ccti nguyn trong chng trnh.*/
TCHAR szAppName[] = TEXT ("MenuDemo") ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE
hPrevInstance, 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 4: Hp thai v iu khin
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Bi 4: Hp thai v iu khin
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
case IDM_APP_EXIT:
/*Gi thng ip ng ng 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 ID
mc mi*/
/* Check mc chn mi*/
CheckMenuItem (hMenu, iSelection,
MF_CHECKED) ;
/* Thit lp mu tng ng vi mc chn
mi*/
SetClassLong(hwnd,GCL_HBRBACKGRO
UND, (LONG)
GetStockObject(idColor[iSelectionIDM_BKGND_WHITE]));
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case IDM_APP_HELP:
MessageBox(hwnd, TEXT("Help not yet
implemented!"), szAppName,
MB_ICONEXCLAMATION | MB_OK) ;
return 0 ;
case IDM_APP_ABOUT:
MessageBox (hwnd, TEXT ("Menu
Demonstration Program\n (c) Charles
Petzold, 1998"), szAppName,
MB_ICONINFORMATION | MB_OK) ;
return 0 ;
}
break;
case WM_DESTROY:
Bi 4: Hp thai v iu khin
109
110
111
112
113
PostQuitMessage(0) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}
Bi 5: X L VN BN
Phn b thi lng:
- S tit ging lp: 6 tit
- S tit t hc nh: 6 tit
- S tit ci t chng trnh nh: 12 tit
1. Hin th vn bn
hin th ni dung vn bn trn cc thit b xut, da vo tng trng hp th
hin khc nhau, ta dng cc hm Win32 API khc nhau. Cc hm ny ph
thuc vo font ch, thuc tnh ca thit b ng cnh DC (Device Context ) v
khong cch k t th hin.
Hm ph bin nht thc hin thao tc xut mt chui k t vn bn, s dng
font ch, mu ch v mu nn hin hnh l :
BOOL TextOut(HDC hdc, int nXStart, int nYStart, LPCTSTR
lpString,int cbString);
2. nh dng vn bn
a) Hm thit lp mu ch v mu nn:
b) Xc nh mu ch v mu nn hin hnh:
c) Xc nh ch nn hin ti:
fMode:
TA_LEFT,
TA_RIGHT,
TA_CENTER,
TA_TOP,
Nu thnh cng, gi tr tr v cho bit khong cch hin ti. Ngc li,
gi tr tr v l 0x80000000.
3. S dng font
Lp ch s font ch.
Np font ch.
FONT
ANSI_FIXED_FONT
ANSI_VAR_FONT
OEM_FIXED_FONT
SYSTEM_FONT
SYSTEM_FIXED_FONT
Hoc gn hn, ta c th gi :
SelectObject(hDC.GetStockObject(fnObject));
DeleteObject (i tng): hy.
V d:
HFONT hfnt, hOldFont;
hfnt = GetStockObject(ANSI_VAR_FONT);
if (hOldFont = SelectObject(hdc, hfnt))
{
Bi ging: Lp trnh C for Win .............................................................................................Trang 66/69
TEXTMETRIC tm;
case WM_CREATE:
{
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxchar=tm.tmInternalLeading+tm.tmExternal;
cychar=tm.tmAveCharWidth;
ReleaseDC(hwnd, hdc);
return 0;
}
case WM_PAINT:
{
for(int i=0; i<10; i++)
TextOut(hdc, cxchar, cychar*i, aaa, 3);
}
Tnh di ca xu k t
Cc k t hin th c b rng khc nhau do vy khng nn dng
hm strlen() ly s k t di.
Dng hm: BOOL GetTextExtentPoint32 (HDC hdc, LPCSTR
lpszString, int len, LPSIZE lpSize);
typedef struct tagSIZE
{
long cx;
long cy;
//Tnh theo n v logic
} SIZE;
len: Tng s k t.
To lp c tnh mi cho font ch
HFONT CreateFont (int Height, int Width, int Escapement, int Orientation,
int fnWeight, DWORD Italic, DWORD Underline, DWORD StrikeOut,
DWORD CharSet, DWORD outputPrecision, DWORD ClipPrecision,
DWORD Quality, DWORD PitchAndFamily, LPCSTR lpszFontName)
Vi:
Bi ging: Lp trnh C for Win .............................................................................................Trang 68/69
Gi tr
Tn
Gi tr
FW_DONTCARE
FW_SEMIBOLD
600
FW_THIN
100
FW_DEMIBOLD
600
FW_EXTRALIGHT
200
FW_BOLD
700
FW_ULTRALIGHT
200
FW_EXTRABOLD
800
FW_LIGHT
300
FW_ULTRABOLD
800
FW_NORMAL
400
FW_HEAVY
900
FW_REGULAR
400
FW_BLACK
900
FW_MEDIUM
500
Macro xc nh m nht lfWeight