際際滷

際際滷Share a Scribd company logo
1
L畉p tr狸nh C tr棚n Windows
V鱈 d畛 v Bi t畉p
Nguy畛n 畛c Hong H畉
Khoa CNTT  Tr動畛ng HKHTN
Email: ndhha@fit.hcmuns.edu.vn
2
WinMain
 int WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
//1. hInstanceng k箪 lo畉i c畛a s畛 m畛i
//2. T畉o m畛t c畛a s畛hWnd
//3. Th畛c hi畛n v嘆ng l畉p nh畉n v g畛i x畛 l箪 th担ng i畛p
return 0;
}
3
RegisterClassEx
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BBBBB);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_BBBBB;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(hInstance,(LPCTSTR)IDI_SMALL);
RegisterClassEx(&wcex);
4
CreateWindow
 hWnd = CreateWindow( szWindowClass, szTitle,
WS_OVERLAPPEDWINDOW,
100, 200, //tr叩i - tr棚n
500, 300, //r畛ng - cao
NULL, // c畛a s畛 cha
NULL, // menu
hInstance,
NULL );
 WS_OVERLAPPEDWINDOW = WS_OVERLAPPED |
WS_CAPTION | WS_SYSMENU | WS_THICKFRAME |
WS_MINIMIZEBOX | WS_MAXIMIZEBOX
5
DialogBox
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DialogBox(hInstance,
LPCTSTR(IDD_DIALOG1),
NULL,
DLGPROC(dlgProc) );
return 0;
}
6
Resource
7
Resource
8
Resource
9
Resource
10
WndProc-DlgProc
 LRESULT CALLBACK WndProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
 INT_PTR CALLBACK dlgProc(
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
11
MOUSE
 WM_LBUTTONDOWN - WM_LBUTTONUP -
WM_LBUTTONDBLCLK
 WM_RBUTTONDOWN - WM_RBUTTONUP -
WM_RBUTTONDBLCLK
 WM_MOUSEMOVE
 wParam: ghi nh畉n c叩c ph鱈m 畉c bi畛t
 MK_CONTROL
 MK_SHIFT
 MK_LBUTTON, MK_RBUTTON, MK_MBUTTON
 lParam:
 x = LOWORD(lParam)
 Y = HIWORD(lParam)
12
V鱈 d畛 1
D湛ng chu畛t v畉 o畉n th畉ng
 M担 t畉 bi to叩n:
 Nh畉n chu畛t tr叩i ch畛n i畛m 畉u
 Nh畉n chu畛t ph畉i ch畛n i畛m cu畛i
 Khi c坦 thay 畛i th狸 v畉 l畉i ra mn h狸nh
 Y棚u c畉u:
 Thi畉t k畉 d畛 li畛u
 Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n sau:
 WM_CREATE
 WM_PAINT
 WM_LBUTTONDOWN
 WM_RBUTTONDOWN
13
V鱈 d畛 1
 D畛 li畛u:
POINT P1, P2;
 WM_CREATE:
P1.x = P1.y = 0; P2.x = P2.y = 0;
 WM_LBUTTONDOWN:
P1.x = LOWORD(lParam); P1.y = HIWORD(lParam);
 WM_RBUTTONDOWN:
P2.x = LOWORD(lParam); P2.y = HIWORD(lParam);
InvalidateRect(hWnd, NULL,TRUE); //V畉 l畉i mn h狸nh
 WM_PAINT:
MoveTo(hdc,P1.x, P1.y);
LineTo(hdc,P2.x, P2.y);
14
Bi t畉p 1
 M担 t畉 bi to叩n:
 Nh畉n chu畛t tr叩i ch畛n i畛m 畉u
 Th畉 chu畛t tr叩i ch畛n i畛m cu畛i
 Khi c坦 thay 畛i th狸 v畉 l畉i ra mn h狸nh
 Y棚u c畉u:
 Thi畉t k畉 d畛 li畛u
 Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n sau:
 WM_CREATE, WM_PAINT
 WM_LBUTTONDOWN, WM_LBUTTONUP
 WM_MOUSEMOVE
15
TIMER
 UINT_PTR SetTimer(HWND hWnd,
UINT_PTR nID,
UINT uElapse, //s畛 nh畛p
TIMERPROC lpTimerFunc );
 WM_TIMER: wParam == nID
 1 gi但y = 1000 nh畛p
 V鱈 d畛: SetTimer(hWnd, 1, 500, NULL);
 Khi kh担ng d湛ng n畛a th狸 d湛ng l畛nh KillTimer 畛 h畛y:
KillTimer(hWnd, nID)
 Th動畛ng l畛nh SetTimer 動畛c 畉t trong s畛 ki畛n
WM_CREATE v KillTimer 畉t trong s畛 ki畛n
WM_DESTROY
16
V鱈 d畛 2
M担 ph畛ng banh r董i
 M担 t畉 bi to叩n:
 M畛t qu畉 banh r董i t畛 do t畛 畛 cao H xu畛ng n畛n r畛i
d畛i l棚n l畉i 畛 cao H.
 Y棚u c畉u:
 Thi畉t k畉 d畛 li畛u
 Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n sau:
 WM_CREATE, WM_DESTROY
 WM_PAINT
 WM_TIMER
17
V鱈 d畛 2
 D畛 li畛u:
double h; //畛 cao hi畛n t畉i
double a = -5;//gia t畛c
double v, //v畉n t畛c hi畛n t畉i
double H=300; //畛 cao t畛i a
int r=5; //b叩n k鱈nh
int T = 50;//s畛 nh畛p d湛ng 畛 畉t trong SetTimer
 WM_PAINT
x = 100; y = H+100-h; //畉o h畛 t畛a 畛
Ellipse(hdc,x-r,y-r, x+r,y+r);
MoveToEx(hdc,0,H+100,NULL);
LineTo(hdc,300,H+100);
18
V鱈 d畛 2
case WM_TIMER:
t = T / 1000.0;
h = a*t*t + v*t + h;
v = a*t + v;
if (h<0) {
h = 0;
v = -v;
}
InvalidateRect(hWnd,NULL,TRUE);
break;
19
V鱈 d畛 2
case WM_CREATE:
v = 0;//v畉n t畛c ban 畉u
h = H;//畛 cao ban 畉u
SetTimer(hWnd,1,T,NULL);
break;
case WM_DESTROY:
KillTimer(hWnd,1);
PostQuitMessage(0);
break;
20
 M担 t畉 bi to叩n:
 Y棚u c畉u:
 Thi畉t k畉 d畛 li畛u
 Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n sau:
 WM_CREATE , WM_DESTROY
 WM_PAINT
 WM_TIMER
Bi t畉p 2
H
V
L
21
 M担 t畉 bi to叩n:
 Nh畉n c叩c n炭t b畉ng chu畛t 畛 nh畉p
 Cho ph辿p nh畉n ph鱈m s畛 tr棚n bn ph鱈m
 Y棚u c畉u:
 Thi畉t k畉 d畛 li畛u
 Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n:
 WM_INITDIALOG
 WM_COMMAND
 WM_CHAR
V鱈 d畛 3
M叩y t鱈nh b畛 t炭i
22
V鱈 d畛 3
case WM_COMMAND:
switch(LOWORD(wParam)) {
case ID_BTN1:
//X畛 l箪 nh畉n n炭t 1
break;
case ID_BTN2:
//X畛 l箪 nh畉n n炭t 2
break;
}
return 1;
23
Bi t畉p 3
 Th棚m ph鱈m 但m d動董ng vo v鱈 d畛 3
24
Debug
 Ch畉y t畛ng l畛nh : F10
 Ch畉y vo trong l畛nh: F11
 畉t i畛m d畛ng: F9
 Chay ch動董ng tr狸nh 畛 ch畉 畛 debug: F5
 Ch畉y ch動董ng tr狸nh kh担ng 畛 ch畉 畛 debug:
Ctrl+F5
 Mu畛n debug 畛 s畛 ki畛n no th狸 ph畉i 畉t i畛m
d畛ng 畛 s畛 ki畛n 坦. Kh担ng th畛 ch畉y t畛ng l畛nh
(F10, F11) t畛i v畛 tr鱈 坦 動畛c.
25
MSDN
26
27
MFC l g狸?

More Related Content

11 vidu baitap_c

  • 1. 1 L畉p tr狸nh C tr棚n Windows V鱈 d畛 v Bi t畉p Nguy畛n 畛c Hong H畉 Khoa CNTT Tr動畛ng HKHTN Email: ndhha@fit.hcmuns.edu.vn
  • 2. 2 WinMain int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { //1. hInstanceng k箪 lo畉i c畛a s畛 m畛i //2. T畉o m畛t c畛a s畛hWnd //3. Th畛c hi畛n v嘆ng l畉p nh畉n v g畛i x畛 l箪 th担ng i畛p return 0; }
  • 3. 3 RegisterClassEx WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BBBBB); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCSTR)IDC_BBBBB; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(hInstance,(LPCTSTR)IDI_SMALL); RegisterClassEx(&wcex);
  • 4. 4 CreateWindow hWnd = CreateWindow( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 100, 200, //tr叩i - tr棚n 500, 300, //r畛ng - cao NULL, // c畛a s畛 cha NULL, // menu hInstance, NULL ); WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
  • 5. 5 DialogBox int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { DialogBox(hInstance, LPCTSTR(IDD_DIALOG1), NULL, DLGPROC(dlgProc) ); return 0; }
  • 10. 10 WndProc-DlgProc LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); INT_PTR CALLBACK dlgProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  • 11. 11 MOUSE WM_LBUTTONDOWN - WM_LBUTTONUP - WM_LBUTTONDBLCLK WM_RBUTTONDOWN - WM_RBUTTONUP - WM_RBUTTONDBLCLK WM_MOUSEMOVE wParam: ghi nh畉n c叩c ph鱈m 畉c bi畛t MK_CONTROL MK_SHIFT MK_LBUTTON, MK_RBUTTON, MK_MBUTTON lParam: x = LOWORD(lParam) Y = HIWORD(lParam)
  • 12. 12 V鱈 d畛 1 D湛ng chu畛t v畉 o畉n th畉ng M担 t畉 bi to叩n: Nh畉n chu畛t tr叩i ch畛n i畛m 畉u Nh畉n chu畛t ph畉i ch畛n i畛m cu畛i Khi c坦 thay 畛i th狸 v畉 l畉i ra mn h狸nh Y棚u c畉u: Thi畉t k畉 d畛 li畛u Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n sau: WM_CREATE WM_PAINT WM_LBUTTONDOWN WM_RBUTTONDOWN
  • 13. 13 V鱈 d畛 1 D畛 li畛u: POINT P1, P2; WM_CREATE: P1.x = P1.y = 0; P2.x = P2.y = 0; WM_LBUTTONDOWN: P1.x = LOWORD(lParam); P1.y = HIWORD(lParam); WM_RBUTTONDOWN: P2.x = LOWORD(lParam); P2.y = HIWORD(lParam); InvalidateRect(hWnd, NULL,TRUE); //V畉 l畉i mn h狸nh WM_PAINT: MoveTo(hdc,P1.x, P1.y); LineTo(hdc,P2.x, P2.y);
  • 14. 14 Bi t畉p 1 M担 t畉 bi to叩n: Nh畉n chu畛t tr叩i ch畛n i畛m 畉u Th畉 chu畛t tr叩i ch畛n i畛m cu畛i Khi c坦 thay 畛i th狸 v畉 l畉i ra mn h狸nh Y棚u c畉u: Thi畉t k畉 d畛 li畛u Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n sau: WM_CREATE, WM_PAINT WM_LBUTTONDOWN, WM_LBUTTONUP WM_MOUSEMOVE
  • 15. 15 TIMER UINT_PTR SetTimer(HWND hWnd, UINT_PTR nID, UINT uElapse, //s畛 nh畛p TIMERPROC lpTimerFunc ); WM_TIMER: wParam == nID 1 gi但y = 1000 nh畛p V鱈 d畛: SetTimer(hWnd, 1, 500, NULL); Khi kh担ng d湛ng n畛a th狸 d湛ng l畛nh KillTimer 畛 h畛y: KillTimer(hWnd, nID) Th動畛ng l畛nh SetTimer 動畛c 畉t trong s畛 ki畛n WM_CREATE v KillTimer 畉t trong s畛 ki畛n WM_DESTROY
  • 16. 16 V鱈 d畛 2 M担 ph畛ng banh r董i M担 t畉 bi to叩n: M畛t qu畉 banh r董i t畛 do t畛 畛 cao H xu畛ng n畛n r畛i d畛i l棚n l畉i 畛 cao H. Y棚u c畉u: Thi畉t k畉 d畛 li畛u Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n sau: WM_CREATE, WM_DESTROY WM_PAINT WM_TIMER
  • 17. 17 V鱈 d畛 2 D畛 li畛u: double h; //畛 cao hi畛n t畉i double a = -5;//gia t畛c double v, //v畉n t畛c hi畛n t畉i double H=300; //畛 cao t畛i a int r=5; //b叩n k鱈nh int T = 50;//s畛 nh畛p d湛ng 畛 畉t trong SetTimer WM_PAINT x = 100; y = H+100-h; //畉o h畛 t畛a 畛 Ellipse(hdc,x-r,y-r, x+r,y+r); MoveToEx(hdc,0,H+100,NULL); LineTo(hdc,300,H+100);
  • 18. 18 V鱈 d畛 2 case WM_TIMER: t = T / 1000.0; h = a*t*t + v*t + h; v = a*t + v; if (h<0) { h = 0; v = -v; } InvalidateRect(hWnd,NULL,TRUE); break;
  • 19. 19 V鱈 d畛 2 case WM_CREATE: v = 0;//v畉n t畛c ban 畉u h = H;//畛 cao ban 畉u SetTimer(hWnd,1,T,NULL); break; case WM_DESTROY: KillTimer(hWnd,1); PostQuitMessage(0); break;
  • 20. 20 M担 t畉 bi to叩n: Y棚u c畉u: Thi畉t k畉 d畛 li畛u Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n sau: WM_CREATE , WM_DESTROY WM_PAINT WM_TIMER Bi t畉p 2 H V L
  • 21. 21 M担 t畉 bi to叩n: Nh畉n c叩c n炭t b畉ng chu畛t 畛 nh畉p Cho ph辿p nh畉n ph鱈m s畛 tr棚n bn ph鱈m Y棚u c畉u: Thi畉t k畉 d畛 li畛u Vi畉t m達 gi畉 x畛 l箪 cho c叩c s畛 ki畛n: WM_INITDIALOG WM_COMMAND WM_CHAR V鱈 d畛 3 M叩y t鱈nh b畛 t炭i
  • 22. 22 V鱈 d畛 3 case WM_COMMAND: switch(LOWORD(wParam)) { case ID_BTN1: //X畛 l箪 nh畉n n炭t 1 break; case ID_BTN2: //X畛 l箪 nh畉n n炭t 2 break; } return 1;
  • 23. 23 Bi t畉p 3 Th棚m ph鱈m 但m d動董ng vo v鱈 d畛 3
  • 24. 24 Debug Ch畉y t畛ng l畛nh : F10 Ch畉y vo trong l畛nh: F11 畉t i畛m d畛ng: F9 Chay ch動董ng tr狸nh 畛 ch畉 畛 debug: F5 Ch畉y ch動董ng tr狸nh kh担ng 畛 ch畉 畛 debug: Ctrl+F5 Mu畛n debug 畛 s畛 ki畛n no th狸 ph畉i 畉t i畛m d畛ng 畛 s畛 ki畛n 坦. Kh担ng th畛 ch畉y t畛ng l畛nh (F10, F11) t畛i v畛 tr鱈 坦 動畛c.
  • 26. 26