Bài giảng Lập trình Windows - Phạm Ngọc Hưng

pdf 312 trang cucquyet12 3660
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình Windows - Phạm Ngọc Hưng", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_lap_trinh_windows_pham_ngoc_hung.pdf

Nội dung text: Bài giảng Lập trình Windows - Phạm Ngọc Hưng

  1. 9/13/2011 ክኧ 0ኈ  ኞ   ኤ  ኛW  ኛ W  ኘ W ϯ@ WINDOWS Š኶ ‰ዌ ዛ‰ ዒ môn ዣ –Š—ኼ– Máy tính hungpn@soict.hut.edu.vn 1
  2. 9/13/2011 Н’ trình Windows ‡ M㠕ዎ: IT4781 ‡ Šዕ‹ Žዛዘ‰: 2 (2-1-0-4) ± Lý –Š—›ዅ–: 30 –‹ዅ– ± Bài –ኼ’: 15 –‹ዅ– ‡ ዌ ’Šኹ tiên “—›ዅ–: ± Ngôn ‰ዟ Žኼ’ trình C, C++ ± ኼ’–”¿ŠŠዛዔ‰¯ዎ‹–ዛዘ‰ ኼ’–”¿Š‹†‘™• 2 2
  3. 9/13/2011 г‹ dung ‡ Šዛዓ‰ 1. ዐ‰ quan Žኼ’ trình Windows (4LT + 1BT) ‡ Šዛዓ‰ 2. ኼ’ trình Windows VC++/MFC (15LT + 6BT) ‡ Šዛዓ‰͵Ǥዒ– •ዎ Šዚ ¯዆ nâng cao ± ኶‘ –Šዛ ˜‹ዉ liên ዅ– ¯ዒ‰ (DLL) (3LT + 2BT) ± —ዏ‰ (Thread) (3LT + 2BT) ± Windows Socket (2LT + 2BT) ‡ Šዛዓ‰4. Ÿ›†ዠ‰ዜ‰†ዙ‰‹†‘™• ‘”ȋ͵ + 2BT) ‡ Bài –ኼ’ Žዔ ኼ’–”¿Š‹†‘™• 3 3
  4. 9/13/2011 Tài Ž‹Ъ— tham ŠИ‘ ‡ 1. Ivor ‘”–‘ǯ•ǡ‡‰‹‹‰‹•—ƒŽΪΪʹͲͳͲ ‡ 2. Programming Windows with MFC 2nd ‡ 2. VC++ and MFC Programming 2nd ‡ 3. Sams teach yourself VC++ 6 in 21 days ‡ 4. MSDN ‡ 5. ኼ’–”¿Š‹†‘™• 4 4
  5. 9/13/2011 Šмд‰ 1. б‰ quan ŽН’ trình Windows ‡ 1.1. ዓ „኷ ˜዆ Žኼ’ trình Windows (1T) ‡ 1.2. ኸ— trúc ዚƒ Šዛዓ‰ trình Windows (2T) ‡ 1.3. Microsoft Foundation Classes (MFC) ‡ 1.4. ኼ’ trình Windows ˜ዔ‹ VC++ 2008 (1LT + 1BT) ኼ’–”¿Š‹†‘™• 5 5
  6. 9/13/2011 1.1. д „И ˜Ч ŽН’ trình Windows ‡ Windows: ¯ዏ Šዌƒ –”ዠ quan, tài nguyên ¯ƒ †኶‰. ‡ ኮ‰ †ዙ‰ thân –Š‹ዉ ˜ዔ‹ ‰ዛዕ‹ •ዞ †ዙ‰ thông qua giao †‹ዉ ¯ዏ Šዌƒ Windows. ‡ Các ዜ‰ †ዙ‰ có giao †‹ዉ –ዛዓ‰ tác ‰‹ዎ‰ nhau: ± Windows, title bar, toolbar, menu bar, status bar ± Thanh –Šዠ ¯ዓ: File, Edit, Tool, Helpǥ ± ዒ’ –Š‘኶‹: –Šዛዕ‰ Šዜƒ các ¯‹዆— Š‹዇ chung: Edit Control, Button Control, CheckboxǥǤ ኼ’–”¿Š‹†‘™• 6 6
  7. 9/13/2011 Các thành ’ŠК лƒ г– пƒ •б ኼ’–”¿Š‹†‘™• 7 7
  8. 9/13/2011 д…ŠЦŽН’–”¿Š˜‡–-driven ኼ’–”¿Š‹†‘™• 8 8
  9. 9/13/2011 ŠØ‰¯‹Ъ’ȋ‹†‘™•‡••ƒ‰‡•Ȍ ‡ ዉ –Šዎ‰ (Windows) ghi Ž኶‹ ዑ‹ •ዠ ‹ዉ š኷› ra (event) trong ዒ– thông ¯‹ዉ’ (message) và ¯዁– trong hàng ¯ዘ‹ thông ¯‹ዉ’ (messages queue) ‡ Thông ¯‹ዉ’ phát sinh: ± ዝ Šዉ –Šዎ‰ (Windows OS) ± ዝ ዜ‰ †ዙ‰ ‡ Ví †ዙ: click/drag Š—ዒ–, ኸ phím, minimize/maximize/close ዞƒ •ዐ, thay ¯ዐ‹ kích –Šዛዔ ዞƒ •ዐǡǥ ኼ’–”¿Š‹†‘™• 9 9
  10. 9/13/2011 Windows Message (cont.) ‡ 0 ‹†‘™•¯኷Š‹ዉ ˜‹ዉ –”—›዆ –ኸ– ኷ các –ŠØ‰¯‹ዉ’…ዚƒ ዜ‰ †ዙ‰ vào các ዞƒ •ዐ khác nhau ዚƒ ዜ‰ †ዙ‰ ¯×. ‡ ዒ– Šዛዓ‰ trình Windows ’Š኷‹ Šዜƒ ዒ– hàm ¯዁ „‹ዉ– ¯዇ šዞ lý các thông ¯‹ዉ’ ‰ዌ‹ là hàm šዞ lý ዞƒ •ዐ WinProc (Š‘዁ WindowProc): ± 0ዛዘ ‰ዌ‹ khi có „ኸ– ü thông ¯‹ዉ’ ¯ዛዘ –”—›዆ ¯ዅ ዞƒ •ዐ (thông qua con –”ው hàm) ± ዞ lý các thông ¯‹ዉ’ ¯× và –”኷ ¯‹዆— Š‹዇ ˜዆ cho Šዉ –Šዎ‰. ‡ Š‹዆— thông ¯‹ዉ’ –”኷ Ž኶‹ cho Windows šዞ lý, ‰ዌ‹ hàm DefWindowProc( ) ኼ’–”¿Š‹†‘™• 10 10
  11. 9/13/2011 Windows Message (cont.) ኼ’–”¿Š‹†‘™• 11 11
  12. 9/13/2011 Windows API ‡ Application Programming Interface: Giao †‹ዉ Žኼ’ trình ዜ‰ †ዙ‰ ‡ Cung ኸ’ các hàm truy ኼ’ tài nguyên trong Šዉ –Šዎ‰ (Windows) Šዜƒ trong các –Šዛ ˜‹ዉ liên ዅ– ¯ዒ‰ DLL. ‡ Các ዜ‰ †ዙ‰ có –Š዇ truy ኼ’ ¯ዅ các hàm API ‡ Các hàm GDI (Graphics Device Interface) cho phép ዜ‰ †ዙ‰ làm ˜‹ዉ ˜ዔ‹ Š‹዆— ‹዇— –Š‹ዅ– „ዋ ¯ዏ Šዌƒ. ኼ’–”¿Š‹†‘™• 12 12
  13. 9/13/2011 Windows Data Types (1) ‡ 0ዋŠ ‰ŠÂƒ Šዕ –ዝ khóa typedef trong các –ኼ’ tin (windef.h; winbase.h, winuser.h ¯ዛዘ include trong windows.h). ‡ Ví †ዙ: ± typedef int INT; ± typedef unsigned int UINT; ± char * PSTR ‡ WPARAM (UINT) và LPARAM (LONG) ¯዆— 32 bit ‡ LRESULT: ‹዇— LONG ‡ HANDLE: ዒ–‰‹ž–”ዋ͵ʹ„‹–•ŠØ‰†ኸ—ȋ—•‹‰‡†Ȍ†‘ 0  –኶‘”ƒ¯዇Ž¯ዋŠ†ƒŠ…Š‘ͳ¯ዎ‹–ዛዘ‰ȋˆ‹Ž‡ǡ…ዞƒ•ዐǡ˜î‰ Šዔǡ menu, –‘‘Ž„ƒ”ǥȌǤÀ†ዙǣ ǡ ǡ hPrevInstance ኼ’–”¿Š‹†‘™• 13 13
  14. 9/13/2011 Windows Data Types (2) .LʀX éQJKśD HANDLE 6͑QJX\rQ-ELWÿʈQKGDQK HWND 6͑QJX\rQ-ELWÿʈQKGDQK BYTE *LiWUʈ-ELWNK{QJGɢX WORD 6͑QJX\rQ-ELWNK{QJGɢX DWORD 6͑QJX\rQ-ELWNK{QJGɢX UINT 6͑QJX\rQNK{QJGɢX-bit. LONG long 32-bit. BOOL .LʀX logic True/False LPSTR &RQWU͏FKX͗L. (char *) LPCSTR +ɮQJFRQWU͏FKX͗L WPARAM 32-bit. LPARAM 32-bit. BSTR *LiWUʈ-ELWWU͏ÿɼQNtWͱ LPVOID &RQWU͏-ELWÿɼQP͙WNLʀXNK{QJ[iFÿʈQK LPTSTR *L͑QJQKɉLPSTR QKɉQJFyWKʀFKX\ʀQVDQJGɞQJ8QLFRGHYj DBCS. LPCTSTR *L͑QJQKɉLPCTSTR QKɉQJFyWKʀFKX\ʀQVDQJGɞQJ8QLFRGHYj DBCS. ኼ’–”¿Š‹†‘™• 14 14
  15. 9/13/2011 Microsoft style (1) ‡ ኸ–…኷…ž…‹዇—†ዟŽ‹ዉ—ዖ”ዒ‰…ዚƒ™‹†‘™•ǡ–²…Žƒ••ǡ–² •–”—…–ǡ–²Šኾ‰•ዎ¯ዋŠ•ዀ¯዆—¯ዛዘ…˜‹ዅ–‹Š‘ƒ À†ዙǣ T²‹዇—†ዟŽ‹ዉ—ǣ ǡǡ ǡ ǡǡǥǤ T²…ኸ—–”ï…ǣ ǡ ǡǡǤǤǤǤ T²Šኾ‰•ዎ¯ዋŠ•ዀǣǡ  ̴  ǡ ̴ǡǥǤǤ ‡ ኸ–…኷…ž…‡–Š‘†ȋ…ž…Šǡ…ž…’Šዛዓ‰–Šዜ…Ȍ¯዆—¯ዛዘ…˜‹ዅ– hoa (pascal notation). À†ዙǣ ‘ƒ† ‘ȋǥȌ ‘ƒ†—”•‘”ȋǥȌ ‘‡–Š‡”ƒ‡ȋǥȌ ኼ’–”¿Š‹†‘™• 15 15
  16. 9/13/2011 Microsoft Style (2) ‡ Qui ዛዔ…: „ኽ– ¯ኹ— (–‹዆ –ዎ) Šዟ –Šዛዕ‰ –Š዇ Š‹ዉ ‹዇— †ዟ Ž‹ዉ— „‹ዅ. ‡ Ví †ዙ: szCmdLine là ዒ– „‹ዅ Žዛ— Š—ዑ‹ Šኼ’ –ዝ dòng ŽዉŠ, sz là –Š዇ Š‹ዉ cho „‹ዅ ‹዇— Š—ዑ‹ ዅ– thúc ký –ዠ 0, ‡ hInstance và hPrevInstance, trong ¯× h ˜‹ዅ– –ኽ– cho ‹዇— handle. ኼ’–”¿Š‹†‘™• 16 16
  17. 9/13/2011 Microsoft Style (3) Ȃ ‹Ч–Я–²„‹Ц 7LɾQW͑ .LʀXGͯOLʄX c char, WCHAR, TCHAR by BYTE n short i int x,y ELɼQOɉXW͍Dÿ͙[\ b BOOL w WORD l long dw DWORD s string sz FKX͗LNɼWWK~FE͟LNtWͱ h handle p pointer Lpsz FRQWU͏GjLFKX͗LNêWͱNɼWWK~FNtWͱ ኼ’–”¿Š‹†‘™• 17 17
  18. 9/13/2011 Microsoft style (4) - Calling convention ‡ 0ዋŠ‰ŠÂƒ‹዇—Š‰ዌ‹„ዖ‹‹†‘™• #define CALLBACK __stdcall #define WINAPI __stdcall #define APIENTRY WINAPI #define APIPRIVATE __stdcall #define PASCAL __stdcall ‡ Thêm vào –”ዛዔ tên hàm ¯዇ Šዊ ra cho compiler ”ኾ‰ hàm này có cách –ዐ Šዜ ’Šዙ ˜ዙ cho ˜‹ዉ ‰ዌ‹ ¯ዅ Šዛ –Šዅ nào. ኼ’–”¿Š‹†‘™• 18 18
  19. 9/13/2011 1.2. Й— trúc лƒ Šмд‰ trình Windows ኼ’–”¿Š‹†‘™• 19 19
  20. 9/13/2011 1.2. Й— trúc лƒ Šмд‰ trình Windows ‡ Šዛዓ‰ trình Windows ¯ዓ ‰‹኷ (dùng hàm API) ‰ዏ 2 hàm WinMain() và hàm šዞ lý ዞƒ •ዐ WinProc(). ‡ WinMain() –Šዠ Š‹ዉ các Šዜ £‰: ± Tell Windows what kind of window the program requires ± Create the program window ± Initialize the program window ± Retrieve Windows messages intended for the program ‡ WinProc(): šዞ lý các thông ¯‹ዉ’ liên quan ¯ዅ ዞƒ •ዐ ዚƒ nó. ኼ’–”¿Š‹†‘™• 20 20
  21. 9/13/2011 1.2.1. Hàm WinMain() ‡ Hàm chính ዚƒ ዜ‰ †ዙ‰ trên Windows ‡ Program entry point int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) ‡ hInstance, is a handle to the instance of the program you are writing. ‡ hPrevInstance, is used if your program had any previous instance. If not, this argument can be ignored. ‡ szCmdLine, is a string that represents all items used on the command line to compile the application. ‡ nCmdShow, controls how the window you are building will be displayed. ኼ’–”¿Š‹†‘™• 21 21
  22. 9/13/2011 Hàm WinMain (cont.) ‡ Tìm Š‹዇— hàm WinMain ኻ—: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("HelloWin"); // tên ͲQJGͮQJ HWND hwnd; MSG msg; WNDCLASS wndclass; // EL͈Q ÿ͌ ÿ͔QK danh P͢W FͶD V͞ /* ç͔QK QJKƭD NL͌X FͶD V͞ */ wndclass.style = SC_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; // Hàm WKͰ WͮF FͶD V͞ wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; // ç͔QK danh ͲQJ GͮQJ wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCusor (NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; // Không có menu 22 22
  23. 9/13/2011 Tìm Š‹Ш— hàm WinMain М— (cont.) wndclass.lpszClassName = szAppName; // tên ͲQJGͮQJ /* çăQJ ký OͤS FͶD V͞ */ if (!RegisterClass(&wndclass)) return 0; /* 7̪R WK͌ KL͐QOͤSFͶD V͞ ÿmÿ͔QKQJKƭD*/ hwnd = CreateWindow(szAppName, // Tên FͶD V͞ "Hello Program", // Tiêu ÿ͊ WS_OVERLAPPEDWINDOW, // .L͌X FͶD V͞ CW_USEDEFAULT, // 7R̪ ÿ͢ x CW_USEDEFAULT, // 7R̪ ÿ͢ y CW_USEDEFAULT, // &KL͊X U͢QJ CW_USEDEFAULT, // &KL͊X dài NULL, // &ͶD V͞ cha NULL, // Không có menu hInstance, // ç͔QK danh Ͳng GͮQJ NULL); // Tham V͚ E͞ sung /* +L͌Q WK͔ cͶD V͞ */ ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); /* Chu trình xͶ lý các thông ÿL͐S*/ while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; 23 } 23
  24. 9/13/2011 0ЬŠ ‰ŠÂƒ Žе’ пƒ •б WNDCLASS ‡ Windows cung ኸ’ ዒ– ኸ— trúc WNDCLASS (file winuser.h) ‰ዌ‹ là Žዔ’ ዞƒ •ዐ. ‡ ዔ’ này Šዜƒ các –Š—ዒ tính –኶‘ thành ዒ– ዞƒ •ዐ typedef struct _WNDCLASS { UINT style; WNDPROC lpfnWndProc; int cbClsExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCSTR lpszMenuName; LPCSTR lpszClassName; } WNDCLASS, *PWNDCLASS; ኼ’–”¿Š‹†‘™• 24 24
  25. 9/13/2011 0ЬŠ ‰ŠÂƒ Žе’ пƒ •б WNDCLASS ‡ Ý ‰ŠÂƒ –Š—ዒ tính Žዔ’ ዞƒ •ዐ WNDCLASS: 7KX͙FWtQK éQJKśD Ghi chú .ɼWKͣSQKLɾXNLʀXJLi style .LʀXO͛S WUʈNKiFQKDXEɮQJ WRiQWͭ25 lpfnWndProc &RQWU͏ÿɼQWKͧWͥF[ͭ OêFͭDV͕ 6͑E\WHÿɉͣFFɢSSKiWWKrPVDX cbClsExtra 0ɴFÿʈQK FɢXWU~FZLQGRZ-class 6͑E\WHÿɉͣFFɢSSKiWWKrPVDX cbWndExtra 0ɴFÿʈQK P͙WLQVWDQFHFͧDZLQGRZ hInstance ĈʈQKGDQKFKͩDWKͧWͥFFͭDV͕ FͧDO͛SZLQGRZ hIcon ĈʈQKGDQKFͧDELʀXWɉͣQJ Dùng hàm LoadIcon hCursor ĈʈQKGDQKFͧDFRQWU͏FKX͙W Dùng hàm LoadCursor Dùng hàm hbrBackground ĈʈQKGDQKFͧDFK͕LW{QɾQ GetStockObject 7rQWKͱFÿɇQJɬQY͛L FͭDV͕WKͱFÿɇQnày lpszMenuName 7rQWKͱFÿɇQ ÿɉͣFNKDLEiRWURQJ WɪSWLQWjLQJX\rQ lpszClassName 7rQO͛S 25 25
  26. 9/13/2011 0£‰ ký Žе’ пƒ •б ‡ Sau khi ¯ዋŠ ‰ŠÂƒ Žዔ’ ዞƒ •ዐ ’Š኷‹ ¯£‰ ký „ኾ‰ hàm RegisterClass: ATOM RegisterClass(CONST WNDCLASS * lpWndClass); ኼ’–”¿Š‹†‘™• 26 26
  27. 9/13/2011 З‘ пƒ •б ‡ ዔ’ ዞƒ •ዐ –Š዇ Š‹ዉ ¯዁ tính chung ዞƒ •ዐ. Cho phép –኶‘ Š‹዆— –Š዇ Š‹ዉ ዞƒ •ዐ ዚƒ Žዔ’ ‡ Dùng hàm CreateWindow, Šዊ ¯ዋŠ các ¯዁ tính riêng ዚƒ ዞƒ •ዐ, và phân „‹ዉ– nó ˜ዔ‹ các ዞƒ •ዐ khác –኶‘ ra cùng ዒ– Žዔ’. HWND CreateWindow(LPCSTR lpClassName, // Tên OͤS cͶD V͞ ÿm ÿăQJ ký LPCSTR lpwindowName, // Tên FͰD FͶD V͞ DWORD dwStyle, // .L͌X FͰD cͶD V͞ int x, // 9͔ trí ngang ban ÿ̰X int y, // 9͔ trí G͖F ban ÿ̰X int nWidth, // ç͢ U͢QJ ban ÿ̰X int nHeight, // ç͢ cao ban ÿ̰X HWND hWndParent, // ç͔QK danh FͰD cͶa V͞ cha MENU hMenu, // ç͔QK dang FͰD thͺc ÿ˱Q INSTANCE hInstance, // ç͔QK danh WK͌ KL͐Q Ͳng GͮQJ PVOID lpParam // Các tham V͚ ban ÿ̰X ); ኼ’–”¿Š‹†‘™• 27 27
  28. 9/13/2011 ‹Ш –ŠЬ пƒ •б ‡ Sau khi –኶‘ ዞƒ •ዐ ¯ዋŠ danh ዞƒ •ዐ HWND ‡ 0዇ š—ኸ– Š‹ዉ ዞƒ •ዐ ‰ዌ‹ hàm ShowWindow BOOL ShowWindow(HWND hWnd, // ç͔QK danh cͰD cͶD V͞ F̰Q WK͌ KL͐Q int iCmdShow // 7U̪QJ thái KL͌Q WK͔ ); ‡ ዒ– •ዎ –”኶‰ thái Š‹዇ –Šዋ (iCmdShow): SW_HIDE: ኌ ዞƒ •ዐ. SW_MAXIMIZE: Phóng ዞƒ •ዐ ra toàn „ዒ màn hình. SW_MINIMIZE: Thu Šው thành „‹዇— –ዛዘ‰ trên màn hình. SW_RESTORE: ‹዇ –Šዋ †ዛዔ‹ †኶‰ Š—ኺ. ኼ’–”¿Š‹†‘™• 28 28
  29. 9/13/2011 1.2.2. Hàm šп lý thông ¯‹Ъ’ WinProc ‡ Có –Š዇ có Š‹዆— Šዓ ዒ– hàm šዞ lý thông ¯‹ዉ’ ‡ Luôn ¯‹ kèm ዞƒ •ዐ ዚƒ nó ‡ Giao –‹ዅ’ ˜ዔ‹ Šዉ –Šዎ‰ (Windows), toàn „ዒ các thông ¯‹ዉ’ ‰ዞ‹ –ዔ‹ ¯ዛዘ šዞ lý qua hàm này ‡ Šዛዕ‰ khai báo Šዛ sau: LRESULT CALLBACK WinProc (HWND, UINT, WPARAM, LPARAM); ‡ Trong ¯× tham •ዎ ¯ኹ— tiên là ¯ዋŠ danh ዚƒ ዞƒ •ዐ, tham •ዎ –Šዜ 2 là ¯ዋŠ danh thông ¯‹ዉ’, và —ዎ‹ cùng là 2 tham •ዎ WPARAM và LPARAM „ዐ sung thông tin kèm theo thông ¯‹ዉ’. ኼ’–”¿Š‹†‘™• 29 29
  30. 9/13/2011 Message Loop ‡ Windows duy trì ዒ– hàng ¯ዘ‹ thông ¯‹ዉ’ cho ዑ‹ Šዛዓ‰ trình ‡ Khi •ዠ ‹ዉ Šኼ’ thông tin š—ኸ– Š‹ዉ, Windows †ዋ…Š •ዠ ‹ዉ thành †኶‰ thông ¯‹ዉ’ và ¯ዛƒ vào hàng ¯ዘ‹ thông ¯‹ዉ’ ዚƒ ዜ‰ †ዙ‰ –ዛዓ‰ ዜ‰. ‡ ኮ‰ †ዙ‰ Šኼ thông ¯‹ዉ’ –ዝ hàng ¯ዘ‹ „ኾ‰ ¯‘኶ mã: MSG msg; // Windows message structure while (GetMessage(&msg, NULL, 0,0)) { TranslateMessage (&msg); DispatchMessage (&msg); } ኼ’–”¿Š‹†‘™• 30 30
  31. 9/13/2011 Message Loop ‡ Trong ¯× msg là ዒ– „‹ዅ ኸ— trúc ‹዇— MSG ¯ዛዘ ¯ዋŠ ‰ŠÂƒ trong winuser.h typedef struct tagMSG { HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; } MSG, *PMSG; ‡ POINT là ‹዇— –ዌƒ ¯ዒ ¯‹዇, ¯ዋŠ ‰ŠÂƒ trong windef.h typedef struct tagPOINT { LONG x; LONG y; } POINT, *PPOINT; 31 31
  32. 9/13/2011 Message Loop ‡ Hàm GetMessage •ዄ –”኷ ˜዆ 0 ዅ— msg Šዜƒ thông ¯‹ዉ’ có ¯ዋŠ danh WM_QUIT (0x0012), khi ¯× vòng Ž዁’ thông ¯‹ዉ’ ‰ዛ‰ và ዜ‰ †ዙ‰ ዅ– thúc. ‡ ‰ዛዘ Ž኶‹ thì hàm •ዄ –”኷ ˜዆ ዒ– giá –”ዋ khác 0 ˜ዔ‹ các thông ¯‹ዉ’ khác. ኼ’–”¿Š‹†‘™• 32 32
  33. 9/13/2011 Multitasking ኼ’–”¿Š‹†‘™• 33 33
  34. 9/13/2011 Hàm šп lý thông ¯‹Ъ’ WinProc - À†к ‡ Tìm Š‹዇— ዒ– hàm šዞ lý ዞƒ •ዐ: LRESULT CALLBACK WinProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; /*XͶ lý các thông ÿL͐S F̰Q WKL͈W YͤL Ͳng GͮQJ*/ switch (msg){ case WM_CREATE: /*9L͈W ÿR̪Q mã khi W̪R cͶD V͞*/ return 0; case WM_PAINT: /*9L͈W ÿR̪Q mã khi tô Y͆ O̪L cͶD V͞*/ hdc = BeginPaint (hwnd, &ps); GetClientRect (hwnd, &rect); ኼ’–”¿Š‹†‘™• 34 34
  35. 9/13/2011 DrawText(hdc, "Hello", -1, &rect, DT_SINGLELINE| DT_CENTER| DT_VCENTER); EndPaint(hwnd, &ps); return 0; case WM_SIZE: /*9L͈W ÿR̪Q mã khi kích th˳ͤF cͶD V͞ thay ÿ͞L*/ return 0; case WM_DESTROY: /*CͶa V͞ E͔ ÿyQJ*/ PostQuitMessage (0); return 0; } return DefWindowProc (hwnd, msg, wParam, lParam); } ‡ Thông –Šዛዕ‰ Šዊ Š዁ ¯዇ šዞ lý các thông ¯‹ዉ’ ኹ –Š‹ዅ–, còn Ž኶‹ giao cho hàm šዞ lý ዁ ¯ዋŠ DefWindowProc 35 35
  36. 9/13/2011 ׏–О– ኼ’–”¿Š‹†‘™• 36 36
  37. 9/13/2011 1.2.3. Šмд‰ trình Windows ¯д ‰‹И - Šዛዓ‰–”¿Šǣ ‡ŽŽ‘‘”Ž†ȋዞ†ዙ‰–”ዠ…–‹ዅ’ Windows API) - 2 hàm chính -Hàm WinMainǣ–Šዠ…Š‹ዉ…ž……Šዜ…£‰ - Šƒ‹„ž‘Žዔ’…ዞƒ•ዐ - 0£‰ýŽዔ’…ዞƒ•ዐ˜ዝƒŠƒ‹„ž‘ - ኶‘˜Š‹዇–ŠዋŽዔ’…ዞƒ•ዐ - ֐‰Ž዁’Šኼ–ŠØ‰¯‹ዉ’˜‰ዞ‹–ዔ‹–Šዚ–ዙ…šዞŽý -Hàm WinProcǣ šዞŽý–ŠØ‰¯‹ዉ’‰ዞ‹¯ዅǤž… –ŠØ‰¯‹ዉ’¯ዛዘ…‹†‘™•šዞŽý዁…¯ዋŠ„ኾ‰Š DefWindowProc() ኼ’–”¿Š‹†‘™• 37 37
  38. 9/13/2011 Win32 Application in Visual C++ Developer Studio ኼ’–”¿Š‹†‘™• 38 38
  39. 9/13/2011 HelloWorld (1) /* HELLOWORLD.C */ #include LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName [] = TEXT ("HelloWorld"); 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); wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass (&wndclass)) { MessageBox(NULL, TEXT (" The program requires Windows"), szAppName, MB_ICONERROR); 39 return 0; 39
  40. 9/13/2011 HelloWorld (2) hwnd = CreateWindow(szAppName, // Tên OͤS FͶD V͞ TEXT (" The Hello World Program"), // Tiêu ÿ͊ FͶD V͞ WS_OVERLAPPEDWINDOW, // .L͌X cͶa V͞ CW_USEDEFAULT, // 7R̪ ÿ͢ x CW_USEDEFAULT, // 7R̪ ÿ͢ y CW_USEDEFAULT, // &KL͊X ngang CW_USEDEFAULT, // &KL͊X G͖F NULL, // CͶD V͞ cha NULL, // ThͺF ÿ˱Q hInstance, // ç͔QK danh NULL); // Tham V͚ ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } // End WinMain ኼ’–”¿Š‹†‘™• 40 40
  41. 9/13/2011 HelloWorld (3) LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; switch (msg) { case WM_CREATE: return 0; case WM_PAINT: hdc = BeginPaint (hwnd, &ps); GetClientRect (hwnd, &rect); DrawText(hdc, TEXT("Hello World"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint (hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0; } // End switch return DefWindowProc (hwnd, msg, wParam, lParam); } ኼ’–”¿Š‹†‘™• 41 41
  42. 9/13/2011 HelloWorld (run) ኼ’–”¿Š‹†‘™• 42 42
  43. 9/13/2011 Ý ‰ŠÂƒ các hàm trong HelloWorld Tên hàm éQJKśD LoadIcon 1ɞSP͙WELʀXWɉͣQJÿʀVͭGͥQJWURQJFKɉɇQJWUuQK LoadCursor 1ɞSP͙WFRQWU͏FKX͙WFKRFKɉɇQJWUuQK 1KɪQP͙Wÿ͑LWɉͣQJÿ͓K͍DWURQJWUɉ͝QJKͣSFͧD GetStockObject FKɉɇQJWUuQKWKuOɢ\P͙WFK͕LW{ÿʀW{OɞLQɾQFͧDFͭD V͕ RegisterClass ĈăQJNêP͙WO͛SFͭDV͕FKRFͭDV͕ͩQJGͥQJWURQJ FKɉɇQJWUuQK MessageBox +LʀQWKʈP͙WWK{QJÿLʄS CreateWindow 7ɞRP͙WFͭDV͕GͱDWUrQP͙WO͛SFͭDV͕ ShowWindow +LʀQWKʈFͭDV͕OrQPjQKuQK UpdateWindow <rXFɤXFͭDV͕YɺOɞLFKtQKEɠQWKkQQy GetMesssage 1KɪQP͙WWK{QJÿLʄSWͫKjQJÿͣLWK{QJÿLʄS TranslateMessage 'ʈFKWK{QJÿLʄSEjQSKtP DispatchMessage *͟LWK{QJÿLʄSÿɼQKjP[ͩOêFͭDV͕ BeginPaint .K͟LWɞRFKͩFQăQJYɺFͧDFͭDV͕ GetClientRect /ɢ\KuQKFKͯQKɪWOɉXYQJOjPYLʄF DrawText +LʀQWKʈP͙WFKX͗LYăQEɠQ EndPaint .ɼWWK~FYLʄFYɺFͭDV͕ PostQuitMessage ĈɉDWK{QJÿLʄSWKRiWYjRKjQJÿͣLWK{QJÿLʄS DefWindowProc 7KͱFKLʄQYLʄF[ͭOêPɴFÿʈQKFiFWK{QJÿLʄS 43 43
  44. 9/13/2011 1.3. Microsoft Foundation Classes (MFC) ‡ MFC = Microsoft Foundation Classes ‡ Šዛ˜‹ዉ…ž…Žዔ’¯×‰‰×‹…ž…Š‹͵ʹ API ‡ ኼ’ Šዘ’ các Žዔ’ C++ (methods, members) và hàm toàn ዙ ‡ Phát –”‹዇ ዜ‰ †ዙ‰ Windows †ወ dàng, rút ‰ኽ –Šዕ‹ gian ȋ•‘˜ዔ‹Žኼ’–”¿Š‹†‘™• API). ‡ Program user interface ኼ’–”¿Š‹†‘™• 44 44
  45. 9/13/2011 MFC và Win32 API ‡ Win32 API: –ኼ’ Šዘ’ các hàm, ኸ— trúc, thông ¯‹ዉ’, macros và interfaces ± Cho phép phát –”‹዇ ዜ‰ †ዙ‰ Win32 platforms. ± Program low-level, tiêu –ዎ –Šዕ‹ gian. ‡ MFC: ± Tái •ዞ †ዙ‰ code (reuse) và phát –”‹዇ các –Šዛ ˜‹ዉ. ± 0ዓ ‰‹኷ hóa và –£‰ –ዎ phát –”‹዇ ዜ‰ †ዙ‰ „ኾ‰ cách cung ኸ’ ዒ– –ኼ’ các Žዔ’ C++ ¯×‰ gói các ¯዁ ¯‹዇ ዚƒ Win32 API ኼ’–”¿Š‹†‘™• 45 45
  46. 9/13/2011 MFC và Win32 API Ȃ Ví †к ‡ Khái ‹ዉ Win32 ዚƒ ዒ– ዞƒ •ዐ (window) ¯ዛዘ ¯×‰ gói „ዖ‹ Žዔ’ CWnd trong MFC. ‡ CWnd Šዜƒ 1 „‹ዅ thành viên ‹዇— HWND (‹዇— †ዟ Ž‹ዉ— ¯ዋŠ ‰ŠÂƒ Win32 „‹዇— †‹ወ ዒ– ዞƒ •ዐ). ‡ CWnd Šዜƒ các ’Šዛዓ‰ –Šዜ ¯×‰ gói ‰ዌ‹ ¯ዅ các hàm Win32 mà dùng HWND Šዛ 1 tham •ዎ. ‡ Ví †ዙ: ± Hàm: BOOL ShowWindow(HWND hWnd, int nCmdShow); ± 0ዛዘ ¯×‰ gói trong MFC: BOOL CWnd::ShowWindow(int nCmdShow); ኼ’–”¿Š‹†‘™• 46 46
  47. 9/13/2011 MFC Application Framework ‡ Ngoài ˜‹ዉ ¯×‰ gói ዚƒ Win32 API, MFC còn ¯ዋŠ ‰ŠÂƒ ዒ– nhóm các Žዔ’ ¯዇ „‹዇— „‹ወ các object ዜ‰ †ዙ‰ chung và –Š‹ዅ– Žኼ’ ዎ‹ quan Šዉ trong nhóm này ¯዇ –Šዠ thi các hành vi ዚƒ ዜ‰ †ዙ‰. ‡ Application Framework: ዒ– ዆ –኷‰ ¯዇ xây †ዠ‰ các ዜ‰ †ዙ‰. ‡ Dùng MFC Wizard ¯዇ –኶‘ ra –ኼ’ Šዘ’ các Žዔ’ †ኻ š—ኸ– –ዝ các Žዔ’ ዚƒ framework. ‡ ‘዁ xây †ዠ‰ các Žዔ’ ዔ‹ theo yêu ኹ— •ዞ †ዙ‰ ኼ’–”¿Š‹†‘™• 47 47
  48. 9/13/2011 ኼ’–”¿Š‹†‘™• 48 48
  49. 9/13/2011 Й—–”ï…н‰†к‰  ‡ ኮ‰ †ዙ‰ MFC cung ኸ’: ± ዒ– Žዔ’ „‹዇— †‹ወ ዜ‰ †ዙ‰ȋApplication Class) ± ዒ–Žዔ’ „‹዇— †‹ወ ዞƒ •ዐ chính ዚƒ ዜ‰ †ዙ‰ (Main Window) ± ”‹዇Šƒ‹Š‹ƒ‹ȋȌȋ¯ዛዘ…ኺ¯‹Ȍ (WinMain() Function) ኼ’–”¿Š‹†‘™• 49 49
  50. 9/13/2011 1.4. Н’ trình Windows ˜е‹ VC++ 2008 ‡ ͵…ž…Š–኶‘ዜ‰†ዙ‰‹†‘™•˜ዔ‹ΪΪǣ ± ኮ‰†ዙ‰•ዞ†ዙ‰–”ዠ…–‹ዅ’‹†‘™• (Win32 Project) ± ኮ‰†ዙ‰•ዞ†ዙ‰  (MFC Application) ± ኮ‰†ዙ‰‹†‘™• ‘” (CLR/Windows Form Application) ኼ’–”¿Š‹†‘™• 50 50
  51. 9/13/2011 Môi –”мж‰ phát –”‹Ш –À…ŠŠй’ΪΪȋʹͲͲͺȌ IDE: ‡ The Solution (Workspace) ‡ Output Pane ‡ Editor Area ‡ Menu Bar ‡ Rearranging on the IDE ኼ’–”¿Š‹†‘™• 51 51
  52. 9/13/2011 VC++ 2008 Developer Studio Environment -Solution ToolBox Explorer -Class View -Resource View Editor Area Properties Output ኼ’–”¿Š‹†‘™• 52 52
  53. 9/13/2011 The Solution ‡ Solution Explorer: allows you to view and navigate all the files that make up your application. ‡ Class View allows you to navigate and manipulate your source code on a C++ class level. ‡ Resource View: allows you to find and edit each of the various resources in your application, including dialog window designs, icons, and menus. ኼ’–”¿Š‹†‘™• 53 53
  54. 9/13/2011 Output Pane & Editor Area ‡ Output Pane: ± Cung ኸ’ thông tin cho ‰ዛዕ‹ Žኼ’ trình ± Compiler progress statements ± Warnings, error messages ± Thông tin debugger: ± —””‡–˜ƒŽ—‡•‘ˆ˜ƒ”‹ƒ„Ž‡•™Š‡•–‡’–Š”‘—‰Š…‘†‡Ǥȋዞƒ •ዐƒ–…ŠȌ ‡ Editor Area: ± ‘኶–Š኷‘ ŽዉŠ ± Š‹ዅ–ዅ‰‹ƒ‘†‹ዉȋ‡•‹‰‘†‡Ȍ ± Icon Editor ኼ’–”¿Š‹†‘™• 54 54
  55. 9/13/2011 Menu Bar + Rearranging IDE ‡ Menu bar + Tool bars ‡ Standard toolbar ‡ Build toolbar ‡ Text Editor ‡ ǥǤ ‡ Add/remove Toolbars ‡ ±‘–Š኷ǡ„ዎ–”À…ž……ዞƒ•ዐŽ˜‹ዉ ‡ Ž‘ƒ–‹‰ǡ‘…ǡ—–‘Š‹†‡ǡǥǤ ኼ’–”¿Š‹†‘™• 55 55
  56. 9/13/2011 ž…–ŠŠ’ŠКн‰†к‰’Šž––”‹Ш˜е‹ΪΪ 56 56
  57. 9/13/2011 Character Sets, và _T macro (1) ‡ Ž†‹†‘™•ǣኼ’ý–ዠ ͺ„‹– ‡ ‹†‘™•ǣኼ’ý–ዠ‹…‘†‡ͳ͸„‹–ȋ„ƒ‘ trùm ANSI). ‡ ‹²†ዋ…Šዜ‰†ዙ‰ΪΪ–”²‹†‘™• ×–Š዇…Šዌ•ዞ†ዙ‰‹…‘†‡Š‘዁…ŠØ‰Ǥ ‡ ዅ—ዒ–…Š—ዑ‹Dz ‡ŽŽ‘dz–Š¿–”¿Š„‹²†ዋ…Š•ዄ –Š዇Š‹ዉ†኶‰ ȋͺ„‹–Ȁͳý–ዠȌǤ ‡ ዅ—…Š—ዑ‹dz ‡ŽŽ‘dz–Š¿–”¿Š„‹²†ዋ…Š•ዄ–Š዇ Š‹ዉ†኶‰…Š—ዑ‹‹…‘†‡Ǥ ኼ’–”¿Š‹†‘™• 57 57
  58. 9/13/2011 Character Sets, và _T macro (2) ‡ ƒ…”‘̴ȋ…ዚƒ Ȍ…Š‘…Š—ዑ‹†኶‰ ̴ȋDz ‡ŽŽ‘dzȌ–Š¿ዅ–“—኷•ዄ¯ዛዘ…–Š዇Š‹ዉ†኶‰ ‹…‘†‡ዅ—ýŠ‹ዉ—–‹዆šዞŽý̴  ¯ዛዘ…¯ዋŠ‰ŠÂƒȋ…Šዌ‹዇—…Š—ዑ‹ý–ዠ ‹…‘†‡…Š‘ዜ‰†ዙ‰Ȍǡዅ—ŠØ‰዁…¯ዋŠ Žý–ዠ Ǥ 58 58
  59. 9/13/2011 Character Sets và _T macro (3) ‡ ‹዇—TCHAR ȋ†î‰–Šƒ›…Š‘‹዇—…Šƒ”Ȍǣ ± ™…Šƒ”̴–ȋ‹…‘†‡ͳ͸„‹–Ȍዅ—̴ ¯ዛዘ…¯ዋŠ ‰ŠÂƒ ± …Šƒ”ȋ ͺ„‹–Ȍዅ—̴ ŠØ‰¯ዛዘ…¯ዋŠ ‰ŠÂƒ ‡ ዛዓ‰–ዠǡ²†î‰TCHAR* Š‘዁…LPTSTR (con –”ው…Š—ዑ‹Ȍ–Šƒ›…Š‘™…Šƒ”̴–ȗǡŠ‘዁……Šƒ”ȗǤ ‡ LPCTSTR: ‘–”ው–ዔ‹…Š—ዑ‹Šኾ‰ ‡ ዞ†ዙ‰•‹œ‡‘ˆȋ Ȍ¯዇šž…¯ዋŠ¯ዒ”ዒ‰ý–ዠ (8 hay 16 bit) ‡ ዞ†ዙ‰…ž…ƒ…”‘–”‘‰–Šዛ˜‹ዉ–…Šƒ”ǤŠ–Šƒ›…Š‘ ž…ŠšዞŽýý–ዠȀ…Š—ዑ‹…ዚƒ–Šዛ˜‹ዉ-runtime. À†ዙǣ̴–…•…’›–Šƒ›…Š‘Š•–”…’›ȋȌ 59 59
  60. 9/13/2011 Exercises Chapter 1. ‡ Ex1-1. HelloWorld ± ኶‘’”‘Œ‡…–‹͵ʹ”‘Œ‡…–Šዛ˜À†ዙŽý–Š—›ዅ– ± ”‹዇Šƒ‹Š‹ƒ‹ǡ‹”‘…ǡ…ዓ…Šዅ–ŠØ‰ ¯‹ዉ’ ‡ Ex1-2. HelloMFC ± ¿Š‹዇—•ዞ†ዙ‰ ‹•—ƒŽ•–—†‹‘ ± ኶‘’”‘Œ‡…– ƒ’’ŽŽ‹…ƒ–‹‘ǡ•ዞ†ዙ‰‹œƒ”† ± Code view, design view ± Compile, debug, run ኼ’–”¿Š‹†‘™• 60 60
  61. 9/13/2011 Create a project Tên ዜ‰ †ዙ‰ ኼ’–”¿Š‹†‘™• 61 61
  62. 9/13/2011 п†к‰‹œƒ”† &KӑQNLӇXӭQJ GөQJ 6ӱGөQJWKѭYLӋQ Unicode ኼ’–”¿Š‹†‘™• 62 62
  63. 9/13/2011 Š‹Ц–Ц‰‹ƒ‘†‹Ън‰†к‰ȋ‹ƒŽ‘‰Ȍ ኼ’–”¿Š‹†‘™• 63 63
  64. 9/13/2011 Add code to greeting (1) ‡ —––‘Dz ‡ŽŽ‘dzǣ‹‰Š–‘—•‡Ȁ††˜‡– ƒ†Ž‡” Šዌ •ዠ ‹ዉ, tên hàm šዞ lý ኼ’–”¿Š‹†‘™• 64 64
  65. 9/13/2011 Add code to greeting (2) Ví †ዙ ‰ዌ‹ Šዒ’ –Š‘኶‹ thông báo void CHelloMFCDlg::OnBnClickedButtonHello() { // TODO: Add your control notification handler code here MessageBox(L"Xin chào ! ኼ’ trình Windows", L"First MFC Application"); } ኼ’–”¿Š‹†‘™• 65 65
  66. 9/13/2011 Compile, Build, Debug, Run Compile (Ctrl+F7), Build (F6), Debug (F5), Run without Debug (Ctrl+F5) Set Breakpoint (F9) and Run Debug 66
  67. 9/13/2011 Šмд‰ 2. Н’ trình Windows VC++/MFC ‡ ʹǤͳǤŠš኶–ŠØ‰¯‹ዉ’–”‘‰  ‡ 2.2. Document/View trong MFC ‡ 2.3. Menu và Toolbar ‡ 2.4. ዞ lý ˜ዄ (Drawing) ‡ ʹǤͷǤ ‹ƒ‘–‹ዅ’„’ŠÀȀ…Š—ዒ– ‡ 2.6. ዒ’ –Š‘኶‹ (Dialogs) ‡ 2.7. ዞ †ዙ‰ các ¯‹዆— Š‹዇ (controls) ኼ’–”¿Š‹†‘™• 67 67
  68. 9/13/2011 2.1. Message Map trong MFC (1) ‡ ኼ’–”¿Š …Š‘’Š±’˜‹ዅ–…ž…Š¯዇šዞŽý…ž… –ŠØ‰¯‹ዉ’ȋ‡••ƒ‰‡•Ȍ‘‰—ዎǤȋ‰‘‹…ž…šዞ Žý዁…¯ዋŠ¯ዛዘ… …—‰…ኸ’ȌǤ ‡ ዌ‹Ž…ž…message handlers ȋŠ‘዁ handlers). ȋ…ñ‰ŽŠ–ŠŠ˜‹²…ዚƒŽዔ’˜¯ዛዘ…’ŠŸ„‹ዉ– „ኾ‰–‹዆–ዎƒˆš̴•‰ȌǤ ‡ ዓ…ŠዅŽ‹²ዅ–‰‹ዟƒ–ŠØ‰¯‹ዉ’˜ŠšዞŽý¯ዛዘ… šž…¯ዋŠ„ዖ‹Message Map ȋŠš኶–ŠØ‰¯‹ዉ’Ȍ ኼ’–”¿Š‹†‘™• 68 68
  69. 9/13/2011 2.1. Message Map trong MFC (2) ‡ ዑ‹Žዔ’…ךዞŽý–ŠØ‰¯‹ዉ’•ዄ–”‹዇Šƒ‹ዒ– ‡••ƒ‰‡ƒ’Ǥዞ†ዙ‰…ž…ƒ…”‘ǣ ± DECLARE_MESSAGE_MAP() trong file .h ± ዁’ ̴ ̴ȋȌ˜̴ ̴ȋȌ trong file .cpp ȋ዁’›„ƒ‘ዒ–†ƒŠ•ž…Š…ž…ŠšዞŽý–ዛዓ‰ዜ‰˜ዔ‹ ž…–ŠØ‰¯‹ዉ’Ȍ ኼ’–”¿Š‹†‘™• 69 69
  70. 9/13/2011 2.1. Message Map trong MFC (3) ‡ ±–ዒ–˜À†ዙዜ‰†ዙ‰ ¯ዓ‰‹኷ǣ ኼ’–”¿Š‹†‘™• 70 70
  71. 9/13/2011 Message Map trong MFC (2) ኼ’–”¿Š‹†‘™• 71 71
  72. 9/13/2011 —ž–”¿ŠšпŽý–ŠØ‰¯‹Ъ’–”‘‰  ኼ’–”¿Š‹†‘™• 72 72
  73. 9/13/2011 ŠŸŽ‘З‹–ŠØ‰¯‹Ъ’–”‘‰  Message Description Category Windows Thông ¯‹ዉ’…Š—ኺ…ዚƒ‹†‘™•ǡ„ኽ–¯ኹ—˜ዔ‹̴ Messages ȋ‰‘኶‹–”ዝ…ž…–ŠØ‰¯‹ዉ’̴Ȍ À†ዙǣ̴ ǡ̴ǡǥ Control Các –ŠØ‰¯‹ዉ’̴¯ዛዘ…‰ዞ‹–ዝ…ž…¯‹዆— Notification Š‹዇ȋ‘–”‘Ž•ȌŠዛ—––‘ǡŠ‡…‘šǡǥ¯ዅ…ዞƒ•ዐ Messages Šዜƒ…Šï‰ǡŠ‘዁…–ዝ…ዞƒ•ዐ…‘¯ዅ…ዞƒ•ዐ…ŠƒǤ Šƒ•ዎ¯‹°ȋ •Ȍ…Š‘„‹ዅ–‰ዞ‹–ዝ…‘–”‘Ž•‘ Command ñ‰ Ž…ž…–ŠØ‰¯‹ዉ’̴Šዛ‰¯ዛዘ… Messages –ዝ…ž…–ŠŠ’Šኹ‰‹ƒ‘†‹ዉ‰ዛዕ‹†î‰Šዛ‡— ‹–‡•ǡ–‘‘Ž„ƒ”„—––‘•Ǥȋ0ዛዘ… ¯ዋŠ‰ŠÂƒ…ž… • Š—ኺǡ‰ዛዕ‹†î‰…×–Š዇–኶‘–Š²Ȍ ኼ’–”¿Š‹†‘™• 73 73
  74. 9/13/2011 Add Message Handlers ‡ ×–Š዇Ž–Šዚ…ؐ‰ ȋƒ—ƒŽȌȋ‹ዅ––”‘‰ Message Map). ‡ ‘዁……Šዌ Properties/Message BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWndEx) ON_WM_CREATE() ON_COMMAND(ID_WINDOW_MANAGER, &CMainFrame::OnWindowManager) ON_COMMAND(ID_VIEW_CUSTOMIZE, &CMainFrame::OnViewCustomize) ON_REGISTERED_MESSAGE(AFX_WM_CREATETOOLBAR, &CMainFrame::OnToolbarCreateNew) ON_WM_CHAR() END_MESSAGE_MAP() ኼ’–”¿Š‹†‘™• 74 74
  75. 9/13/2011 2.2. Document/View trong MFC ‡ ‘…—‡–ǣኼ’Šዘ’†ዟŽ‹ዉ—…ዚƒዜ‰†ዙ‰ ȋ†ኻš—ኸ––ዝŽዔ’‘…—‡–Ȍ ‡ ‹‡™ǣዓ…Šዅ Š‹዇–Šዋ†ዟŽ‹ዉ— trong Document ȋ†ኻš—ኸ––ዝŽዔ’ Cview) ኼ’–”¿Š‹†‘™• 75 75
  76. 9/13/2011 Document/View trong MFC ‡ ×–Š዇Žዠƒ…Šዌ‘…—‡– –‡”ˆƒ…‡•ǣ ±  ȋ‹‰Ž‡‘…—‡– –‡”ˆƒ…‡Ȍǣ ‹዇–Šዋዒ––‹ Ž‹ዉ—ȋ‘…—‡–Ȍዑ‹Žኹ–”²ዒ–…ዞƒ•ዐȋ˜‹‡™Ȍ ± MDI (Multiple Document Interface): Cho phép ዖŠ‹዆—–‹Ž‹ዉ—ዒ–Žï ‡ 0዆—¯ዛዘ…Šዘ’–ŠŠ„ዖ‹…ž…–ŠŠ’Šኹ CDocument và Cview ‡ ×–Š዇–኶‘ዜ‰†ዙ‰ ‹዇—‹ዅ–”ï… SDI/MDI ኼ’–”¿Š‹†‘™• 76 76
  77. 9/13/2011 Š—‰н‰†к‰†З‰ ‡ Šዊዖዒ––‹Ž‹ዉ— ኼ’–”¿Š‹†‘™• 77 77
  78. 9/13/2011 Š—‰н‰†к‰†З‰ ‡ Š‘’Š±’ዖŠ‹዆—–‹Ž‹ዉ— ኼ’–”¿Š‹†‘™• 78 78
  79. 9/13/2011 Џ‰†к‰ „ƒ•‡†‘…—‡–Ȁ‹‡™ ‡ ”‘‰“—ž–”¿Š–኶‘ዜ‰†ዙ‰†኶‰ ˜  ǡ…ž……Žƒ••¯ዛዘ…–኶‘”ƒŠኹ—Šዅ–¯ዛዘ…ዅ –Šዝƒȋ†ኻš—ኸ–Ȍ–ዝ…ž Žƒ••Šዛǣ ± ዔ‹†኶‰ : CWinApp, CFrameWnd, CDocument, CView ± ዔ‹†኶‰ : CWinApp, CMDIFrameWnd, CMDIChildWnd, CDocument, CView ኼ’–”¿Š‹†‘™• 79 79
  80. 9/13/2011 Џ‰†к‰ „ƒ•‡†‘…—‡–Ȁ‹‡™ ‡ ‹዇— 80 80
  81. 9/13/2011 Mô –И vai trò các class ኼ’–”¿Š‹†‘™• 81 81
  82. 9/13/2011 Й— trúc Document/View †З‰ SDI ኼ’–”¿Š‹†‘™• 82 82
  83. 9/13/2011 Й— trúc Document/View †З‰ MDI ኼ’–”¿Š‹†‘™• 83 83
  84. 9/13/2011 с –мд‰ tác ‰‹рƒ Document và View ‡ ዔ‹…኷ʹ†኶‰ ˜ ǡ•ዠŽ‹²ዅ––Š዇Š‹ዉ–ŠØ‰ “—ƒͳ„‹ዅ’‘‹–‡”‹዇—¯ዎ‹–ዛዘ‰Žዔ’CDocument ȋ„‹ዅ›–Šዛዕ‰…×–²Ž’‘…ȌǤŠØ‰“—ƒ„‹ዅ ›ǡ…×–Š዇¯‹዆—Š‹዇–Šƒ‘–ž……Š—›዇–ŠØ‰–‹ȋ–ዝ Žዔ’‹‡™•ƒ‰‘…—‡–ȌŠƒ›Žኸ›–ŠØ‰–‹˜዆ ȋ–ዝŽዔ’‘…—‡–•ƒ‰Žዔ’‹‡™ȌǤ ‡ ŠØ‰–Šዛዕ‰ǡ˜ዔ‹…኷ዜ‰†ዙ‰†኶‰ Šƒ›  –Š¿–”‘‰Žዔ’‹‡™Ž—ؐ…׊GetDocument ŠኾŠዑ–”ዘ˜‹ዉ…Žኸ›„‹ዅ…‘–”ው›Ǥ ኼ’–”¿Š‹†‘™• 84 84
  85. 9/13/2011 мд‰ tác Document/View ‡ À†ዙǣ void CSDIDrawView::OnDraw(CDC* pDC) { CSDIDrawDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; //Add draw code here } ኼ’–”¿Š‹†‘™• 85 85
  86. 9/13/2011 е’‘…—‡– ዒ–•ዎ’Šዛዓ‰–Šዜ…“—ƒ–”ዌ‰ǣ ‡ virtual BOOL OnNewDocument(); ‡ virtual BOOL OnOpenDocument(LPCTSTR lpszPathName); ‡ void SetModifiedFlag(BOOL bModified = ȌǢȋ0žŠ†ኸ—ǣ…Ž‡ƒȀ†‹”–›Ȍ ‡ BOOL IsModified(); ‡ virtual void OnCloseDocument(); ‡ virtual BOOL OnSaveDocument(LPCTSTR lpszPathName); ‡ CDocument::DeleteContents ኼ’–”¿Š‹†‘™• 86 86
  87. 9/13/2011 е’‹‡™ ‡ ‘…—‡–‰‹ዟዒ‹†—‰–‹Ž‹ዉ—Ǥ0዇Š‹዇–ŠዋǡŽዔ’ ‹‡™–኶‘”ƒ…‘–”ው¯ዅŽዔ’‘…—‡– ± CDocument* GetDocument() const; ‡ OnDrawǣዄ…ዞƒ•ዐ‹‡™ ‡ OnInitialUpdateǣ0ዛዘ…‰ዌ‹–”ዛዔ…Š‹ዒ–˜‹‡™Šዖ‹ –኶‘˜Š‹዇–ŠዋǤ ‡ OnUpdateǣ0ዛዘ…‰ዌ‹Š‹†ዟŽ‹ዉ—¯ –Šƒ›¯ዐ‹¯዇…ኼ’ Šኼ–Ž኶‹‹‡™ ‡ Xem MSDN ኼ’–”¿Š‹†‘™• 87 87
  88. 9/13/2011 ž…Žе’‹‡™…—‰…Й’•С ‡  šŸ›†ዠ‰•ዀ…ž…Žዔ’‹‡™Šዑ–”ዘ…ž…†኶‰‰‹ƒ‘†‹ዉ Š‹዇–Šዋ†ዟŽ‹ዉ—ȋ…Šዌ†ኻš—ኸ––ዝŽዔ’„ƒ•‡–ዛዓ‰ዜ‰Ȍ ኼ’–”¿Š‹†‘™• 88 88
  89. 9/13/2011 Ex2-ͳǤЏ‰†к‰‡š–†‹–‘” ‡ Ÿ›†ዠ‰ዜ‰†ዙ‰ ‹ዅ–”ï… ‡ ×…ž…–ÀŠ£‰•‘኶–Š኷‘¯ዓ‰‹኷ȋዛዓ‰–ዠ Notepad) ኼ’–”¿Š‹†‘™• 89 89
  90. 9/13/2011 App Wizard ‡ Ÿ›†ዠ‰ዜ‰†ዙ‰‹ዅ–”ï… ኼ’–”¿Š‹†‘™• 90 90
  91. 9/13/2011 App Wizard ŠዌŽዔ’‹‡™•ዄዅ –Šዝƒȋ„ƒ•‡…Žƒ••Ȍ ኼ’–”¿Š‹†‘™• 91 91
  92. 9/13/2011 Ex2-ʹǤЏ‰†к‰‡–…Š‡” ‡ ኶‘ዜ‰†ዙ‰ ‹ዅ–”ï… ‡ ዞ†ዙ‰‹œƒ”†ǣ ± Leave the default option, Multiple documents, but opt out of Tabbed documents. ± Select MFC standard as the project style and Windows Native/Default as the Visual style and colors option. ± Keep the Use Unicode libraries option. ± ’‡…‹ˆ›–Š‡ƤŽ‡‡š–‡•‹‘ƒ••‡–‘‰‡–ƒƤŽ–‡”ˆ‘”ȗǤ•‡ †‘…—‡–•†‡Ƥ‡†Ǥ ± Change the Generated Classes set of options at their default settings so that the base class for the CSketcherView class is CView. ኼ’–”¿Š‹†‘™• 92 92
  93. 9/13/2011 App Wizard ኼ’–”¿Š‹†‘™• 93 93
  94. 9/13/2011 2.3. Menu và Toolbar ‡ 2.3.1. Menu ± ዒ–˜‹Šž‹‹ዉ ± ኶‘Žኼ’‡— ± ዞŽý ± ‹–ኼ’ ኼ’–”¿Š‹†‘™• 94 94
  95. 9/13/2011 Menu Ȃг–vài khái ‹Ъ ‡ Menu bar: bao ‰ዏ Š‹዆— drop-down menu và menu items ‡ Drop-down menu: Šዜƒ menu item Š‘዁ drop- down menu khác ȋ ‹Ž‡ǡ†‹–ǡǥȌ ‡ Menu items: –ዛዓ‰ ዜ‰ ˜ዔ‹ ዒ– ŽዉŠ ዚƒ Šዛዓ‰ trình. Xác ¯ዋŠ „ኾ‰ ID riêng „‹ዉ–, item ID hay ‘ƒ† ȋ’‡ǡƒ˜‡ǡǥȌ ‡ Pop-up menu: ‰‹ዎ‰ drop-down menu Šዛ‰ có –Š዇ š—ኸ– Š‹ዉ ዖ˜ዋ trí „ኸ– ü (–Šዛዕ‰ Š—ዒ– ’Š኷‹) ‡ System menu: Šዜƒ các ŽዉŠ Šዉ –Šዎ‰ ¯‹዆— Š‹዇ ዞƒ •ዐ (Minimize, maximize, Close) ኼ’–”¿Š‹†‘™• 95 95
  96. 9/13/2011 З‘ ŽН’ menu ‡ Šዛዕ‰ có 2 cách chính: ± ኶‘ ‡—ዖ†኶‰ resource, load vào khi Š኶› (Dùng menu editor) ± ኶‘ –”ዠ –‹ዅ’ „ኾ‰ các hàm (•ዞ †ዙ‰ Žዔ’ CMenu) 96 ኼ’–”¿Š‹†‘™• 96
  97. 9/13/2011 п†к‰‡—†‹–‘” ኼ’–”¿Š‹†‘™• 97 97
  98. 9/13/2011 Add Handler for menu messages ኼ’–”¿Š‹†‘™• 98 98
  99. 9/13/2011 п lý ŽЪŠ лƒ Menu item ‡ ዠƒ trên message WM_COMMAND ‡ 0ዋŠ ‰ŠÂƒ message map BEGIN_MESSAGE_MAP(CSketcherDoc, CDocument) ON_COMMAND(ID_COLOR_BLACK, &CSketcherDoc::OnColorBlack) ON_COMMAND(ID_COLOR_RED, &CSketcherDoc::OnColorRed) ON_COMMAND(ID_COLOR_GREEN, &CSketcherDoc::OnColorGreen) ON_COMMAND(ID_COLOR_BLUE, &CSketcherDoc::OnColorBlue) END_MESSAGE_MAP() ‡ ‹ዅ–ŠšዞŽýǣ void CSketcherDoc::OnColorBlack() { // TODO: Add your command handler code here } ኼ’–”¿Š‹†‘™• 99 99
  100. 9/13/2011 п†к‰Žе’CMenu ‡ BOOL CreateMenu(); ‡ BOOL CreatePopupMenu(); ‡ BOOL LoadMenu(LPCTSTR lpszResourceName); ‡ BOOL LoadMenu(UINT nIDResource); ‡ Thao tác trên Menu: ± DeleteMenu: ‘žዒ–¯ዎ‹–ዛዘ‰–”²‡— ± TrackPopupMenu: Š‹዇–Šዋ‡—’‘’—’ ኼ’–”¿Š‹†‘™• 100 100
  101. 9/13/2011 п†к‰Žе’‡— ‡ Thao tác trên Menu item: ± AppendMenu: –Š²˜‘…—ዎ‹‡—ዒ–‡—‹–‡Ǥ ± InsertMenu: –Š²ዒ–¯ዎ‹–ዛዘ‰‡—–኶‹ዒ–˜ዋ–”Àšž… ¯ዋŠ ± ModifyMenu: ŠዊŠ•ዞƒዒ–¯ዎ‹–ዛዘ‰‡—šž…¯ዋŠǤ ± Š‡…‡— –‡ǣ–Š²Ȁ„ው†ኸ—…Š‡…’ŠÀƒ–”ዛዔ…ዒ– menuitem ± ƒ„Ž‡‡— –‡ǣ–Š‹ዅ–Žኼ’‡—‹–‡…ž…–”኶‰–Šž‹ enable (MF_ENABLED), disable (MF_DISABLED), gray(MF_GRAYED). ኼ’–”¿Š‹†‘™• 101 101
  102. 9/13/2011 Popup (Context) Menu ‡ 2 cách: ± Š‹ዅ– ዅ ዒ– menu ¯዁ „‹ዉ– dùng làm context menu („ው qua top level) ± ዞ †ዙ‰ ዒ– thành ’Šኹ drop-down menu (submenu) –ዝ menu chính ኼ’–”¿Š‹†‘™• 102 102
  103. 9/13/2011 З‘ pop-up menu ‡ Hàm šዞ lý CWnd::OnContextMenu ‡ ‹዇ –Šዋ ዒ– menu pop-up ዐ‹ ± BOOL CMenu::TrackPopupMenu(UINT nFlags, int x, int y, CWnd* pWnd, LPCRECT lpRect=0); ‡ nFlags: ዕ ˜ዋ trí trên màn hình (TPM_CENTERALIGN , TPM_LEFTALIGN , TPM_RIGHTALIGN), ዕ Šዊ phím Šኸ ዚƒ Š—ዒ– (TPM_LEFTBUTTON, TPM_RIGHTBUTTON) ‡ ‹ዅ– hàm šዞ lý: 103 103
  104. 9/13/2011 З‘ ŽН’ menu cho dialog ‡ ኶‘ ዒ– tài nguyên menu cho ዜ‰ †ዙ‰ ‡ Šዌ menu ኹ ‰ኽ vào dialog trong Šዒ’ –Š‘኶‹ Dialog Properties. ‡ ‘዁…•ዞ†ዙ‰Š CMenu::LoadMenu() ኼ’–”¿Š‹†‘™• 104 104
  105. 9/13/2011 Ex2-3a. Menu ‡ ኶‘Žኼ’‡—…Š‘ዜ‰†ዙ‰‡–…Š‡”–”‘‰ Ex2-ʹ…×…ž…–ŠŠ’Šኹǣ ± Ž‡‡–…Šዜƒ…ž…‡—‹–‡•ǣ‹‡ǡ‡…–ƒ‰Ž‡ǡ Circle, Curve ± ‘Ž‘”…Šዜƒ…ž…‡—‹–‡•ǣŽƒ…ǡ‡†ǡ ”‡‡ǡ Blue ‡ ȋŠዙ…˜ዙ˜‹ዉ…˜ዄ…ž…Š¿Š˜ዔ‹—•ኽ…Šž… nhau) ኼ’–”¿Š‹†‘™• 105 105
  106. 9/13/2011 2.3. Menu và Toolbar (cont.) ‡ 2.3.2. Toolbar ± ዒ–•ዎ†኶‰‘‘Ž„ƒ” ± Š‹ዅ–ዅ‘‘Ž„ƒ””‡•‘—”…‡ ± Š²‘‘Ž„ƒ”˜‘ዜ‰†ዙ‰ ± ዞŽý–ŠØ‰¯‹ዉ’…ž…ï–ŽዉŠ–”²‘‘Ž„ƒ” ± ዞŽýዒ–•ዎ†኶‰–‘‘Ž„ƒ”¯዁…„‹ዉ– ኼ’–”¿Š‹†‘™• 106 106
  107. 9/13/2011 г–•Я†З‰‘‘Ž„ƒ” ኼ’–”¿Š‹†‘™• 107 107
  108. 9/13/2011 Š‹Ц–Ц‘‘Ž„ƒ”‡•‘—”…‡ ‡ ዔ‹ዜ‰†ዙ‰‹ዅ–”ï… Ȁ ǣ–኶‘•ዀ –‘‘Ž„ƒ”዁…¯ዋŠǤ ‡ ×–Š዇•ዞ†ዙ‰‘‘Ž„ƒ”†‹–‘”ȋዖ ‡•‘—”…‡Ȁ‘‘Ž„ƒ”Ȍ¯዇•ዞƒ¯ዐ‹–‘‘Ž„ƒ”¯ …× Š‘዁…–኶‘–‘‘Ž„ƒ”ዔ‹Ǥ ‡ ×–Š዇•ዞ†ዙ‰኷Š„‹–ƒ’ȋ–Š²˜‘ ‡•‘—”…‡Ȍ¯዇–Š‹ዅ–ዅ–‘‘Ž„ƒ”Ǥȋዞ†ዙ‰Š ‘ƒ†‹–ƒ’ȋȌ…ዚƒŽዔ’‘‘Ž„ƒ”¯዇኶’Ȍ ኼ’–”¿Š‹†‘™• 108 108
  109. 9/13/2011 п †к‰ toolbar editor ‡ Resource/Toolbar ኼ’–”¿Š†‘™• 109 109
  110. 9/13/2011 Thêm Toolbar vào н‰ †к‰ ‡ ዞ†ዙ‰Žዔ’‘‘Žƒ”ǣ ȋ ‘዁… ‘‘Žƒ”Ȍ ± Šƒ‹„ž‘¯ዎ‹–ዛዘ‰Žዔ’ ± ዌ‹Š–኶‘Žኼ’…ዞƒ•ዐ‘‘Ž„ƒ” ± ኶’‘‘Ž„ƒ””‡•‘—”…‡¯ –Š‹ዅ–ዅ ± Š‹ዅ–Žኼ’–Š—ዒ…–ÀŠ…Š‘–‘‘Ž„ƒ” ± ኽ–‘‘Ž„ƒ”˜‘…ዞƒ•ዐ…ŠÀŠ ኼ’–”¿Š‹†‘™• 110 110
  111. 9/13/2011 Khai báo ¯Я‹–мй‰Žе’CToolBar ኼ’–”¿Š‹†‘™• 111 111
  112. 9/13/2011 пŽýtrong hàm OnCreate Ȃ ‹ŠŠЭƒͳ ኼ’–”¿Š‹†‘™• 112 112
  113. 9/13/2011 пŽý–”‘‰Š”‡ƒ–‡Ȃ ‹ŠŠЭƒʹ ኼ’–”¿Š‹†‘™• 113 113
  114. 9/13/2011 Toolbar Style ኼ’–”¿Š‹†‘™• 114 114
  115. 9/13/2011 пŽý–”‘‰Š…”‡ƒ–‡Ȃ ‹ŠŠЭƒʹȋ––Ȍ ኼ’–”¿Š‹†‘™• 115 115
  116. 9/13/2011 Toolbar docking style ኼ’–”¿Š‹†‘™• 116 116
  117. 9/13/2011 šпŽý…Š‘…ž…ï–ŽЪŠ–”²–‘‘Ž„ƒ” ‡ ዅ— toolbar là Šዜ £‰ –ዝ menu item: Šዌ ID cho button trùng ID ዚƒ menu item –ዛዓ‰ ዜ‰ȋ–Šƒ…Š‹ዅ—¯ዅŠšዞ Žý…ዚƒ‡—‹–‡•Ȍ ‡ ዅ— không có menu item –ዛዓ‰ ዜ‰, thêm ˜‹ዅ– hàm šዞ lý thông ¯‹ዉ’ (vào MESSAGE_MAP). Ví †ዙ ID_BUTTON_BOLD 117 117
  118. 9/13/2011 пŽýг–•Я†З‰–‘‘Ž„ƒ”¯Т…„‹Ъ– ‡ אŠ×„—––‘†኶‰…Š‡…„‘š ‡ אŠ×„—––‘†኶‰”ƒ†‹‘ ‡ Có Combo box trên Toolbar ኼ’–”¿Š‹†‘™• 118 118
  119. 9/13/2011 Nhóm button †З‰ checkbox và radio ‡ ኶‰…Š‡…„‘šǣŠ‘…ŠዌŠ‹዆—„—––‘–”‘‰ͳŠ× ‡ ኶‰”ƒ†‹‘ǣŠዊ…Šዌͳ„—––‘–”‘‰ͳŠ× ‡ ‹኷‹’Šž’ǣ0዁–…ž…„‹ዅ…ዕ–”኶‰–Šž‹ǡ’Šዎ‹Šዘ’šዞŽý•ዠ‹ዉ ̴̴ …ዚƒ…ž…„—––‘–ዛዓ‰–ዠŠዛ˜ዔ‹‡— items ኼ’–”¿Š‹†‘™• 119 119
  120. 9/13/2011 À†кšпlý „—––‘‹Ш—…Š‡…„‘šȋͳȌ ኼ’–”¿Š‹†‘™• 120 120
  121. 9/13/2011 À†кšпlý „—––‘‹Ш—…Š‡…„‘šȋʹȌ ኼ’–”¿Š‹†‘™• 121 121
  122. 9/13/2011 À†кšпlý „—––‘‹Ш—”ƒ†‹‘ȋͳȌ ኼ’–”¿Š‹†‘™• 122 122
  123. 9/13/2011 À†кšпlý „—––‘‹Ш—”ƒ†‹‘ȋʹȌ ኼ’–”¿Š‹†‘™• 123 123
  124. 9/13/2011 Enable/Disable button ኼ’–”¿Š‹†‘™• 124 124
  125. 9/13/2011 Combo box trên toolbar ‡ ‰—›²Žý–኶‘…ዞƒ•ዐ…‘ȋ‘„‘š„‘šȌ‰ኽ ˜‘…ዞƒ•ዐ…Šƒȋ‘‘Ž„ƒ”Ȍ ኼ’–”¿Š‹†‘™• 125 125
  126. 9/13/2011 Khai báo ¯Я‹–мй‰…‘„‘„‘š ኼ’–”¿Š‹†‘™• 126 126
  127. 9/13/2011 О combo box vào toolbar ኼ’–”¿Š‹†‘™• 127 127
  128. 9/13/2011 Ex2-3b. Toolbar ‡ Ÿ›†ዠ‰–‘‘Ž„ƒ”…Š‘ዜ‰†ዙ‰‡–…Š‡”Ǥ‘‘Ž„ƒ” –”‹዇Šƒ‹…ž…ŽዉŠ…ዚƒ‡—‹–‡•…ŠዌŽ‡‡–˜ ‘Ž‘”–ዛዓ‰ዜ‰Ǥ ‡ ‘†‹ˆ›–ዝ‘‘Ž„ƒ”…וዀ…ዚƒዜ‰†ዙ‰ ኼ’–”¿Š‹†‘™• 128 128
  129. 9/13/2011 2.4. п lý ˜Х (Drawing) ‡ ʹǤͶǤͳǤዓ„኷˜዆˜ዄ–”²‹†‘™• ‡ ʹǤͶǤʹǤ”—›š—ኸ–‰ዟ…኷Š–Š‹ዅ–„ዋ ‡ ʹǤͶǤ͵Ǥዒ–•ዎ–Šƒ‘–ž…˜ዄ…ዓ„኷ ‡ ʹǤͶǤͶǤዞ†ዙ‰…ž…¯ዎ‹–ዛዘ‰˜ዄ ኼ’–”¿Š‹†‘™• 129 129
  130. 9/13/2011 ʹǤͶǤͳǤд„И˜Ч˜Х–”²‹†‘™• ‡ ዉ–ዌƒ¯ዒ…—‰…ኸ’…Š‘šዞŽý˜ዄ–”²…ዞƒ•ዐǫ ‡ ዜ‰†ዙ‰…ዚƒ…ዞƒ•ዐȋ…Ž‹‡–ƒ”‡ƒȌǫ ኼ’–”¿Š‹†‘™• 130 130
  131. 9/13/2011 GDI ? ‡ ‹†‘™•…—‰…ኸ’  ȋ ”ƒ’Š‹…•‡˜‹…‡ –‡”ˆƒ…‡Ȍ Ȃ giao †‹ዉ –Š‹ዅ– „ዋ ¯ዏ Šዌƒ, giúp –Šዠ Š‹ዉ thao tác ˜ዄ †ወ dàng Šዓ, không ’Šዙ –Š—ዒ vào ’Šኹ ዜ‰ ¯ዏ Šዌƒ ዚƒ Šዉ –Šዎ‰. ‡ ዒ– Šዛዓ‰ trình ዜ‰ †ዙ‰ (WindowsApp) không ˜ዄ –”ዠ –‹ዅ’ ra màn hình, máy ‹ǥmà Šዊ ˜ዄ trên Dz„዆ ዁– Ž—ኼ lýdz–Š዇ Š‹ዉ „ዖ‹ ‰ዟ ኷Š –Š‹ዅ– „ዋ (Device Context Ȃ DC). ‡ ‰ዟ ኷Š –Š‹ዅ– „ዋ Šዜƒ các thông tin ˜዆ Šዉ –Šዎ‰, ዜ‰ †ዙ‰ và ዞƒ •ዐ ዚƒ WindowsApp ñ‰ Šዛ các ¯ዎ‹ –ዛዘ‰ ¯ዏ Šዌƒ ¯ƒ‰ ¯ዛዘ ˜ዄ trong WindowApp ¯×. ኼ’–”¿Š‹†‘™• 131 131
  132. 9/13/2011 д…ŠЦ˜Х–”‘‰  ‡ ኮ‰†ዙ‰ ȋ‹ዅ–”ï…‘…—‡–Ȁ‹‡™Ȍ…ŠዜƒŽዔ’†ኻ š—ኸ––ዝŽዔ’‹‡™ȋ“—኷Žý˜‹ዉ…Š‹዇–Šዋ†ዟŽ‹ዉ—Ž²˜î‰Ž ˜‹ዉ…ȋ…Ž‹‡–ƒ”‡ƒȌ…ዚƒ…ዞƒ•ዐዜ‰†ዙ‰ȌǤ ‡ ž…šዞŽý˜ዄ–”²˜î‰Š‹዇–Šዋ›¯ዛዘ…–Šዠ…Š‹ዉ–”‘‰ hàm OnDraw( ) ȋŠ‘˜‡””‹†‡¯ž’ዜ‰–ŠØ‰¯‹ዉ’ ̴ ¯ዛዘ…‰ዌ‹„ኸ–…ዜŠ‹‘˜î‰…ዞƒ•ዐ…ኹ¯ዛዘ…˜ዄ Ž኶‹ȌǤ‹ŠŠዌƒǣ 132 132
  133. 9/13/2011 ʹǤͶǤʹǤ”—›š—Й–‰р ИŠ –Š‹Ц– „Ь (1) ‡ ዔ’ǣ ± Šዜƒͳ‰ዟ…኷Š–Š‹ዅ–„ዋȋ†‡˜‹…‡…‘–‡š–Ȃ dc) và ž…’Šዛዓ‰–Šዜ……ኹ–Š‹ዅ–¯዇šዞŽý˜ዄ–”‘‰ዜ‰ †ዙ‰Ǥȋ̱ͳͲͲŠȌǤ ± À†ዙǣዄ¯ዛዕ‰ȋ‹‡‘Ȍǡ˜ዄŠ¿Š…ŠዟŠኼ– ȋ‡…–ƒ‰Ž‡Ȍǡ˜ዄŠ¿ŠŽŽ‹’•‡ǡǥ ± Žዔ’…ዓ•ዖ ኼ’–”¿Š‹†‘™• 133 133
  134. 9/13/2011 ”—›š—Й–‰р ИŠ –Š‹Ц– „ЬȋʹȌ ‡ ‰‘‹”ƒ …—‰…ኸ’ዒ–•ዎŽዔ’‰ዟ…኷Š–Š‹ዅ–„ዋ Š—›²„‹ዉ–ȋዅ–Šዝƒ–ዝŽዔ’Ȍǣ ± CPaintDCǣዞ†ዙ‰…Š‘˜‹ዉ…˜ዄ–”‘‰˜î‰ዜ‰†ዙ‰…ዚƒ ዞƒ•ዐȋ…Šዊ–Šƒ‘–ž…˜ዔ‹•ዠ‹ዉƒ‹–Ȍ ± CClientDCǣዞ†ዙ‰…Š‘˜‹ዉ…˜ዄ–”‘‰˜î‰ዜ‰†ዙ‰…ዞƒ •ዐȋ˜ዄ„ኸ–üዓ‹‘Šዛ‰…Šዊ–Šƒ‘–ž…˜ዔ‹•ዠ‹ዉ OnPaint) ± CWindowDC: ȋዞ†ዙ‰…Š‘˜‹ዉ…˜ዄ–”‘‰…ዞƒ•ዐǡ„ƒ‘ ‰ዏ…኷˜î‰ŠØ‰Ž˜î‰ዜ‰†ዙ‰…ዞƒ•ዐȌ ± CMetaFileDCǣȋዞ†ዙ‰…Š‘˜‹ዉ…˜ዄዒ–  ‡–ƒˆ‹Ž‡Ȍ ኼ’–”¿Š‹†‘™• 134 134
  135. 9/13/2011 Truy š—Й– ‰р ИŠ –Š‹Ц– „Ьȋ͵Ȍ ‡ 0዇Žኸ›‰ዟ…኷Š–Š‹ዅ–„ዋǡŠƒ‹„ž‘„‹ዅ¯ዎ‹–ዛዘ‰ Žዔ’ǡ†î‰ǣ CDC dc(this); ‡ ‘዁…„‹ዅ…‘–”ውȗ’α ‡–ȋȌǢ ‡ 0዇‰‹኷‹’Š×‰‰ዟ…኷Š–Š‹ዅ–„ዋǡ†î‰ǣ ReleaseDC(pDC); delete pDC; ኼ’–”¿Š‹†‘™• 135 135
  136. 9/13/2011 ”—›š—Й–‰р…ИŠ–Š‹Ц–„ЬȋͶȌ ‡ ዌƒ¯ዒ˜ዄŠ‹ዉ–Šዕ‹ȋ…—””‡–’‘•‹–‹‘Ȍǣ ± ዌƒ¯ዒ¯‹዇–”²˜î‰˜ዄ ± —ƒŠዉ–ዛዓ‰¯ዎ‹ǡ…×–Š዇–Šƒ›¯ዐ‹Š‹–Šዠ…Š‹ዉ ˜ዄ…ž…–Šዠ…–Š዇ȋŽ‹‡ǡ…‹”…Ž‡ǡ”‡…–ƒ‰Ž‡ǡ–‡š–ǥȌ ± ×–Š዇–Š‹ዅ–Žኼ’„ኾ‰’Šዛዓ‰–Šዜ……ዚƒŽዔ’ǣ CPoint MoveTo(int x, int y); // Move to position x,y CPoint MoveTo(POINT aPoint); // Move to position defined by aPoint ኼ’–”¿Š‹†‘™• 136 136
  137. 9/13/2011 ʹǤͶǤ͵Ǥг–•Я–Šƒ‘–ž…˜Х…д„ИȋͳȌ ‡ ዄ¯ዛዕ‰–Š኿‰ȋ‹‡•Ȍǣ–ዝ˜ዋ–”ÀŠ‹ዉ–኶‹¯ዅ˜ዋ–”À (x,y) BOOL LineTo(int x, int y); // Draw a line to position x,y BOOL LineTo(POINT aPoint); // Draw a line to position defined by aPoint ‡ À†ዙǣ CDC *pDC; pDC->MoveTo(50,50); // Set the current position pDC->LineTo(50,200); // Draw a vertical line down 150 units pDC->LineTo(150,200); // Draw a horizontal line right 100 units pDC->LineTo(150,50); // Draw a vertical line up 150 units pDC->LineTo(50,50); // Draw a horizontal line left 100 units ኼ’–”¿Š‹†‘™• 137 137
  138. 9/13/2011 г–•Я–Šƒ‘–ž…˜Х…д„ИȋʹȌ ‡ ዄ¯ዛዕ‰–”֐ȋ…‹”…Ž‡Ȍǣ BOOL Ellipse(int x1, int y1, int x2, int y2); BOOL Ellipse(LPCRECT lpRect); ‡ ዄ…—‰ȋ”…Ȍǣ BOOL Arc(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); BOOL Arc(LPCRECT lpRect, POINT startPt, POINT endPt); ‡ À†ዙǣ CDC *pDC; pDC->Ellipse(50,50,150,150); // Draw the 1st (large) circle ȀȀ‡Ƥ‡–Š‡„‘—†‹‰”‡…–ƒ‰Ž‡ˆ‘”–Š‡ʹ†ȋ•ƒŽŽ‡”Ȍ…‹”…Ž‡ CRect rect(250,50,300,100); CPoint start(275,100); // Arc start point CPoint end(250,75); // Arc end point pDC->Arc(&rect, start, end); // Draw the second circle 138
  139. 9/13/2011 г–•Я–Šƒ‘–ž…˜Х…д„Иȋ͵Ȍ ‡ CDC::Polygon ‡ CDC::Rectangle ‡ CDC::Draw3dRect ‡ CDC::DrawText virtual int DrawText( ‡ ǥ LPCTSTR lpszString, int nCount, LPRECT lpRect, UINT nFormat ); int DrawText( const CString& str, LPRECT lpRect, UINT nFormat ); ኼ’–”¿Š‹†‘™• 139 139
  140. 9/13/2011 ʹǤͶǤͶǤп†к‰…ž…¯Я‹–мй‰˜ХȋͳȌ ‡ –Šዅ‘¯዇ǣ ± ዄ…ž…¯ዛዕ‰˜ዄ…׏—•ኽ…ǡ’Š‘‰…ž…Šȋ•–›Ž‡Ȍ khác nhau ± ؏—…ž…¯ዎ‹–ዛዘ‰˜ዄ˜ዔ‹—•ኽ…ǡ‹዇—–Ø khác nhau ± ዄ…Šዟ˜ዔ‹…ž…—•ኽ…ǡ‹዇—…ž…ŠŠž…Šƒ— ‡ ኹ•ዞ†ዙ‰…ž…¯ዎ‹–ዛዘ‰˜ዄȋ„ï–˜ዄǡ…Šዐ‹–Ø —ǡˆ‘–…ŠዟȌ ኼ’–”¿Š‹†‘™• 140 140
  141. 9/13/2011 п†к‰…ž…¯Я‹–мй‰˜ХȋʹȌ ‡ ï–˜ዄȋ‡Ȍǣ†î‰¯዇˜ዄ¯ዛዕ‰ ‡ Šዐ‹˜ዄȀ…ዌ˜ዄȋ”—•ŠȌǣ†î‰–؏— ‡ Font ‡ ž…¯ዎ‹–ዛዘ‰›¯዆—¯ዛዘ…“—኷Žý„ዖ‹…ž…Žዔ’  –ዛዓ‰ዜ‰Ǥ ኼ’–”¿Š‹†‘™• 141 141
  142. 9/13/2011 п†к‰„ï–˜ХȋͳȌ ‡ ዔ’‡ ‡ ዄ¯ዛዕ‰˜ዔ‹‹዇—ǡ—•ኽ…ǡ¯ዒ”ዒ‰šž…¯ዋŠ ‡ ኶‘„ï–˜ዄǣ CPen( ); CPen( int nPenStyle, int nWidth, COLORREF crColor ); CPen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL ); ኼ’–”¿Š‹†‘™• 142 142
  143. 9/13/2011 п†к‰„ï–˜ХȋʹȌ ‡ VÀ†ዙŠዛ•ƒ—–኶‘„ï–˜ዄዔ‹˜…ŠዌŽ„ï–˜ዄ Š‹ዉ–Šዕ‹ǡ†î‰ǣ ኼ’–”¿Š‹†‘™• 143 143
  144. 9/13/2011 п†к‰„ï–˜Хȋ͵Ȍ ‡ ×–Š዇–኶‘„ï–˜ዄ†î‰Š‡ǣǣ”‡ƒ–‡‡ ‡ À†ዙ‹ŠŠዌƒ…ž…Š†î‰›ǣ // Declare a pen object and initialize it as // a red solid pen drawing a line 2 pixels wide CPen aPen; aPen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); CPen* pOldPen = pDC->SelectObject(&aPen); // Select aPen as the pen pDC->Ellipse(50,50,150,150); // Draw the 1st (large) circle // Define the bounding rectangle for the 2nd (smaller) circle CRect rect(250,50,300,100); CPoint start(275,100); // Arc start point CPoint end(250,75); // Arc end point pDC->Arc(&rect,start, end); // Draw the second circle pDC->SelectObject(pOldPen); // Restore the old pen ኼ’–”¿Š‹†‘™• 144 144
  145. 9/13/2011 п†к‰…Э˜ХȋͳȌ ‡ ዔ’„”—•Š ‡ ×–Š዇–኶‘…ዌ˜ዄ„ኾ‰…ž…Š†î‰Š–኶‘ǡŠ‘዁…†î‰ hàm CreateBrush CBrush( ); CBrush( COLORREF crColor ); CBrush( int nIndex, COLORREF crColor ); explicit CBrush( CBitmap* pBitmap ); ኼ’–”¿Š‹†‘™• 145 145
  146. 9/13/2011 п†к‰…Э˜ХȋʹȌ ‡ À†ዙ–኶‘…ዌ˜ዄዔ‹˜…ŠዌŽ…ዌ˜ዄŠ‹ዉ–Šዕ‹: ኼ’–”¿Š‹†‘™• 146 146
  147. 9/13/2011 GDI Fonts và е’ CFont ‡ ‰‘‹˜‹ዉ…•ዞ†ዙ‰ˆ‘–…Šዟ዁…¯ዋŠǡ‰ዛዕ‹†î‰ ֐…×–Š዇–኶‘ˆ‘–…Šዟ–”‘‰…Šዅ¯ዒ¯ዏŠዌƒ tùy ŠዌǤ ኼ’–”¿Š‹†‘™• 147 147
  148. 9/13/2011 Ex2-ͶǤХ–”‘‰н‰†к‰‡–…Š‡” ‡ Šዠ Š‹ዉ các thao tác ˜ዄ…ዓ„኷–”²ዜ‰ †ዙ‰‡–…Š‡”ǣ ± ዄ¯ዛዕ‰–Š኿‰ǡŠ¿Š–”֐ǡŠ¿Š…ŠዟŠኼ– ± ȋž…¯ዎ‹–ዛዘ‰˜ዄ–኶‹˜ዋ–”ÀŠ‹ዉ–Šዕ‹Ȍ ‡ Thay ¯ዐ‹ bút ˜ዄ (CPen), Šዐ‹ ˜ዄ (CBrush) khác nhau ኼ’–”¿Š‹†‘™• 148 148
  149. 9/13/2011 2.5. Giao –‹Ц’ ˜е‹…Š—г–ǡ„’ŠÀ ‡ ʹǤͷǤͳǤ ‹ƒ‘–‹ዅ’˜ዔ‹…Š—ዒ– ‡ ʹǤͷǤʹǤ ‹ƒ‘–‹ዅ’˜ዔ‹„’ŠÀ ኼ’–”¿Š‹†‘™• 149 149
  150. 9/13/2011 ʹǤͷǤͳǤ ‹ƒ‘–‹Ц’˜е‹…Š—г– ‡ ኹšዞŽý•ዠ‹ዉ…Š—ዒ–‰¿ǫ ± ŠÀ…Š—ዒ––”ž‹Ȁ’Š኷‹¯ዛዘ…ኸȀŠ኷ȋ†‘™Ȁ—’Ȍ ± ‹ዟ’ŠÀ–”ž‹˜†‹…Š—ዒ–ȋ‘—•‡‘˜‡Ȍ ± ŠÀ…Š—ዒ–‰‹ዟƒǡ’ŠÀ…Š—ዒ–ዖ”ዒ‰ȋዅ—…×Ȍ ± ኊ’ŠÀ…Š—ዒ–°’ŠÀ…Šዜ…£‰ȋ‘–”‘ŽǡŠ‹ˆ–Ȍ ‡ ዞŽý‰¿ǫȋ’Šዙ–Š—ዒ…˜‘ዜ‰†ዙ‰Ȍ ‡ ዓ‹‘ȋŽዔ’‘ȌŠኼšዞŽý•ዠ‹ዉ…Š—ዒ–ǫ ‡ À†ዙǣዔ‹ዜ‰†ዙ‰•‡–…Š‡”ǣ ± —ዎ˜ዄ¯ዎ‹–ዛዘ‰„ኽ–¯ኹ—Š‹ኸ…Š—ዒ––”ž‹ȋ†‘™Ȍǡ‰‹ዟ Ϊ†‹…Š—ዒ–ȋ‘˜‡ȌǡŠ‘–ŠŠŠ‹Š኷…Š—ዒ–ȋ—’ȌǤ ± ž…¯ዎ‹–ዛዘ‰¯ዛዘ…˜ዄ–”²Žዔ’‡–…Š‡”‹‡™ǡ…ኹ„ኽ– •ዠ‹ዉ…Š—ዒ––”²Žዔ’›Ǥ ኼ’–”¿Š‹†‘™• 150 150
  151. 9/13/2011 ŠØ‰¯‹Ъ’–о•с‹Ъ…Š—г– ‡ ŠዌŽዔ’…ኹšዞŽý–ŠØ‰ ¯‹ዉ’–ዝ…Š—ዒ– ‡ Šዌ‘—•‡‡••ƒ‰‡• —ዎšዞŽýȋ–”‘‰ Properties) ‡ ŠዌŠšዞŽýȋ‡••ƒ‰‡ Šƒ†Ž‡”•Ȍ–”‘‰…ዒ–„² ’Š኷‹–ዛዓ‰ዜ‰‡••ƒ‰‡ —ዎšዞŽý ኼ’–”¿Š‹†‘™• 151 151
  152. 9/13/2011 Mouse Message Handlers (1) Thông ¯‹Ъ’ с ‹Ъ Hàm šпŽý (Messages) (Events) (Handlers) WM_LBUTTONDOWN Nút –”ž‹…Š—ዒ–¯ዛዘ…Šኸš—ዎ‰ OnLButtonDown WM_LBUTTONUP Nút –”ž‹…Š—ዒ–¯ዛዘ…Š኷”ƒ OnLButtonUp WM_LBUTTONDBLCLK Nút –”ž‹…Š—ዒ–¯ዛዘ…Šኸ±’ OnLButtonDblClk WM_MBUTTONDOWN Nút ‰‹ዟƒ…Š—ዒ–¯ዛዘ…Šኸš—ዎ‰ OnMButtonDown WM_MBUTTONUP Nút ‰‹ዟƒ…Š—ዒ–¯ዛዘ…Š኷”ƒ OnMButtonUp WM_MBUTTONDBLCLK Nút ‰‹ዟƒ…Š—ዒ–¯ዛዘ…Šኸ±’ OnMButtonDblClk WM_RBUTTONDOWN Nút ’Š኷‹…Š—ዒ–¯ዛዘ…Šኸš—ዎ‰ OnRButtonDown WM_RBUTTONUP Nút ’Š኷‹…Š—ዒ–¯ዛዘ…Š኷”ƒ OnRButtonUp WM_RBUTTONDBLCLK Nút ’Š኷‹…Š—ዒ–¯ዛዘ…Šኸ±’ OnRButtonDblClk WM_MOUSEMOVE Con Š—ዒ–¯ዛዘ…†‹…Š—›዇ OnMouseMove ኼ’–”¿Š‹†‘™• 152 152
  153. 9/13/2011 Mouse Message Handlers (2) ‡ ž…ŠšዞŽý–ŠØ‰¯‹ዉ’…׆኶‰ǣ afx_msg void OnMsgName(UINT nFlags, CPoint point) ‡ À†ዙŠšዞŽý–ŠØ‰¯‹ዉ’ï–…Š—ዒ––”ž‹¯ዛዘ… Šኸ–”²˜î‰…ዞƒ•ዐŠ‹዇–Šዋ void CSketcherView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonDown(nFlags, point); } ‡ ኼ’–”¿ŠšዞŽýŠ‘዁…‰ዌ‹¯ዅŠƒ†Ž‡”•–ዛዓ‰ዜ‰ ዚƒŽዔ’…ዓ•ዖǤ ኼ’–”¿Š‹†‘™• 153 153
  154. 9/13/2011 Mouse Message Handlers (3) ‡ ʹ–Šƒ•ዎǣ ± ‘‹–’‘‹–ǣዌƒ¯ዒ…Š—ዒ–Žï…š኷›”ƒ•ዠ‹ዉ ±   Žƒ‰•ǣዕȋ͵ʹ„‹–Ȍ…Š‘„‹ዅ–ï–…Š—ዒ–¯ዛዘ… Šኸ˜Šኼ„‹ዅ–…ž…’ŠÀ‘–”‘ŽȀŠ‹ˆ–ዅ— ¯ዛዘ…„ኸ°Ǥ ± ×…ž…‰‹ž–”ዋŠኾ‰዁–኶ȋƒ•Ȍ¯ዛዘ…¯ዋŠ ‰ŠÂƒ’Šዙ…˜ዙ…Š‘šž…¯ዋŠ Žƒ‰•›Ǥ ኼ’–”¿Š‹†‘™• 154 154
  155. 9/13/2011 nFlags ‡ 0዇Šኼ„‹ዅ–‰‹ž–”ዋ…ዕ›ǡ†î‰’Š±’Ƭȋ„‹–™‹•‡Ȍ ˜ዔ‹዁–኶ǤÀ†ዙŠኼ„‹ዅ–’ŠÀ–”Ž…ׯዛዘ…Šኸ …Š—ዒ–ǣ ‹ˆȋȋ Žƒ‰•Ƭ̴Ȍαα̴ȌȓǥȔ ኼ’–”¿Š‹†‘™• 155 155
  156. 9/13/2011 À†к‰‹ƒ‘–‹Ц’˜е‹…Š—г–ȋͳȌ void CVdMouseView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default MyDrawFunction(point); CView::OnLButtonDown(nFlags, point); } void CVdMouseView::MyDrawFunction(CPoint point) { CDC *pDC = GetDC(); if((m_PrevPoint.x == -1)&&(m_PrevPoint.y == -1)) pDC->MoveTo(point); else { pDC->MoveTo(m_PrevPoint); pDC->LineTo(point); } m_PrevPoint = point; } 156
  157. 9/13/2011 À†к‰‹ƒ‘–‹Ц’˜е‹…Š—г–ȋʹȌ void CVdMouseView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CDC *pDC = GetDC(); CString szTextOut; szTextOut.Format("Current point(%d, %d)", point.x, point.y); pDC->TextOut(0, 0, szTextOut); if((nFlags & MK_CONTROL)==MK_CONTROL) MyDrawFunction(point); CView::OnMouseMove(nFlags, point); } ኼ’–”¿Š‹†‘™• 157 157
  158. 9/13/2011 ʹǤͷǤʹǤ ‹ƒ‘–‹Ц’˜е‹„’ŠÀ ‡ ‘኶‹•ዠ‹ዉ’ŠÀǣ ± ŠÀ–ŠØ‰–Šዛዕ‰¯ዛዘ…Šኸš—ዎ‰ȀŠ኷Ž² (down/up) ± ŠÀ…Šዜ…£‰¯ዛዘ…Šኸš—ዎ‰ȀŠ኷Ž² ± ŠÀ¯ዛዘ…Šኸ˜‰‹ዟȋŠ‘Ž†Ȍǡ¯ዌ…¯ዛዘ…Š‹዆— ŽኹǤ ± ዐŠዘ’’ŠÀ¯ዛዘ…ŠኸȋŠኸ°…ž…’ŠÀ Šዜ…£‰ǣŽ–ǡ–”ŽǡŠ‹ˆ–Ȍ ‡ ዙ…–‹²—ǣ„ኽ–¯ዛዘ……ž…•ዠ‹ዉ˜዆’ŠÀǡ¯ዌ…  ’ŠÀ¯዇…ךዞŽý–ŠÀ…ŠŠዘ’ ኼ’–”¿Š‹†‘™• 158 158
  159. 9/13/2011 пŽý–ŠØ‰¯‹Ъ’–о„’ŠÀȋͳȌ ‡  Šዑ–”ዘ‰‹ƒ‘–‹ዅ’„’ŠÀ„ዖ‹…ž…–ŠØ‰ ¯‹ዉ’˜ŠšዞŽý–ዛዓ‰ዜ‰Šዛ•ƒ—ǣ Thông ¯‹Ъ’ с ‹Ъ Hàm šпŽý (Messages) (Events) (Handlers) WM_KEYDOWN Phím –Šዛዕ‰¯ዛዘ…Šኸš—ዎ‰ OnKeyDown WM_KEYUP Phím –Šዛዕ‰¯ዛዘ…Š኷”ƒ OnKeyUp WM_SYSKEYDOWN Phím Šዜ…£‰¯ዛዘ…Šኸš—ዎ‰ OnSysKeyDown WM_SYSKEYYP Phím Šዜ…£‰¯ዛዘ…Š኷”ƒ OnSysKeyUp ኼ’–”¿Š‹†‘™• 159 159
  160. 9/13/2011 п lý –ŠØ‰¯‹Ъ’–о„’ŠÀȋʹȌ ‡ ž…ŠšዞŽý–ŠØ‰¯‹ዉ’’ŠÀ…׆኶‰ǣ afx_msg void OnMsgName(UINT nChar, UINT nRepCnt, UINT nFlags) ‡ À†ዙǣ void CSketcherView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default CView::OnKeyDown(nChar, nRepCnt, nFlags); } ኼ’–”¿Š‹†‘™• 160 160
  161. 9/13/2011 п lý –ŠØ‰¯‹Ъ’–о„’ŠÀȋ͵Ȍ ‡ ‰‘‹”ƒ…×–Š዇†î‰Šǣ afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) ‡ ž…–Šƒ•ዎǣ ± Šƒ”ǣšž…¯ዋŠ ’ŠÀ¯ዛዘ…ኸȋ˜‹”–—ƒŽ‡›…‘†‡ȌǤ ȋ0ዛዘ…¯ዋŠ‰ŠÂƒ–”‘‰‹—•‡”ǤŠȌ ± ‡’–ǣዎŽኹŽ዁’ȋŠ‹‰‹ዟ’ŠÀȌ ±  Žƒ‰•ǣšž…¯ዋŠ •…ƒ…‘†‡ǡ–”኶‰–Šž‹’ŠÀ–”ዛዔ…ǡ ’ŠÀŠኸȀŠ኷ǡǥ ± À†ዙǣ ‡ ‹–ͳ͵ȋ‘–‡š–‘†‡Ȍαͳዅ—’ŠÀŽ–¯ዛዘ…Šኸ° ‡ Bit 15 (Key-–”ƒ•‹–‹‘ȌαͲዅ—ŽŠኸȋ’”‡••‡†Ȍǡαͳዅ—ŽŠ኷ (released) ኼ’–”¿Š‹†‘™• 161 161
  162. 9/13/2011 nChar (virtual key code) ȂÀ†к ኼ’–”¿Š‹†‘™• 162 162
  163. 9/13/2011 Ví †к‰‹ƒ‘–‹Ц’„’ŠÀ ‡ Šዠ Š‹ዉ thay ¯ዐ‹ con –”ው –ዛዓ‰ ዜ‰ ˜ዔ‹ các phím Šዌ ‡ ዞ lý •ዠ ‹ዉ: OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) ኼ’–”¿Š‹†‘™• 163 163
  164. 9/13/2011 2.6. г’ –Š‘З‹ȋ‹ƒŽ‘‰•Ȍ ‡ ʹŽ‘኶‹ǣ ± ‹ƒŽ‘‰ȋ ዒ’–Š‘኶‹–ŠØ‰–Šዛዕ‰Ȁ–‹ዅȌ ± ‘‘‹ƒŽ‘‰ȋž…Šዒ’–Š‘኶‹…Š—›²†ዙ‰Ȍ ኼ’–”¿Š‹†‘™• 164 164
  165. 9/13/2011 ʹǤ͸ǤͳǤ г’–Š‘З‹ȋ‹ƒŽ‘‰•Ȍ ‡ Dialog box: ± ዒ–…ዞƒ•ዐȋ™‹†‘™Ȍ ± ኮ‰†ዙ‰–Šዛዕ‰†î‰†‹ƒŽ‘‰•…Šዜƒ…ž…¯‹዆—Š‹዇ ȋ…‘–”‘Ž•Ȍȋ„኷…Šኸ–…ñ‰Ž…ዞƒ•ዐȌ ‡ ʹ›ዅ—–ዎ…ዚƒͳ†‹ƒŽ‘‰–”‘‰ዜ‰†ዙ‰ ǣ ± ‹ዉ኶‘˜ኼ–Žý…ዚƒ†‹ƒŽ‘‰ȋ¯ዛዘ…¯ዋŠ‰ŠÂƒ–”‘‰ˆ‹Ž‡ resource) ± 0ዎ‹–ዛዘ‰Žዔ’†‹ƒŽ‘‰ȋ†î‰¯዇“—኷Žý…ž…Š‘኶–¯ዒ‰…ዚƒ †‹ƒŽ‘‰˜…ž…¯‹዆—Š‹዇¯዁––”²×Ȍ ‡  …—‰…ኸ’Žዔ’…ዓ•ዖ‹ƒŽ‘‰ǡ…×–Š዇•ዞ†ዙ‰Žዔ’ †ኻš—ኸ––ዝ×…Š‘…ž…†‹ƒŽ‘‰•ƒ—Š‹¯ –኶‘”ƒ ”‡•‘—”…‡ȋ†‹ዉ኶‘–Š‹ዅ–ዅȌǤ ኼ’–”¿Š‹†‘™• 165 165
  166. 9/13/2011 Š‹Ц–Ц†‹ƒŽ‘‰ȋ”‡•‘—”…‡Ȍ ‡ ዖ‡•‘—”…‡‹‡™ǡ…Š‘’Š±’ǣ ± Š‹ዅ–ዅͳ†‹ƒŽ‘‰–”²˜î‰Ž˜‹ዉ ± ኸ›…ž…¯‹዆—Š‹዇ȋ…‘–”‘ŽȌ–ዝ‘‘Ž‘š ± ኶‘†‹ƒŽ‘‰ዔ‹„ኾ‰ •‡”–‹ƒŽ‘‰ ‡ 0ዋŠ†ƒŠ†‹ƒŽ‘‰ǣ ̴  166 166
  167. 9/13/2011 Н’–”¿Š…Š‘†‹ƒŽ‘‰ȋͳȌ ‡ ዏʹŠÀƒ…኶Š…ኹ‰‹኷‹“—›ዅ–ǣ ± ‹዇–Šዋȋ‰ዌ‹Ȍ†‹ƒŽ‘‰¯×Ž² ± ኼ’–”¿ŠšዞŽý•ዠ‹ዉ…Š‘…ž…¯‹዆—Š‹዇¯዁– –”²†‹ƒŽ‘‰¯× ኼ’–”¿Š‹†‘™• 167 167
  168. 9/13/2011 Н’–”¿Š…Š‘†‹ƒŽ‘‰ȋʹȌ ‡ ኽŽዔ’“—኷Žý cho dialog sau Š‹¯ –Š‹ዅ–ዅ ”‡•‘—”…‡„ኾ‰ ž…Š…Šዌ†† Žƒ••ȋ…Šዌ Š—ዒ–’Š኷‹–”² †‹ƒŽ‘‰…ኹƒ††Ȍ ኼ’–”¿Š‹†‘™• 168 168
  169. 9/13/2011 е’“—ИŽý†‹ƒŽ‘‰Ȃ ‹ŠŠЭƒ // CPenDialog dialog class CPenDialog : public CDialogEx { DECLARE_DYNAMIC(CPenDialog) public: CPenDialog(CWnd* pParent = NULL); // standard constructor virtual ~CPenDialog(); // Dialog Data enum { IDD = IDD_PENWIDTH_DLG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support DECLARE_MESSAGE_MAP() }; ኼ’–”¿Š‹†‘™• 169 169
  170. 9/13/2011 Modal và Modaless Dialogs ‡ ×ʹŽ‘኶‹†‹ƒŽ‘‰•ǣ‹዇—‘†ƒŽ˜‘†ƒŽ‡••ȋŠž… Šƒ—˜዆…ž…ŠŠ‘኶–¯ዒ‰Ȍ ‡ Modal dialog: ± Š‹Š‹዇–ŠዋŽ–ኸ–…኷…ž……ዞƒ•ዐŠž……ዚƒዜ‰†ዙ‰•ዄ –”‡‘…Š‘¯ዅŠ‹×¯ዛዘ…¯×‰ȋ–Šዛዕ‰Ž…Šዌǡ Cancel) ± ኶‘„ኾ‰…ž…ŠŠƒ‹„ž‘¯ዎ‹–ዛዘ‰Žዔ’“—኷Žý†‹ƒŽ‘‰ǡ˜ ‰ዌ‹Š‘‘†ƒŽȋȌǤ ‡ ‘†‡Ž‡••†‹ƒŽ‘‰ǣȋ‰ዛዘ…Ž኶‹Ȍ ± Š‹Š‹዇–Šዋ×ǡ˜ኻ…×–Š዇…Šዌ˜‘…ž……ዞƒ•ዐŠž……ዚƒ ዜ‰†ዙ‰Ǥ ± ×–Š዇–኶‘„ኾ‰…ž…Š†î‰Š‹ƒŽ‘‰ǣǣ”‡ƒ–‡ ኼ’–”¿Š‹†‘™• 170 170
  171. 9/13/2011 ‹Ш–ŠЬ†‹ƒŽ‘‰ ‡ ž…¯ዋŠŠ‹‘—ዎŠ‹዇–Šዋ†‹ƒŽ‘‰ȋ’Šዙ–Š—ዒ… ˜‘ዜ‰†ዙ‰ȌǤÀ†ዙǣŠ‹…Šዌዒ–‡—‹–‡ǡ…Ž‹… ˜‘ዒ–ï–ŽዉŠǡǥ ‡ ‹ዅ– ŽዉŠ‰ዌ‹Š‹዇–Šዋ†‹ƒŽ‘‰¯ …Š—ኺ„ዋ–኶‹ዓ‹ ¯ šž…¯ዋŠǤ‹ŠŠዌƒǣ // Handler for the pen width menu item void CSketcherDoc::OnPenWidth() { CPenDialog aDlg; // Create a local dialog object aDlg.DoModal(); // Display the dialog as modal } ‡ Šïýǣኹ͓‹…Ž—†‡Š‡ƒ†‡”ˆ‹Ž‡…ዚƒŽዔ’“—኷Žý˜‘ ˆ‹Ž‡ ‰—ዏ…ŠዜƒŠƒ‹„ž‘¯ዎ‹–ዛዘ‰Žዔ’“—኷Žý dialog. ኼ’–”¿Š‹†‘™• 171 171
  172. 9/13/2011 0א‰ȋ…Ž‘•‡Ȍ†‹ƒŽ‘‰ ‡ ï–ŽዉŠǡƒ…‡Ž ‡ ኷‘ȋ˜‹”–—ƒŽȌ–ዛዓ‰ዜ‰ǣ ± ‹ƒŽ‘‰ǣǣȋȌ–”኷˜዆ ï–ȋ዁…¯ዋŠ Ȍ ± ‹ƒŽ‘‰ǣǣƒ…‡ŽȋȌ–”኷˜዆ ï–ƒ…‡Žȋ Ȍ ‡ ×–Š዇–Š‹ዅ–ዅǡ˜‡””‹†‡Ž኶‹ŠšዞŽý•ዠ‹ዉ…Ž‹… ž…ï–›¯዇–Šዠ…Š‹ዉŠዟ‰ŠŠšዞ–ŠÀ…ŠŠዘ’ ኼ’–”¿Š‹†‘™• 172 172
  173. 9/13/2011 Hàm MessageBox (1) ‡ nType: what buttons display ? ኼ’–”¿Š‹†‘™• 173 173
  174. 9/13/2011 Hàm MessageBox (2) ‡ nType: button OR Icon ኼ’–”¿Š‹†‘™• 174 174
  175. 9/13/2011 2.6.2. Common Dialogs (1) ‡  …—‰…ኸ’…ž…Šዒ’–Š‘኶‹…Š—›²†ዙ‰Šዑ –”ዘŠዟ‰–Šƒ‘–ž…’Šዐ„‹ዅ–”‘‰…ž…ዜ‰ †ዙ‰–”²‹†‘™•ǣ…Šዌ ‘–ǡ…Šዌ—ǡ ’‡Ȁƒ˜‡ˆ‹Ž‡ǡ ኸǡǥ ‡ ž…Ž‘኶‹Šዒ’–Š‘኶‹›¯዆—…׎ዔ’ “—኷ lý ኼ’–”¿Š‹†‘™• 175 175
  176. 9/13/2011 Common Dialogs (2) ‡ Nguyên –ኽ ‰ዌ‹ các †‹ƒŽ‘‰•…Š—›²†ዙ‰ǣ ± ኶‘ ¯ዎ‹ –ዛዘ‰ –Š—ዒ Žዔ’ Dialog ኹ dùng (cung ኸ’ các thông tin ዓ „኷) ± ዌ‹ hàm DoModal() ¯዇ Š‹዇ –Šዋ ± ኸ› các giá –”ዋ quan tâm –ዝ ¯ዎ‹ –ዛዘ‰ ¯ khai báo ȋƒ—Š‹–ዛዓ‰–ž…˜ዔ‹‰ዛዕ‹†î‰Ȍ ኼ’–”¿Š‹†‘™• 176 176
  177. 9/13/2011 CFileDialog (1) ‡ ዔ’ ¯×‰ gói làm ˜‹ዉ ˜ዔ‹ Šዒ’ –Š‘኶‹ –ኼ’ tin thông †ዙ‰ ዚƒ ‹†‘™•ȋ’‡ǡƒ˜‡•ǡǥȌ ‡ Šз‹–З‘ ‹Ž‡‹ƒŽ‘‰::CFileDialog ‡ Šዖ‹ –኶‘ዒ–¯ዎ‹–ዛዘ‰Šዒ’–Š‘኶‹–ኼ’–‹‹†‘™• Š—ኺǤ CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL ); ኼ’–”¿Š‹†‘™• 177 177
  178. 9/13/2011 CFileDialog (2) ‡ bOpenFileDialog: ± TRUE: Open ± FALSE: Save As ‡ lpszDefExt: ± 0—Ø‹ዖ”ዒ‰዁…¯ዋŠ…ዚƒ–²–ኼ’–‹Ǥዅ—‰ዛዕ‹†î‰ ŠØ‰Šኼ’°¯—Ø‹ዖ”ዒ‰–”‘‰Ø‡†‹–„‘š…ዚƒ ‹Ž‡ ƒ‡ǡ’Šኹዖ”ዒ‰¯ዛዘ……Šዊ¯ዋŠ„ዖ‹Ž’•œ‡ˆš–•ዄ ¯ዛዘ…–ዠ¯ዒ‰–Š²˜‘–²…ዚƒ–ኼ’–‹Ǥዅ—‰‹ž–”ዋ›Ž ǡ•ዄŠØ‰…×’Šኹዖ”ዒ‰¯ዛዘ…–Š²˜‘Ǥ ‡ lpszFileName: ± ²–ኼ’–‹Šዖ‹–኶‘•ዄ¯ዛዘ…š—ኸ–Š‹ዉ–”‘‰Ø‡†‹–„‘š ዚƒ ‹Ž‡ƒ‡Ǥዅ—‰‹ž–”ዋ›Žǡ•ዄŠØ‰…×–² –ኼ’–‹‘š—ኸ–Š‹ዉǤ ኼ’–”¿Š‹†‘™• 178 178
  179. 9/13/2011 CFileDialog (3) ‡ dwFlags: ± ዒ–’Š±’ዅ–Šዘ’…ዚƒዒ–Šƒ›Š‹዆—„‹ዅ…ዕ…Š‘’Š±’ –î›…ŠዊŠŠዒ’–Š‘኶‹ǤKhi ŠዊŠ•ዞƒ„‹ዅ–ŠŠ˜‹²…ኸ— –”̴‘ˆǤ Žƒ‰•ǡ†î‰ዒ––‘ž–ዞ–”‘‰Šዟ‰–Šƒ› ¯ዐ‹¯዇‰‹ዟŠዟ‰–Š‹ዅ–Žኼ’዁…¯ዋŠŠØ‰„ዋ኷Š Šዛዖ‰Ǥ ‡ lpszFilter: ± ዒ–† ›…ž……዁’…Š—ዑ‹…Šዊ¯ዋŠŠዟ‰„ዒŽዌ…„኶…×–Š዇ ž’†ዙ‰…Š‘–ኼ’–‹Ǥዅ—„኶…Šዊ¯ዋŠ„ዒŽዌ…–ኼ’–‹ǡ…Šዊ Šዟ‰–ኼ’–‹…×–”‘‰„ዒŽዌ…ዔ‹š—ኸ–Š‹ዉ–”‘‰Ž‹•– box. ‡ pParentWnd: ± ዒ–…‘–”ው–”ው–ዔ‹¯ዎ‹–ዛዘ‰Šዒ’–Š‘኶‹–ኼ’–‹Ž…ŠƒŠƒ› ዞƒ•ዐ…ŠዚǤ ኼ’–”¿Š‹†‘™• 179 179
  180. 9/13/2011 Các ’Šмд‰ –Šн Žе’ CFileDialog ‡ CFileDialog::DoModal() - –”኷˜዆ Šƒ› Ǥ(–ዛዓ‰ ዜ‰ ‰ዛዕ‹†î‰ Šዌï–Šƒ›ï–ƒ…‡Ž). - Š‹‰ዛዕ‹†î‰…Ž‹…ï–Šƒ›ƒ…‡ŽǡŠƒ›…Šዌ…Šዜ… £‰Ž‘•‡–ዝ¯዆—Š‹዇‡—…ዚƒŠዒ’–Š‘኶‹ǡ“—›዆¯‹዆— Š‹዇¯ዛዘ…–”ƒ‘Ž኶‹…Š‘ዜ‰†ዙgǤƒ—¯×ǡ ×–Š዇‰ዌ‹…ž…Š –ŠŠ˜‹²Šž…¯዇Šኼ˜዆…ž…–Š‹ዅ–Žኼ’Šƒ›…ž…–ŠØ‰–‹ ‰ዛዕ‹†î‰Šኼ’˜‘Šዒ’–Š‘኶‹Ǥ - ‘‘†ƒŽŽዒ–Š኷‘ዅ–Šዝƒ–ዝŽዔ’‹ƒŽ‘‰Ǥ ኼ’–”¿Š‹†‘™• 180 180
  181. 9/13/2011 Các ’Šмд‰ –Šн Žе’ CFileDialog ‡ GetFileExt: Žኸ›’Šኹዖ”ዒ‰…ዚƒ–ኼ’–‹¯ዛዘ… Šዌ ‡ GetFileName: Žኸ›–²–ኼ’–‹¯ዛዘ……Šዌ ‡ GetFileTitle: Žኸ›’Šኹ–²ȋŠØ‰…×’Šኹዖ ”ዒ‰Ȍ ዚƒ–ኼ’–‹¯ዛዘ……Šዌ ‡ GetFolderPath: Žኸ›–Šዛዙ……ዚƒ–ኼ’–‹¯ዛዘ……Šዌ ‡ GetPathName: Žኸ›¯ዛዕ‰†ኻ…ዚƒ–ኼ’–‹¯ዛዘ Šዌ ኼ’–”¿Š‹†‘™• 181 181
  182. 9/13/2011 CFileDialog - Open TCHAR szFilters[] = _T("Text files(*.txt)¦*.txt¦All files *.*)¦*.*¦¦"); CFileDialog dlg(TRUE,_T("txt"),_T("*.txt"),OFN_FILEMUSTEXIST¦OFN_HIDEREADONLY,z Filters); if(dlg.DoModal() == IDOK) { filename = dlg.GetPathName(); // Open the file and read it. } ኼ’–”¿Š‹†‘™• 182 182
  183. 9/13/2011 CFileDialog Ȃ Save As void CChildView::OnFileSaveAs() { CFileDialog dlg(FALSE,_T("phn"),m_strPathName, OFN_OVERWRITEPROMPT¦OFN_PATHMUSTEXIST ¦OFN_HIDEREADONLY, m_szFilters); if(dlg.DoModal() == IDOK) { if(SaveFile(dlg.GetPathName())) m_strPathName = dlg.GetPathName(); } ኼ’–”¿Š‹†‘™• 183 183
  184. 9/13/2011 CColorDialog ‡ Constructor: ± CColorDialog( COLORREF clrInit = 0, DWORD dwFlags = 0, CWnd* pParentWnd = NULL ) ‡ Methods: ± virtual int DoModal() ± COLORREF GetColor() const ± static COLORREF* PASCAL GetSavedCustomColors ± protected virtual BOOL OnColorOK() ± void SetCurrentColor( COLORREF clr ) ኼ’–”¿Š‹†‘™• 184 184
  185. 9/13/2011 CColorDialog CColorDialog dlg(RGB(255, 0, 0), CC_FULLOPEN); if(dlg.DoModal() == IDOK) { /////////////////////////////////// AfxMessageBox("Chon mau", 0, 0); m_oColor = dlg.GetColor(); } ኼ’–”¿Š‹†‘™• 185 185
  186. 9/13/2011 CFontDialog ‡ –኶‘Ȁ‘•–”—…–‘”ǣ ± CFontDialog( const CHARFORMAT& charformat, DWORD dwFlags = CF_SCREENFONTS, CDC* pdcPrinter = NULL, CWnd* pParentWnd = NULL ); ኼ’–”¿Š‹†‘™• 186 186
  187. 9/13/2011 CFontDialog ‡ Parameters: ± lplfInitial: A pointer to a LOGFONT data structure that allows you to set some of the font's characteristics. ± charFormat: A pointer to a CHARFORMAT data structure that allows you to set some of the font's characteristics in a rich edit control. ± dwFlagsSpecifies one or more choose-font flags. One or more preset values can be combined using the bitwise OR operator. If you modify the m_cf.Flags structure member, be sure to use a bitwise OR operator in your changes to keep the default behavior intact. For details on each of these flags, see the description of the CHOOSEFONT structure in the Platform SDK. ± pdcPrinter: A pointer to a printer-device context. If supplied, this parameter points to a printer-device context for the printer on which the fonts are to be selected. ± pParentWnd: A pointer to the font dialog box's parent or owner window. ኼ’–”¿Š‹†‘™• 187 187
  188. 9/13/2011 ʹǤ͹Ǥž…¯‹Ч—Š‹Ш–ŠØ‰†к‰ ‡ 2.7.1. Các ¯‹዆— Š‹዇ –ŠØ‰†ዙ‰ ‡ ʹǤ͹ǤʹǤŠ‹ዅ–ዅ…ž…¯‹዆—Š‹዇–”²Šዒ’–Š‘኶‹ ‡ ʹǤ͹Ǥ͵Ǥž…˜ኸ¯዆…ŠÀŠ˜ዔ‹…ž…¯‹዆—Š‹዇ ‡ ʹǤ͹ǤͶǤዔ’ …ዚƒ…ž…¯‹዆—Š‹዇ ‡ ʹǤ͹ǤͷǤ‹–ኼ’ ኼ’–”¿Š‹†‘™• 188 188
  189. 9/13/2011 2.7.1. Các ¯‹Ч— Š‹Ш –ŠØ‰†к‰ ‡ Static text Click & Drag/Drop ‡ Edit box ‡ Command button ‡ Check box ‡ Radio button ‡ Drop-down list box (combo box) ‡ Sliders, tree and list controls, ’”‘‰”‡••„ƒ”•ǡǥ ኼ’–”¿Š‹†‘™• 189 189
  190. 9/13/2011 ʹǤ͹ǤʹǤŠ‹Ц–Ц…ž…¯‹Ч—Š‹Ш–”²Šг’–Š‘З‹ȋͳȌ ‡ ዠƒ…Šዌ¯‹዆—Š‹዇—ዎ†î‰–”‘‰‘‘Ž„‘šǤ Šƒ‘–ž……Ž‹…Š‘዁…±‘–Š኷Ǥ ‡ Š‹ዅ–ዅ…ž…¯‹዆—Š‹዇¯዁––”²†‹ƒŽ‘‰ȋ•ኽ’šዅ’ǡ…£ ŠዊŠȌ ኼ’–”¿Š‹†‘™• 190 190
  191. 9/13/2011 Š‹Ц–Ц¯‹Ч—Š‹Ш–”²Šг’–Š‘З‹ȋʹȌ ‡ Š‹ዅ–Žኼ’…ž…–Š—ዒ…–ÀŠ…Š‘¯‹዆—Š‹዇–኶‹ –Šዕ‹¯‹዇–Š‹ዅ–ዅǤ ‡ ዞƒ•ዐ”‘’‡”–‹‡•…ዚƒ¯‹዆—Š‹዇ ኼ’–”¿Š‹†‘™• 191 191
  192. 9/13/2011 Š‹Ц–Ц…ž…¯‹Ч—Š‹Ш–”²Šг’–Š‘З‹ȋ͵Ȍ ‡ ዞ†ዙ‰ƒ„”†‡”ǣ ± Format/Tab Order (Ctrl+D): Š‹ዅ– Žኼ’ –Šዜ –ዠ Šዌȋˆ‘…—•Ȍ „ኾ‰ phím Tab cho các ¯‹዆—Š‹዇ ± Click Š—ዒ– ¯዇ Šዌ –Šዜ –ዠ. 192 192
  193. 9/13/2011 Š‹Ц–Ц…ž…¯‹Ч—Š‹Ш–”²Šг’–Š‘З‹ȋ͵Ȍ ‡ ዞ†ዙ‰‡‘‹…•ǣ ± ¯዇–Šƒ‘–ž…˜ዔ‹¯‹዆—Š‹዇„ኾ‰’ŠÀ–ኽ– (Alt + ?) ± Ký –ዠ ‰኶…Š chân trong caption ዚƒ ¯‹዆—Š‹዇ ‡ Ví †ዙ: ± —––‘ǣDzŠ‘™Ƭ‡••ƒ‰‡dzAlt+M ‡ ‹዇ tra: Format/Check Mnemonic ኼ’–”¿Š‹†‘™• 193 193
  194. 9/13/2011 ʹǤ͹Ǥ͵Ǥž…˜Й¯Ч ŠÀŠ˜е‹…ž…¯‹Ч—Š‹ШȋͳȌ ‡ ዟ Ž‹ዉ— ¯ዛዘ Žዛ— trên ž…¯‹዆—Š‹዇là gì ? ȋኹŽኸ›Ȁ‹’—–Šƒ›Š‹ዉȀ‘—–’—–†ዟ Ž‹ዉ—) ‡ ኸ›ȀŠ‹ዉ†ዟŽ‹ዉ—…ዚƒ…ž…¯‹዆—Š‹዇„ኾ‰ cách nào ? ‡ ዟŽ‹ዉ— Žኸ›˜‘–ዝ¯‹዆—Š‹዇…׊ዘ’ Žዉ ? ‡ ‘኶‹•ዠ‹ዉ‰¿…ኹ†î‰Š‹–ዛዓ‰–ž…˜ዔ‹ ¯‹዆—Š‹዇ǫȋŠšዞŽý•ዠ‹ዉ…ኹ†î‰Ȍ ‡ Có nhu ኹ— –Šƒ‘–ž…˜ኼ–Žý¯ዎ‹˜ዔ‹¯‹዆—Š‹዇ ¯×ŠØ‰ǫȋThay ¯ዐ‹ Šዟ, di Š—›዇, ‡ƒ„Ž‡Ȁ†‹•ƒ„Ž‡ǡ•Š‘™ȀŠ‹††‡ǡǥȌ ኼ’–”¿Š‹†‘™• 194 194
  195. 9/13/2011 ž…˜Й¯Ч…ŠÀŠ˜е‹…ž…¯‹Ч—Š‹ШȋʹȌ ‡ 0዇Žኸ›Ȁ¯ዛƒ†ዟŽ‹ዉ—–ዝȀ¯ዅ…ž…¯‹዆—Š‹዇…ኹ–ŠØ‰ “—ƒ„‹ዅ“—኷ŽýȋŽ‘኶‹ƒŽ—‡Ȍ¯ዛዘ…¯£‰ýȋ‰ኽȌ ˜ዔ‹¯‹዆—Š‹዇Ǥ ‡ ኹ•ዞ†ዙ‰Š…Š‘’Š±’¯ዏ‰„ዒ†ዟŽ‹ዉ—‰‹ዟƒ‰‹ž –”ዋ„‹ዅኽ‰‹ዟ˜ዒ‹†—‰Š‹዇–Šዋ–”²¯‹዆— Š‹዇ȋŠ‹–ዛዓ‰–ž…˜ዔ‹‰ዛዕ‹†î‰Ȍ ‡ 0዇–Šዠ…Š‹ዉ–Šƒ‘–ž…˜ኼ–Žý˜ዔ‹¯‹዆—Š‹዇…ኹ‰ዌ‹ ž…’Šዛዓ‰–Šዜ…–ŠÀ…ŠŠዘ’ȋ…ዚƒŽዔ’…ዞƒ•ዐȌǤŠØ‰ “—ƒ„‹ዅŽ‘኶‹‘–”‘Ž¯ዛዘ…¯£‰ý˜ዔ‹¯‹዆—Š‹዇Ǥ ‡ Šƒ‹„ž‘˜˜‹ዅ–ŠšዞŽý•ዠ‹ዉ…Š‘¯‹዆—Š‹዇ ኼ’–”¿Š‹†‘™• 195 195
  196. 9/13/2011 0£‰ ký „‹Ц cho ¯‹Ч—Š‹ШȋͳȌ ‡ ×ʹŽ‘኶‹„‹ዅ…×–Š዇‰ኽ˜ዔ‹¯‹዆—Š‹዇–î› ዙ…¯À…Š: ± 0዇ tham Š‹ዅ— ¯ዅ ¯‹዆— Š‹዇ (thay ¯ዐ‹ †‹ዉ ኶‘) „‹ዅŽ‘኶‹‘–”‘Ž ± 0዇ Šኼ/¯ዛƒ †ዟ Ž‹ዉ— –ዝ/¯ዅ ¯‹዆— Š‹዇ „‹ዅŽ‘኶‹ƒŽ—‡ ‡ Šዠ Š‹ዉ: ± Šዌ ¯‹዆— Š‹዇ —ዎ¯£‰ý„‹ዅ ± Format/Add variable ‘዁ Right Mouse/Add variable ኼ’–”¿Š‹†‘™• 196 196
  197. 9/13/2011 0£‰ ký „‹Ц cho ¯‹Ч—Š‹ШȋʹȌ Tênǡ‹዇—†ዟ Šዌ Ž‘኶‹ „‹ዅǣ Ž‹ዉ—…ዚƒ„‹ዅ Control/Value ²„‹ዅ Chú thích š—ኸ– Š‹ዉ trong code ኼ’–”¿Š‹†‘™• 197 197
  198. 9/13/2011 0а‰ „г †рŽ‹Ъ—‰‹рƒ¯‹Ч—Š‹Ш/„‹Ц ‡ ዙ ¯À…Š: ± 0ዏ‰ „ዒ giá –”ዋ ዚƒዒ– ¯‹዆— Š‹዇ ˜ዔ‹ „‹ዅ Ž‘኶‹ giá –”ዋ ዚƒ nó ± ኼ’ Šኼ– khi ኹ Šኼ †ዟ Ž‹ዉ— –ዝ ¯‹዆— Š‹዇ Š‘዁ ¯ዛƒ †ዟ Ž‹ዉ— ra ¯‹዆— Š‹዇. ‡ Šዠ Š‹ዉ„ኾ‰Šǣ BOOL UpdateData(BOOL bSaveAndValidate) ± „ƒ˜‡†ƒŽ‹†ƒ–‡αȋͳȌǣዟŽ‹ዉ—–”²¯‹዆— Š‹዇¯ዛዘ……ኼ’Šኼ–˜‘„‹ዅǤ ± „ƒ˜‡†ƒŽ‹†ƒ–‡α ȋͲȌǣዟŽ‹ዉ—Žዛ—‰‹ዟ –”‘‰„‹ዅ¯ዛዘ…¯ዛƒ”ƒ¯‹዆—Š‹዇Ǥ ኼ’–”¿Š‹†‘™• 198 198
  199. 9/13/2011 0£‰ ký hàm šп lý •с ‹Ъ (1) ‡ Có Š‹዆— •ዠ ‹ዉ khác Šƒ—˜ዔ‹ ž…¯‹዆—Š‹዇ ± Mouse down/up, DBClickǡ‘˜‡ǡǥ ± ‡›†‘™Ȁ—’ǡ’”‡••‡†ǡǥ ± ‹–ǡ…”‡ƒ–‡ǡ’ƒ‹–ǡǥ ‡ Š²ŠšዞŽý•ዠ‹ዉ‘‰ —ዎ ± C1: Right Mouse/ Add Event Handler ± C2: Properties Window/ Control Events ኼ’–”¿Š‹†‘™• 199 199
  200. 9/13/2011 0£‰ýŠšп lý •с ‹Ъ (2) ‡ Ví †ዙ: Button ShowMSG Add Event Handler Šዌ •ዠ Šዌ Žዔ’Šኼ–ŠØ‰ ‹ዉ ¯‹ዉ’–ዛዓ‰ዜ‰ ²ŠšዞŽý ¯ዛዘ…‰ዘ‹ý ኼ’–”¿Š‹†‘™• 200 200
  201. 9/13/2011 Šƒ‘–ž…˜Н–Žý˜е‹…ž…¯‹Ч—Š‹Ш ‡ Š‘’Š±’ȀŠØ‰…Š‘’Š±’ȋ‡ƒ„Ž‡Ȁ†‹•ƒ„Ž‡Ȍ–ዛዓ‰ –ž…˜ዔ‹¯‹዆—Š‹዇Ǥ BOOL CWnd::EnableWindow(BOOL bEnable) ‡ ኌȀ ‹ዉȋ ‹††‡ȀŠ‘™Ȍ¯‹዆—Š‹዇ BOOL CWnd::ShowWindow(int iCmdShow) ‡ Hàm: CWnd* CWnd::GetDlgItem(int nID) ȀȀኸ›…‘–”ው¯ዅ¯‹዆—Š‹዇Ȁ…ዞƒ•ዐ…‘–”‘‰…ዞƒ •ዐ…ŠƒǤÀ†ዙ CEdit* pBoxOne; pBoxOne = (CEdit*)GetDlgItem(IDC_MYEDIT); ኼ’–”¿Š‹†‘™• 201 201
  202. 9/13/2011 ʹǤ͹ǤͶǤе’ …лƒ…ž…¯‹Ч—Š‹Ш ‡  …—‰…ኸ’Žዔ’¯኶‹†‹ዉ…Š‘…ž…¯‹዆—Š‹዇Ǥ ‡ ዒ–•ዎŽዔ’˜À†ዙǣ ‡ ኷…Šኸ–…ž…¯‹዆—Š‹዇ȋ…‘–”‘Ž•Ȍ…ñ‰Ž…ž……ዞƒ•ዐ¯዁… „‹ዉ–Ǥž…Žዔ’…ዚƒ¯‹዆—Š‹዇†ኻš—ኸ––ዝŽዔ’…ዓ•ዖ†Ǥ‘ ¯×…×–Š዇•ዞ†ዙ‰¯ዅ…ž…Š–ŠÀ…ŠŠዘ’…ዚƒŽዔ’›Ǥ ‡ ‰‘‹–኶‘¯‹዆—Š‹዇Žï…–Š‹ዅ–ዅȋ‡•‘—”…‡Ȍǡ…×–Š዇–኶‘¯‹዆— Š‹዇„ኾ‰ŠCreate ȋŽዔ’…ዓ•ዖ†ȌŽï…–Šዠ…–Š‹Ǥ ኼ’–”¿Š‹†‘™• 202 202
  203. 9/13/2011 ʹǤ͹ǤͶǤͳǤе’†‹–ȋͳȌȂ Šз‹–З‘ ‡ —኷ŽýŠዒ’Šኼ’ǡšዞŽýŠ‘኶–¯ዒ‰Šኼ’Ž‹ዉ—ǡ–ŠØ‰ –‹˜዆–¿ŠŠ¿ŠŠኼ’Ž‹ዉ—…Š‘…ዞƒ•ዐ…ŠƒǤ ‡ –኶‘ǣ†‹–ȋȌ ‡ ”‡ƒ–‡ǣŠዖ‹–኶‘¯ዎ‹–ዛዘ‰Šዒ’Šኼ’˜ዔ‹…ž… –Šƒ•ዎǤ BOOL Create ( †™–›Ž‡ǡȀȀ‹዇—Šዒ’Šኼ’ȋ̴  ǡ ES_PASSWORD, ES_READONLY) ‘•–Ƭ”‡…–ǡȀȀዌƒ¯ዒ˜À…Š–Šዛዔ…Šዒ’Šኼ’ †ȗ’ƒ”‡–†ǡȀȀ‘–”ው–ዔ‹¯ዎ‹–ዛዘ‰…ዞƒ•ዐ…Šƒ   ȀȀ0ዋŠ†ƒŠŠዒ’Šኼ’ ); ኼ’–”¿Š‹†‘™• 203 203
  204. 9/13/2011 е’†‹–ȋʹȌȂ Hàm thành viên ‡ 3 nhóm hàm thành viên: ± †‹–––”‹„—–‡•ǣŠ×Š˜዆–Š—ዒ…–ÀŠ…ዚƒŠዒ’Šኼ’Ǥ ȋÀ…Š–Šዛዔ…ǡ˜ዋ–”Àǡ•ዎ†Ö‰ǡ—†‘ǡǥȌ ± †‹–’‡”ƒ–‹‘•ǣŠ×Š˜዆Š‘኶–¯ዒ‰…ዚƒŠዒ’Šኼ’ ± †‹–Ž‹’„‘ƒ”†’‡”ƒ–‹‘•ǣŠ×Š˜዆–Šƒ‘–ž…•‘኶ –Š኷‘–”²Šዒ’Šኼ’ȋ‘’›ǡ’ƒ•–‡ǡ…—–ǡ…Ž‡ƒ”ǡ—†‘Ȍ ‡ À†ዙዒ–•ዎŠǣ ± void GetRect ( LPRECT lpRect); ± BOOL GetModify( ); ± void SetModify(BOOL bModified = TRUE); ± BOOL SetReadOnly( BOOL bReadOnly = TRUE ); ± void SetPasswordChar (TCHAR ch); ± (Xem thêm MSDN) ኼ’–”¿Š‹†‘™• 204 204
  205. 9/13/2011 е’†‹–ȋ͵ȌȂ пŽý•с‹Ъ ‡ ž…–ŠØ‰¯‹ዉ’•ዠ‹ዉ–”²Šዒ’Šኼ’Ž‹ዉ— ¯ዛዘ…‰ዞ‹¯ዅ…ዞƒ•ዐ…ŠƒǤ ‡ ዙ…šዞŽý–ŠØ‰¯ዛዘ…¯ዛዘ…–Š²˜‘žŠš኶ –ŠØ‰¯‹ዉ’ȋ‡••ƒ‰‡ƒ’Ȍ–”‘‰Žዔ’…ዞƒ•ዐ Šƒ…׆኶‰ǣ ± ON_Notification (ID, memberFxn ) ± ”‘‰¯×ǣ šž…¯ዋŠ¯ዋŠ†ƒŠŠዒ’Šኼ’Ǣ ‡„‡” š–²ŠšዞŽý–ŠØ‰¯‹ዉ’–ዛዓ‰ዜ‰Ǥ ± À†ዙǣON_EN_UPDATE(IDC_EDIT1, &CMyDialogDlg::OnEnUpdateEdit1) ኼ’–”¿Š‹†‘™• 205 205
  206. 9/13/2011 е’†‹–ȋͶȌȂ пŽý•с‹Ъ ‡ ž…–ŠØ‰¯‹ዉ’–”²Šዒ’ Šኼ’ǣ ‡ (EN = Event Notification) ኼ’–”¿Š‹†‘™• 206 206
  207. 9/13/2011 е’†‹–ȋͷȌȂ À†кǣ // dynamically create an edit control CEdit* pEdit = new CEdit; pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(10, 10, 100, 100), this, 1); //get text from edit box. CString yourname; pEdit->GetWindowText( yourname ); MessageBox ( yourname, "Hello !", MB_OK | MB_ICONEXCLAMATION ); ኼ’–”¿Š‹†‘™• 207 207
  208. 9/13/2011 ʹǤ͹ǤͶǤʹǤе’—––‘ȋͳȌȂ Šз‹–З‘ ‡ —኷Žý…ž…¯‹዆—Š‹዇†኶‰ï–…Šዌȋ—––‘ǡ ”ƒ†‹‘ǡ…Š‡…„‘šǡǥȌȋš‡„—––‘•–›Ž‡•Ȍ ‡ –኶‘—––‘ȋȌ ‡ Hàm Create() BOOL Create ( Ž’•œƒ’–‹‘ǡȀȀዒ‹†—‰–ŠØ‰„ž‘ †™–›Ž‡ǡȀȀ†኶‰ï–ȋ˜À†ዙ̴ Ȍ ‘•–Ƭ”‡…–ǡȀȀዌƒ¯ዒǡÀ…Š–Šዛዔ †ȗ’ƒ”‡–†ǡȀȀ‘–”ው…ዞƒ•ዐ…Šƒ   ȀȀ0ዋŠ†ƒŠï–…Šዌ ); ኼ’–”¿Š‹†‘™• 208 208
  209. 9/13/2011 е’—––‘ȋʹȌȂ Hàm thành viên ‡ Các hàm thành viên (xem MSDN) ‡ ዒ–•ዎŠǣ ± HBITMAP SetBitmap(HBITMAP hBitmap) ± HICON SetIcon(HICON hIcon); ± virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct ); ± int GetCheck() const; ± void SetCheck(int nCheck); ኼ’–”¿Š‹†‘™• 209 209
  210. 9/13/2011 е’—––‘ȋ͵ȌȂ пŽý•с‹Ъ ‡ ŠØ‰¯‹ዉ’–”²ï–…Šዌ ON_BN_CLICKED(IDOK, &CMyDialogDlg::OnBnClickedOk) ኼ’–”¿Š‹†‘™• 210 210
  211. 9/13/2011 е’—––‘ȋͶȌȂ À†к CButton myButton1, myButton2, myButton3, myButton4; //Khai báo trong .h ȀȀŠዛዕ‰–኶‘¯‹዆—Š‹዇–”‘‰”‡ƒ–‡…ዚƒŽዔ’…Šƒ // Create a push button. ›—––‘ͳǤ”‡ƒ–‡ȋ̴ȋDz—•Š„—––‘̶Ȍǡ̴ ȁ̴  ȁ̴ ǡ CRect(10,10,100,30), pParentWnd, 1); // Create a radio button. ›—––‘ʹǤ”‡ƒ–‡ȋ̴ȋDzƒ†‹‘„—––‘̶Ȍǡ̴ ȁ̴  ȁ̴ ǡ CRect(10,40,100,70), pParentWnd, 2); // Create an auto 3-state button. ›—––‘͵Ǥ”‡ƒ–‡ȋ̴ȋDz͵-state button"), WS_CHILD|WS_VISIBLE|BS_AUTO3STATE, CRect(10,70,100,100), pParentWnd, 3); // Create an auto check box. ›—––‘ͶǤ”‡ƒ–‡ȋ̴ȋDzŠ‡…„‘š„—––‘̶Ȍǡ̴ ȁ̴  ȁ BS_AUTOCHECKBOX, CRect(10,100,100,130), pParentWnd, 4); ኼ’–”¿Š‹†‘™• 211 211
  212. 9/13/2011 ʹǤ͹ǤͶǤ͵Ǥе’‹•–‘šȋͳȌ ‡ ዔ’“—኷ŽýŠዒ’†ƒŠ•ž…Š…ž…’Šኹ–ዞ ‡ –኶‘‹•–‘šȋȌ ‡ Hàm Create() virtual BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ); ‡ ListBox styles: LBS_XXX (LBS_MULTILESEL, ̴ǡǥȌȋ‡Ȍ ኼ’–”¿Š‹†‘™• 212 212
  213. 9/13/2011 е’‹•–‘šȋʹȌȂ Hàm thành viên ‡ Chia thành các nhóm: ± General Operations: ‡ int GetCountȋȌ–”኷˜዆•ዎዙ……Šዌ ‡ GetText, GetSelǡǥ ± Single-selection Operations: ‡ int GetCurSelȋȌŽኸ›…Šዊ•ዎዙ…¯ዛዘ……Šዌǡ ‡ int SetCurSelȋ‹–‡Ž‡…–Ȍኊ¯ዋŠዙ……Šዌ ± Multiple-selection Operations: GetSelCount, GetSelItemsǡǥ ± String Operations: AddString, DeleteString, InsertString, ResetContentǡǥ ‡ Š‹–‹ዅ–ǣ ኼ’–”¿Š‹†‘™• 213 213
  214. 9/13/2011 е’‹•–‘šȋ͵ȌȂ пŽý•с‹Ъ ‡ ዠ‹ዉ–”²Ž‹•–„‘š˜Š šዞŽý–ዛዓ‰ዜ‰ ON_LBN_SELCHANGE(IDC_LIST1, &CControlDemoDlg::OnLbnSelchangeList1) ኼ’–”¿Š‹†‘™• 214 214
  215. 9/13/2011 е’‹•–‘šȋͶȌȂ À†к int CControlDemoDlg::OnCreate( LPCREATESTRUCT lpCreateStruct ) { if (CWnd::OnCreate(lpCreateStruct) == -1) return -1; ȀȀ኶‘ዒ–Ž‹•–„‘š m_listbox- >Create ( WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_NOTIFY | WS_VSCROLL, CRect( 200, 10, 280, 80 ), this, ̴ ȌǢȀȀ ¯ዋŠ‰ŠÂƒ–”‘‰”‡•‘—”…‡ǤŠ m_listbox- >AddString( "Ong A" ); m_listbox- >AddString( "Ba B" ); m_listbox- >AddString( "Co C" ); ȀȀ኶‘ዒ–‡†‹–„‘šȋ†î‰¯዇Š‹ዉ’Šኹ–ዞ¯ዛዘ……Šዌ–ዝŽ‹•–„‘š m_edit->Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(200, 100, 280, 120), this, 1010); return 0; } ኼ’–”¿Š‹†‘™• 215 215
  216. 9/13/2011 е’‹•–‘šȂ À†кȋ–‹Ц’Ȍ void CControlDemoDlg::OnLbnSelchangeListBox() { CString info; int iSel = m_listbox->GetCurSel(); ‹ˆȋ‹‡Žαα̴ȌȀȀŠØ‰…׎ዠƒ…Šዌ L6HO 7KuFKӑQPөFÿҫXWLrQ m_listbox->GetText(iSel, info ); m_edit->SetWindowText( info); } ኼ’–”¿Š‹†‘™• 216 216
  217. 9/13/2011 ʹǤ͹ǤͶǤͶǤе’‘„‘‘šȋͳȌ ‡ ዔ’“—኷Žý…‘„‘„‘šȋŠዒ’Šኼ’˜’ŠኹŠዑ–”ዘŠኼ’˜ዔ‹ †ƒŠ•ž…Š…ž…’Šኹ–ዞ…Š‘–”ዛዔ…Ȍ ‡ –኶‘‘„‘‘šȋȌ ‡ Hàm Create() BOOL Create ( †™–›Ž‡ǡȀȀ‹዇—…‘„‘‘š const RECT& rect, CWnd* pParentWnd, UINT nID ); ‡ †™–›Ž‡ǣž…†኶‰‘„‘‘šŠž…Šƒ—ǣ ± ̴ ǣ ‹ž–”ዋŠኼ’…Šዊ¯ዛዘ……Šዌ–ዝ†ƒŠ•ž…ŠǤ ± ̴ ǣŠዛ”‘’Ž‹•–†ƒŠ•ž…ŠŠ‹዇–Šዋ–Šዛዕ‰–”ዠ ± ̴ǣŠዛ†”‘’Ž‹•–˜…×–Š዇Šኼ’ዒ‹†—‰ዔ‹ ኼ’–”¿Š‹†‘™• 217 217
  218. 9/13/2011 е’‘„‘‘šȋʹȌȂ Hàm thành viên ‡ Š‹዆—Š–ዛዓ‰–ዠ‹•–‘š ‡ 2 nhóm: ± General Operations: ‡ int GetCountȋȌ–”኷˜዆•ዎዙ……Šዌ ‡ ‡–—”‡Žǡ‡–—”‡Žǡǥ ‡ GetLBText() ኸ›–‡š–…ዚƒ’Šኹ–ዞ ± String Operations: AddString, DeleteString, InsertString, ResetContentǡǥ ‡ Xem MSDN ኼ’–”¿Š‹†‘™• 218 218
  219. 9/13/2011 е’‘„‘‘šȋ͵ȌȂ пŽý•с‹Ъ ‡ ዠ‹ዉ–”²‘„‘‘š˜ŠšዞŽý ON_CBN_SELCHANGE(IDC_COMBO1, &CControlDemoDlg::OnCbnSelchangeCombo1) ኼ’–”¿Š‹†‘™• 219 219
  220. 9/13/2011 е’‘„‘‘šȋͶȌȂ À†к m_pComboBox->Create( WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWNLIST, CRect(10,10,200,100), pParentWnd, IDC_COMBO1); void CControlDemoDlg::OnCbnSelchangeCombo1() { CString info; int iSel = m_Combobox.GetCurSel( ); if (iSel == LB_ERR) // no selection iSel = 0; m_pComboBox- >GetLBText( iSel, info ); m_edit->SetWindowText( info ); } ኼ’–”¿Š‹†‘™• 220 220
  221. 9/13/2011 ʹǤ͹ǤͶǤͷǤе’’‹—––‘–”ŽȋͳȌ ‡ ዔ’“—኷Žý¯‹዆—Š‹዇…Š‘’Š±’Žዠƒ…Šዌ‰‹ž –”ዋȋ–£‰Ȁ‰‹኷Ȍ–”‘‰ዒ–Š‘኷‰¯ …‹¯዁–Ǥ ‡ Šዛዕ‰¯‹°˜ዔ‹ዒ–‡†‹–„‘šȋ¯዇Š‹ዉ‰‹ž –”ዋ—ዎ…ŠዌȌ ‡ Hàm Create() virtual BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ); ኼ’–”¿Š‹†‘™• 221 221
  222. 9/13/2011 CSpinButtonCtrl (2) Ȃ Hàm thành viên ‡ CWnd* SetBuddy (CWnd* pWndBuddy); ȀȀኊ¯ዋŠ¯ዎ‹–ዛዘ‰Šዒ’Šኼ’ȋ‡†‹–„‘šȌ¯‹዆— Š‹዇›Šዑ–”ዘ ‡ CWnd* GetBuddy( ); ‡ ‹–‡–ƒ•‡ȋ‹–ƒ•‡ȌǢȀȀ–Š‹ዅ–Žኼ’„ዛዔ…–£‰‰‹኷ ‡ void SetRange ( int nLower, int nUpper); ȀȀኊ¯ዋŠ’Š኶˜‹‰‹ž–”ዋ„‹ዅ–Š‹²…ዚƒï––£‰ ‰‹኷Ǥ ‡ void GetRange ( int &nLower, int &nUpper); ‡ ‹–‡–‘•ȋ‹–‘•ȌǢȀȀ0዁–‰‹ž–”ዋï––£‰‰‹኷ ‡ int GetPos( int &nPos); ኼ’–”¿Š‹†‘™• 222 222
  223. 9/13/2011 CSpinButtonCtrl (4) ȂÀ†к int CControlDemoDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CWnd::OnCreate(lpCreateStruct) == -1) return -1; m_editAge.Create ( WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER, CRect( 135, 45, 190, 65 ), this, IDC_EDIT); static CSpinButtonCtrl m_spinAge; m_spinAge.Create ( WS_CHILD | WS_VISIBLE | UDS_ARROWKEYS | UDS_SETBUDDYINT | UDS_ALIGNRIGHT, CRect(0,0,1,1), this, IDC_SPIN); ̴•’‹‰‡Ǥ‡–ƒ•‡ȋͳȌǢȀȀዛዔ…–£‰‰‹኷ m_spinAge.SetRange( 15, 80 ); m_spinAge.SetBuddy ( &m_editAge ); return 0; } ኼ’–”¿Š‹†‘™• 223 223
  224. 9/13/2011 ʹǤ͹ǤͶǤ͸Ǥе’Ž‹†‡”–”ŽȋͳȌ ‡ ዔ’“—኷Žý–ŠƒŠ–”ዛዘ– ‡ ዛዓ‰–ዠ’‹ ‡ Š—ዒ…–ÀŠǣ ± Tick Marks ± ‘‹–ǣ‹዇—…‘…Š኶› ± Orientation: Horizontal & vertical ‡ Hàm Create() virtual BOOL Create( †™–›Ž‡ǡȀȀ̴ ǡ̴ǡǥ const RECT& rect, CWnd* pParentWnd, UINT nID ); ኼ’–”¿Š‹†‘™• 224 224
  225. 9/13/2011 е’Ž‹†‡”–”ŽȋʹȌȂ Hàm thành viên ‡ void SetRange(int nMin, int nMax, BOOL bRedraw = FALSE); ‡ void SetPos(int nPos); int GetPosȋȌǡǥǤ ‡ CWnd* SetBuddy (CWnd* pWndBuddy); ‡ CWnd* GetBuddy( ); ‡ ‹– ‡–‹…ȋȌǢȀȀኸ›‰‹ž–”ዋ„ዛዔ……‘…Š኶› ‡ ‹–‡–‹…ȋ‹–‹…ȌǢȀȀŠ‹ዅ–Žኼ’„ዛዔ……‘…Š኶› ‡ void SetRangeMin(int nMin, BOOL bRedraw = FALSE); GetRangeMin(); ‡ void SetTicFreq(int nFreq); ‡ Xem MSDN ኼ’–”¿Š‹†‘™• 225 225
  226. 9/13/2011 ʹǤ͹ǤͶǤ͹Ǥе’”‘‰”‡••–”ŽȋͳȌ ‡ ዔ’“—኷Žý–ŠƒŠ–‹ዅ¯ዒšዞŽý ‡ –኶‘”‘‰”‡••–”ŽȋȌ ‡ Hàm Create() virtual BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ); ± dwStyle: ‡ ̴ ǣŠƒŠ–‹ዅ–”¿Šኾ¯ዜ‰ ‡ ̴ ǣŠዊ¯ዒ–‹ዅ¯ዒ—Ž‹²–ዙ ኼ’–”¿Š‹†‘™• 226 226
  227. 9/13/2011 CProgressCtrl (2) Ȃ Hàm thành viên ‡ void SetRange ( int nLower, int nUpper); ȀȀኊ¯ዋŠ’Š኶˜‹‰‹ž–”ዋ„‹ዅ–Š‹²…ዚƒ–ŠƒŠ–‹ዅ¯ዒ ‡ void GetRange ( int &nLower, int &nUpper); ‡ int SetPosȋ‹–‘•ȌǢȀȀ0዁–‰‹ž–”ዋ˜ዋ–”ዋ–‹ዅ¯ዒ ‡ int GetPos( int &nPos); ‡ int SetStepȋ‹––‡’ȌǢȀȀŠ‹ዅ–Žኼ’„ዛዔ…–£‰ ‡ int GetStep(); ‡ int OffsetPosȋ‹–‘•ȌǢȀȀኊ¯ዋŠዜ…–£‰‰‹኷ –”²ዒ–¯ዓ˜ዋ–ŠŠ–‹ዅ¯ዒ„ኾ‰‘•ǡ¯ዏ‰–Šዕ‹ ኼ’Šኼ––ŠƒŠ–‹ዅ¯ዒ–Š‡‘‰‹ž–”ዋኸ¯ዋŠዔ‹Ǥ ‡ int StepItȋȌǢȀȀŠƒ›¯ዐ‹…Šዊዙ…–‹ዅ¯ዒዒ–„ዛዔ ኼ’–”¿Š‹†‘™• 227 227
  228. 9/13/2011 CProgressCtrl (3) ȂÀ†к static CProgressCtrl myCtrl; // Create a smooth child progress control. myCtrl.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH, CRect(10,10,200,30), this, 1012); // Set the range to be 0 to 100. myCtrl.SetRange(0, 100); // Set the position to be half, 50. myCtrl.SetPos(50); // Offset the position by one-fourth of the total range. int nLower, nUpper; myCtrl.GetRange(nLower, nUpper); myCtrl.OffsetPos((nUpper-nLower)/4); // Advance the position to the next step. myCtrl.StepIt(); ኼ’–”¿Š‹†‘™• 228 228
  229. 9/13/2011 ʹǤ͹ǤͶǤͺǤе’…”‘ŽŽƒ” ‡ ዔ’“—኷Žý¯‹዆—Š‹዇–ŠƒŠ…—ዒ ‡ Hàm Create() virtual BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ); ± †™–›Ž‡ǣ̴ ǡ̴ǡǥǤ ‡ Các hàm thành viên: ± void SetScrollRange ( int nLower, int nUpper); ± void GetScrollRange ( int &nLower, int &nUpper); ± int SetScrollPos( int nPos ); ± int GetScrollPos( int &nPos); ± ˜‘‹†Š‘™…”‘ŽŽƒ”ȋ„Š‘™αȌǢȀȀኼ–Ȁ–ኽ– –ŠƒŠ…—ዒ ኼ’–”¿Š‹†‘™• 229 229
  230. 9/13/2011 пŽý•с‹Ъ˜е‹…ž…¯‹Ч—Š‹Ш†З‰…—г ‡ ዠ‹ዉ–Šዛዕ‰†î‰ ± CWnd::OnHScroll() ዅ—¯‹዆—Š‹዇†኶‰¯ዜ‰ǡ ± CWnd::OnVScroll() ዅ—¯‹዆—Š‹዇†኶‰‰ƒ‰ ‡ ዠ‹ዉ›¯ዛዘ…‰ዞ‹¯ዅŽዔ’…ዞƒ•ዐ…Šƒ…Šዜƒ¯‹዆— Š‹዇ȋዅ–Šዝƒ–ዝŽዔ’†Ȍ ‡ ʹŠšዞŽý•ዠ‹ዉŽ…Š—‰…Š‘ ž…¯‹዆—Š‹዇…׆኶‰…—ዒ (Spin, Progress, slider, scroll), ኹšž…¯ዋŠ¯‹዆—Š‹዇‘‰Ÿ›”ƒ „ኾ‰…ž…Š‹዇–”ƒ  ኼ’–”¿Š‹†‘™• 230 230
  231. 9/13/2011 2.7.5. Bài –Н’ Ȃ Common Controls (1) ‡ Ex2-͹ƒǤዞ†ዙ‰ዒ–•ዎ¯‹዆—Š‹዇–ŠØ‰†ዙ‰ ኼ’–”¿Š‹†‘™• 231 231
  232. 9/13/2011 Š‹Ц– ŽН’ –Š—г tính cho các control ‡ Ví †ዙǣ—––‘DzŠ‘™‡••ƒ‰‡dz ± ID: IDC_BT_SHOWMSG ± Caption: Show &Message ኼ’–”¿Š‹†‘™• 232 232
  233. 9/13/2011 Š‹Ц– ŽН’ –Š—г tính cho các control ‡ Ví †ዙ: Combo box ± ID: IDC_CB_PROG ± Data: Notepad;Spider;Solitaire ኼ’–”¿Š‹†‘™• 233 233
  234. 9/13/2011 ‹–Н’‘‘‘–”‘Ž•ȋʹȌ ‡ Ex2-7b. UserProfile : ± Combo box ± Listbox ± Radio button ± Spin ± Slider ኼ’–”¿Š‹†‘™• 234 234
  235. 9/13/2011 Šмд‰ 3. г– •Я Šл ¯Ч nâng cao ‡ 3.1. DLL ‡ 3.2. Thread ‡ 3.3. Windows Socket ኼ’–”¿Š‹†‘™• 235 235
  236. 9/13/2011 3.1. Dynamic Link Libraries - DLLs ‡ 3.1.1Ǥ ‹ዔ‹–Š‹ዉ—ǫ ‡ 3.1.2ǤŠŸŽ‘኶‹• ‡ 3.1.3Ǥž…Š–Šዜ…‰ዌ‹–”‘‰ዜ‰†ዙ‰ ‡ 3.1.4Ǥ‹ዅ– ‡ 3.1.5Ǥኮ‰†ዙ‰•ዞ†ዙ‰ ‡ ‹–ኼ’ ኼ’–”¿Š‹†‘™• 236 236
  237. 9/13/2011 3.1.1Ǥ ‹е‹–Š‹Ъ— ‡ ȋ›ƒ‹…‹‹„”ƒ”›ȌǣŠዛ˜‹ዉŽ‹²ዅ– ¯ዒ‰Ǥ ‡ ዒ–ˆ‹Ž‡Šዋ’ŠŸ…Šዜƒዒ–Š‘዁…Š‹዆—Š–Šዠ… –Š‹ǤŠ‘’Š±’ዜ‰†ዙ‰Šž…•ዄ…×–Š዇‰ዌ‹¯ዅǤ ‡ ዒ–…×–Š዇¯ዛዘ…Š‹዆—ዜ‰†ዙ‰•ዞ†ዙ‰¯ዏ‰ –Šዕ‹Ǥ ‡ ž…Š–”‘‰¯ዛዘ……Š‹ƒŽʹŽ‘኶‹ǣ ± š’‘”–ȋŠš—ኸ–ǣ…ž…Š…Š‘’Š±’ዜ‰†ዙ‰„²‰‘‹ •ዞ†ዙ‰ȌǤ ± –‡”ƒŽȋŠዒ‹„ዒǣŠዟ‰Š…Šዊ•ዞ†ዙ‰–”‘‰ዒ‹ „ዒ…ዚƒȌ ኼ’–”¿Š‹†‘™• 237 237
  238. 9/13/2011 Static Library ‡ Šዛ˜‹ዉŽ‹²ዅ– –Šȋ•–ƒ–‹…ƒŽŽ› link library) ? ‡ À†ዙǣŠ…‡‹ŽȋȌ –Šዛ˜‹ዉ–‘ž Šዌ……ƒ–ŠǤŠ ኼ’–”¿Š‹†‘™• 238 238
  239. 9/13/2011 How DLL Works ? ‡ …Šዊ ¯ዛዘ…኶’ ˜‘„ዒ ŠዔŠ‹ Šዛዓ‰ –”¿Š–Šዠ… thi. ኼ’–”¿Š‹†‘™• 239 239
  240. 9/13/2011 Ў—¯‹Ш•п†к‰ ‡ À…Š–Šዛዔ…ዜ‰†ዙ‰ŠውǤ ‡ Š‹዆—ዜ‰†ዙ‰…×–Š዇†î‰…Š—‰ͳǡ–‹ዅ–‹ዉ „ዒŠዔǤȋž…ዜ‰†ዙ‰…ׄዒ†ዟŽ‹ዉ—”‹²‰ǡŠዛ‰ ×–Š዇…Š‹ƒ•ዃ ŽዉŠȌǤ ‡ Š‹ŠØ‰…֐•ዞ†ዙ‰ǡ…×–Š዇‰‹኷‹’Š×‰Šው‹ „ዒŠዔ ‡ Š‹…ኹŸ‰…ኸ’ǡ…Šዊ…ኹ–Šƒ›–Šዅˆ‹Ž‡ǡ…ž…ˆ‹Ž‡ Šዛዓ‰–”¿ŠŠž…ŠØ‰„ዋ኷ŠŠዛዖ‰Ǥ ‡ Šዛዘ…¯‹዇ǣኼ’–”¿ŠšዞŽýŠ‹዆—–Šƒ‘–ž…’Šዜ… –኶’ŠዓǤ ኼ’–”¿Š‹†‘™• 240 240
  241. 9/13/2011 ㋆—‰ ‡  ŽዉŠ…ዚƒ…ž…Šȋˆ—…–‹‘•Ȍ ‡ ž…Žዔ’ȋŽƒ••‡•Ȍ ‡ ‹ዅ–‘…ዙ…ȋ‰Ž‘„ƒŽ˜ƒ”‹ƒ„Ž‡•Ȍ ‡ Resource (bitmap, font) ‡ À†ዙǣ‘Ž‹–ƒ‹”‡‰ƒ‡•ዞ†ዙ‰ƒ”†•Ǥ†ŽŽ…Šዜƒ cards images + functions ‡ ŠЯŠф‰–ŠŠ’ŠО¯рн…‡š’‘”–…пƒ й‹…×–ŠЬ¯рн…–”—›š—Н––т„² ngoài. ኼ’–”¿Š‹†‘™• 241 241
  242. 9/13/2011 Hàm DllMain() ‡ ŠØ‰–Šዠ…–Š‹Šዛዒ–…Šዛዓ‰–”¿Š ¯ዒ…Žኼ’ǡŠዛ‰…×…ŠዜƒŠƒ‹¯዁…„‹ዉ– ‰ዌ‹ŽŽŽƒ‹ȋȌǤ ‡ 0ዛዘ…‰ዌ‹„ዖ‹‹†‘™•Š‹×¯ዛዘ…኶’˜‘ „ዒŠዔŽኹ¯ኹ—–‹²Ǥ0዇–Šዠ…Š‹ዉŠዖ‹–኶‘ –”ዛዔ…Š‹•ዞ†ዙ‰ዒ‹†—‰…ዚƒǤ ‡ ñ‰¯ዛዘ…‰ዌ‹Š‹‰‹኷‹’Š×‰Šው‹„ዒŠዔ¯዇ –Šዠ…Š‹ዉŠዟ‰‰‹኷‹’Š×‰…ኹ–Š‹ዅ–Ǥ ኼ’–”¿Š‹†‘™• 242 242
  243. 9/13/2011 3.1.2ǤŠŸŽ‘З‹• ‡ ×͵Ž‘኶‹ǣȋ–”‘‰ Ȍ ± ዖ”ዒ‰ȋ š–‡•‹‘Ȍ ± –ŠØ‰–Šዛዕ‰˜ዔ‹Ž‹²ዅ––Š  (Regular DLL with MFC statically linked). ± –ŠØ‰–Šዛዕ‰˜ዔ‹Ž‹²ዅ–¯ዒ‰  (Regular DLL with MFC dynamically linked) ኼ’–”¿Š‹†‘™• 243 243
  244. 9/13/2011 MFC Extension DLL ‡ Š‘’Š±’•ዞ†ዙ‰…ž…Žዔ’ȋ–”‘‰Ȍ†ኻš—ኸ––ዝ ž…Žዔ’ Ǥ ‡ Š‘’Š±’ዜ‰†ዙ‰†î‰…×–Š዇•ዞ†ዙ‰…ž… Žዔ’…ዚƒ×Ǥ ‡  š–‡•‹‘Ž‹²ዅ–˜ዔ‹…ž……ዚƒ  –Š‡‘†኶‰Ž‹²ዅ–¯ዒ‰Ǥ ‡ ž…•†኶‰›…Šዊ•ዞ†ዙ‰…Š‘ዜ‰†ዙ‰  ×–ÀŠ£‰Dz•‡ ‹ƒŠƒ”‡†dz ‡ ŠØ‰†î‰¯ዛዘ…„ዖ‹…ž…ዜ‰†ዙ‰•ዞ†ዙ‰Ž‹²ዅ– –Š¯ዅ–Šዛ˜‹ዉ ȋ–ƒ–‹…ƒŽŽ›Ž‹‡†–‘ Ȍ ኼ’–”¿Š‹†‘™• 244 244
  245. 9/13/2011 Regular DLL Ȃ Statically linked to MFC ‡ …ž…•ዞ†ዙ‰…ž…Žዔ’…ዚƒ †ዛዔ‹†኶‰Ž‹² ዅ––ŠǤȋ…ž…¯‘኶ –”‘‰…ዚƒ ¯ዛዘ… Š°˜‘‡‰—Žƒ”ዑ‹Š‹Š–ዛዓ‰ዜ‰¯ዛዘ… •ዞ†ዙ‰¯ዅȌǤ…Š‘‡‰—Žƒ”‹዇—›…× À…Š–Šዛዔ…ŽዔǤ ‡ ×–Š዇¯ዛዘ…•ዞ†ዙ‰„ዖ‹…ž…ዜ‰†ዙ‰ Š‘዁… ‹͵ʹ’’Ž‹…ƒ–‹‘ȋŠØ‰…ኹØ‹–”ዛዕ‰ Ȍ ‡ Šዊ…Š‘‡š’‘”–…ž…ŠǡŠØ‰…Š‘‡š’‘”–…ž…Žዔ’ có trong DLL. ኼ’–”¿Š‹†‘™• 245 245
  246. 9/13/2011 Regular DLL Ȃ Dynamically linked to MFC ‡ •ዞ†ዙ‰…ž…Žዔ’ †኶‰Ž‹²ዅ– ¯ዒ‰ȋŠØ‰–Š² ŽዉŠ…ዚƒŽዔ’ ˜‘ ›ǡ…Šዊ‰ዌ‹¯ዅŠ‹¯ዛዘ…†î‰ȌǤ ‡ À…Š–Šዛዔ…‰‹኷•‘˜ዔ‹‹዇—‡‰—Žƒ”Ž‹² ዅ––Š Ǥ ‡ ×–Š዇¯ዛዘ…•ዞ†ዙ‰„ዖ‹ዜ‰†ዙ‰ Š‘዁… „ኸ–üዜ‰†ዙ‰‹͵ʹȋŠዛ‰…ኹ…× ዚƒ –ዛዓ‰ዜ‰¯‹°Ȍ ኼ’–”¿Š‹†‘™• 246 246
  247. 9/13/2011 3.1.3Ǥž…Š‰Э‹–”‘‰н‰†к‰ ‡ ʹ‹዇—ǣ ± ዌ‹Žï…ዜ‰†ዙ‰¯ዛዘ…኶’ȋŽ‘ƒ†–‹‡†›ƒ‹… linking hay early binding). ± ዌ‹Žï…–Šዠ…–Š‹ȋ”—–‹‡†›ƒ‹…Ž‹‹‰Šƒ› late binding) ኼ’–”¿Š‹†‘™• 247 247
  248. 9/13/2011 Э‹Žï…н‰†к‰¯мй…З’ȋŽ‘ƒ†–‹‡Ȍ ‡ ¯ዛዘ…኶’˜‘„ዒŠዔŠ‹ዜ‰†ዙ‰•ዞ †ዙ‰×¯ዛዘ…኶’˜‘„ዒŠዔ¯዇–Šዠ…–Š‹Ǥ ‡ ¯ዛዘ…኶’‰ƒ›–ዝ¯ኹ—„ኸ–዇ዜ‰†ዙ‰ ›²—…ኹ—኶’×…וዞ†ዙ‰¯ዅ…ž…Š…ዚƒ ×–”‘‰“—ž–”¿Š–Šዠ…–Š‹Šƒ›ŠØ‰Ǥ ‡ ‹²ዅ–¯ዅ…ž…Š—ዎ†î‰¯ዛዘ… –Š‹ዅ–Žኼ’‰ƒ›–ዝ¯ኹ—Ǥ ‡ ×–Š዇„ኸ––‹ዉǣዎ„ዒŠዔ኶’ǡዜ‰ †ዙ‰…×–Š዇ŠØ‰Š‘኶–¯ዒ‰ዅ—›²— ኹ—኶’ŠØ‰–ዏ–኶‹Ǥ ኼ’–”¿Š‹†‘™• 248 248
  249. 9/13/2011 Э‹Žï…–Šс…–Š‹ȋ”—–‹‡Ȍ ‡ ኼ’–”¿Š˜‹²“—›ዅ–¯ዋŠŠ‹‘…ኹ኶’Ž² „ዒŠዔǤȋ0ዛዘ…኶’Š‹…׏ዒ–Š…ዚƒ¯ዛዘ… ዜ‰†ዙ‰›²—…ኹ—•ዞ†ዙ‰ȌǤ ‡ Š×Š£ǣዎ…ؐ‰•ዜ…Žኼ’–”¿Š…Š‘˜‹ዉ…኶’ǡ ‰ዌ‹ŠǤ ± ኹ†î‰Š LoadLibrary() ¯዇኶’Š‹× ¯ዛዘ…›²—…ኹ—Ǥ ± Hàm GetProcAddress() ¯዇Žኸ›¯ዋƒ…ŠዊŠȋ…‘–”ውŠȌ ዚƒŠ…ኹ†î‰Ǥ ± Hàm FreeLibrary() ¯዇†‡ƒ––ƒ…ŠŠ‹ዜ‰†ዙ‰ŠØ‰ ֐†î‰¯ዅǤȋ•ዄ¯ዛዘ…‰‹኷‹’Š×‰Š‹ŠØ‰…֐ ዜ‰†ዙ‰‘†î‰¯ዅȌ ኼ’–”¿Š‹†‘™• 249 249
  250. 9/13/2011 ‹ŠŠዌƒ‰ዌ‹Žï… –Šዠ…–Š‹ዜ‰†ዙ‰ ኼ’–”¿Š‹†‘™• 250 250
  251. 9/13/2011 3.1.4Ǥ‹Ц– ‡ ‹ŠŠዌƒšŸ›†ዠ‰ዒ–‡‰—Žƒ”ȋ†኶‰ Ž‹²ዅ–¯ዒ‰˜ዔ‹ Ȍ ‡ ›…ŠዜƒʹŠ–ÀŠ–‘žȋ…ዒ‰˜ŠŸȌ Š‘’Š±’ዜ‰†ዙ‰Šž…‰ዌ‹¯ዅǤ ‡ ኶‘”‘Œ‡…– Ȁ  ‡ Šዌ‹዇—‡‰—Žƒ”—•‹‰•Šƒ”‡†  DLL ኼ’–”¿Š‹†‘™• 251 251
  252. 9/13/2011 ‹ŠŠЭƒ˜‹Ц–‡‰—Žƒ” ኼ’–”¿Š‹†‘™• 252 252
  253. 9/13/2011 Šƒ‹„ž‘Šš—Й– ኼ’–”¿Š‹†‘™• 253 253
  254. 9/13/2011 ‡ Šƒ‹„ž‘Šš—ኸ–¯዁––”‘‰–ዉ’–‹Ǥ…’’Š‘዁… .h ‡ ዒ‹„ዒ…ዚƒŠƒ‹„ž‘˜–”‹዇Šƒ‹ ŠዛŠ…ዚƒዜ‰†ዙ‰–ŠØ‰–Šዛዕ‰ ኼ’–”¿Š‹†‘™• 254 254
  255. 9/13/2011 Šс…–Š‹Šš—Й– ‡ 0዁–ƒ…”‘ ̴ ̴¯ኹ—–‹² trong thân hàm. //TODO: If this DLL is dynamically linked against the MFC DLLs, // any functions exported from this DLL which call into // MFC must have the AFX_MANAGE_STATE macro added at the // very beginning of the function. // // For example: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // normal function body here // } ኼ’–”¿Š‹†‘™• 255 255
  256. 9/13/2011 Šс…–Š‹Šš—Й–ȋ‹ŠŠЭƒȌ ‡ Šኹ–Šዠ…–Š‹Š…ዒ‰ǣ ‡ Šኹ–Šዠ…–Š‹ŠŠŸǣ ኼ’–”¿Š‹†‘™• 256 256
  257. 9/13/2011 ‹²†Ь…Š ‡ Šዛ„‹²†ዋ…Šዜ‰†ዙ‰–ŠØ‰–Šዛዕ‰ ‡ ኼ’–‹ዅ–“—኷ǣǤ†ŽŽ˜ǤŽ‹„ ‡ ž…ዜ‰†ዙ‰•ዞ†ዙ‰†ŽŽ›…ኹ†î‰¯ዅʹ –ኼ’–‹–”²Ǥ ኼ’–”¿Š‹†‘™• 257 257
  258. 9/13/2011 3.1.5Ǥ‹ŠŠЭƒн‰†к‰•п†к‰ ‡ ዌ‹‡‰—Žƒ”¯ ˜‹ዅ–Žï…኶’ዜ‰†ዙ‰ȋŽ‘ƒ† time) ‡ ኶‘ዜ‰†ዙ‰¯ዓ‰‹኷ȋ‹዇—†‹ƒŽ‘‰„ƒ•‡Ȍ ‡ Šዜƒ–ኼ’–‹Ǥ†ŽŽ˜ǤŽ‹„…–Šዛዙ…’”‘Œ‡…– ‡ ††–ኼ’–‹ǤŽ‹„˜‘”‘Œ‡…–ȋ”‘Œ‡…–Ȁ††š‹•–‹‰ Items) ኼ’–”¿Š‹†‘™• 258 258
  259. 9/13/2011 Šƒ‹„ž‘…ž…Š‹’‘”––”‘‰н‰†к‰ ‡ —ዎ•ዞ†ዙ‰Šš—ኸ–ȋ‡š’‘”–Ȍ‘–ዝǡ ዜ‰†ዙ‰•ዄŠƒ‹„ž‘‹’‘”–…Š‘Š¯×Ǥ ‡ ኶‰Šƒ‹„ž‘–ዐ‰“—ž– ኼ’–”¿Š‹†‘™• 259 259
  260. 9/13/2011 ‹Ц– –Šс…–Š‹…Š‘…ž……Šн…£‰ ‡ ‹ዅ– šዞŽýï–…ዒ‰ǡŠŸ‰ዌ‹Š…‘‰ǡ Šƒ–ዛዓ‰ዜ‰…ዚƒ ‡ À†ዙǣ šዞŽý•ዠ‹ዉ…Ž‹……Š—ዒ–ï–…ዒ‰ ኼ’–”¿Š‹†‘™• 260 260
  261. 9/13/2011 ‹ŠŠЭƒн‰†к‰‰Э‹‹Ш—”—–‹‡ ‡ ž…Š–Šዜ…኶’Š–ዝǣ ± ኶’„ኾ‰ŠLoadLibraryǤ‹዇–”ƒš‡ …×–ዏ–኶‹ŠØ‰„ኾ‰…ž…Šš‡ዅ–“—኷–”኷ ˜዆…׍Šž…Šƒ›ŠØ‰ ± ኶’Š…ኹ–Š‹ዅ–„ኾ‰ŠGetProcAddress. ‹዇–”ƒ˜‹ዉ…‰ዌ‹Š–ŠŠ…ؐ‰Šƒ›ŠØ‰Ǥ ± ‹኷‹’Š×‰Šው‹„ዒŠዔ„ኾ‰Š FreeLibrary. ኼ’–”¿Š‹†‘™• 261 261
  262. 9/13/2011 Џ‰†к‰‰Э‹‹Ш——–‹‡ ‡ Šƒ‹„ž‘…‘–”ውŠ…Š‘Š—ዎ‹’‘”– ˜‘ዜ‰†ዙ‰ǤȋŠዙ–Š—ዒ…˜‘Š‡š’‘”– trong DLL) ‡ ዔ‹˜À†ዙ‡‰—Žƒ”ǡŠš—ኸ–…׆኶‰ǣ ‡ 0ዋŠ‰ŠÂƒ…‘–”ውŠ…Š‘ʹŠ›ǣ ኼ’–”¿Š‹†‘™• 262 262
  263. 9/13/2011 ž…Š–Šн…З’Š–о ኼ’–”¿Š‹†‘™• 263 263
  264. 9/13/2011 À†кǣ Э‹ŠŠŸ ኼ’–”¿Š‹†‘™• 264 264
  265. 9/13/2011 Šп‰Š‹Ъ…Š‘…ž…Š‰Э‹‹Ш—”—–‹‡ ‡ ŠØ‰…Š±’ˆ‹Ž‡Ǥ†ŽŽ˜‘…–Šዛዙ…ዜ‰ †ዙ‰•ዞ†ዙ‰†ŽŽǤŠ኶›ዜ‰†ዙ‰¯ዛዘ…ŠØ‰ ǫ×–Šዠ…–Š‹¯ዛዘ……ž……Šዜ…£‰ȋ…ዒ‰ǡ ŠŸȌ…ዚƒዜ‰†ዙ‰ŠØ‰ǫ ‡ ŠØ‰–ኽ–ዜ‰†ዙ‰ǡ…Š±’ˆ‹Ž‡Ǥ†ŽŽ˜‘… –Šዛዙ…ዜ‰†ዙ‰ǤŠዠ…–Š‹¯ዛዘ……ž……Šዜ… £‰…ዚƒዜ‰†ዙ‰ŠØ‰ǫ ኼ’–”¿Š‹†‘™• 265 265
  266. 9/13/2011 Ex-3.? DLL ‡ ‹ዅ–ዒ–š–‡•‹‘…Š‘ዜ‰†ዙ‰ Sketcher ኼ’–”¿Š‹†‘™• 266 266
  267. 9/13/2011 3.2ǤŠ”‡ƒ†ȋ‹Ш—–”¿ŠȀ—а‰Ȍ ‡ 3.2.1Ǥዒ–•ዎŠž‹‹ዉ ‡ 3.2.2Ǥኼ’–”¿Š¯ƒŽ—ዏ‰ ‡ 3.2.3Ǥ኶‘‘”‡”Š”‡ƒ† ‡ 3.2.4Ǥ኶‘ Š”‡ƒ† ‡ 3.2.5Ǥዅ––Šï…ዒ–Š”‡ƒ† ‡ 3.2.6Ǥ0ዏ‰„ዒ…ž…Š”‡ƒ† ‡ 3.2.7ǤÀ†ዙ‹ŠŠዌƒ ኼ’–”¿Š‹†‘™• 267 267
  268. 9/13/2011 3.2.1Ǥг–•ЯŠž‹‹ЪȋͳȌ ‡ ”²‹†‘™•ǡዑ‹ዜ‰†ዙ‰¯ƒ‰…Š኶›Žዒ––‹ዅ trình (Process) ‡ ዒ––‹ዅ–”¿Š…×–Š዇…Šዜƒዒ–Š‘዁…Š‹዆—–‹዇— –”¿ŠȀŠƒ›Ž—ዏ‰ȋthreadȌ–Šዠ…–Š‹Ǥȋ’ƒ–Š‘ˆ‡š‡…—–‹‘Ȍ ‡ Main Thread/ Primary Thread: là –Š”‡ƒ†…ŠÀŠȀ዁…¯ዋŠ ዚƒዒ–ዜ‰†ዙ‰Ǥ ‡ Secondary Thread: các –Š”‡ƒ†Šž…ȋዅ— Šዛዓ‰–”¿Š¯ዛዘ…Žኼ’ –”¿Š–኶‘–Š²–Š”‡ƒ†) ኼ’–”¿Š‹†‘™• 268 268
  269. 9/13/2011 г–•ЯŠž‹‹ЪȋʹȌ ‡ Multithreading: Š‹዆—–Š”‡ƒ†……Š኶›¯ዏ‰–Šዕ‹ ȋ0ዛዘ…‹†‘™•Žኼ’Žዋ…Š- schedule) ‡ ዉ¯‹዆—ŠŠ’ŠŸ…Š‹ƒ–Šዕ‹‰‹ƒ…Š‘…ž… thread Ȃ ዓ…Šዅ¯ƒŠ‹ዉȋmultitasking) ‡ ኶‹•ƒ‘…ኹ¯ƒŽ—ዏ‰ǫ ± ኮ‰†ዙ‰—ዎ–Šዠ…Š‹ዉŠ‹዆—Š‹ዉ˜ዙ¯ዏ‰–Šዕ‹Ǥ ± Šዛዕ‰…ž…Š‹ዉ˜ዙšዞŽý¯ዛዘ……Š኶›DzŠኼ—…኷Šdz ȋ„ƒ…‰”‘—†Ȍ–”‘‰Š‹…ž…šዞŽý–ዛዓ‰–ž…‰ዛዕ‹†î‰ Š኶›Dz–‹዆…኷Šdzȋˆ‘”‡‰”‘—†Ȍ ± À†ዙǣዜ‰†ዙ‰‘”†˜ዔ‹–Š”‡ƒ†…Š‡…•’‡ŽŽ‹‰ grammar. ኼ’–”¿Š‹†‘™• 269 269
  270. 9/13/2011 3.2.2ǤН’–”¿Š¯ƒŽ—а‰ ‡ ኼ’–”¿Š¯ƒŽ—ዏ‰ȋ—Ž–‹–Š”‡ƒ†‹‰Ȍǣ ± Multithreading in pure C ± Multithreading in Win32 API ± Multithreading in MFC ‡ ዔ’‹Š”‡ƒ†ǣŽዔ’…ዓ•ዖ¯×‰‰×‹…ž…Š‘኶–¯ዒ‰ –Šዠ…–Š‹…ዚƒ–Š”‡ƒ†Ǥ ‡  ’ŠŸ„‹ዉ–ʹŽ‘኶‹–Š”‡ƒ†ǣ ± Worker Thread ȋ‹዇—–”¿ŠšዞŽýዒ‹ȌǣŠዑ–”ዘ–Šዠ…Š‹ዉ ž…šዞŽý„²–”‘‰ǡDzŠኼ—…኷Šdzȋ„ƒ…‰”‘—†ȌǤ ± •‡” –‡”ˆƒ…‡Š”‡ƒ†ȋ‹዇—–”¿Š‰‹ƒ‘†‹ዉȌǣ–”ዠ…–‹ዅ’ –‹ዅ’Šኼ›²—…ኹ—‰ዛዕ‹†î‰ȋ…ዓ…Šዅ‹†‘™• Messages) ኼ’–”¿Š‹†‘™• 270 270
  271. 9/13/2011 е’‹Š”‡ƒ† ‡ Data members CObject CCmdTarget ± m_hThread: The current thread handle CWinThread ± m_nThreadID: The current thread ID CWinApp ± m_bAutoDelete: ȋȀ Ȍ–Š”‡ƒ†¯ዛዘ…–Š‹ዅ–Žኼ’–ዠ¯ዒ‰‰‹኷‹’Š×‰Š‘዁…ŠØ‰Ǥ ‡ Data Functions: ± CreateThread: ኽ–¯ኹ—–Šዠ…–Š‹–Š”‡ƒ† ± SuspendThread: ኶†ዝ‰Ȁ‰ዛ‰ዒ––Š”‡ƒ†ȋ–£‰„‹ዅ¯ዅ•ዎ Žኹ•—•’‡†Ȍ ± ResumeThread: ‹ዅ’–ዙ…ዒ––Š”‡ƒ†„ዋ–኶†ዝ‰ȋ‰‹኷„‹ዅ¯ዅ•ዎ Žኹ•—•’‡†ȌǤ ± SetThreadPriority: Š‹ዅ–Žኼ’ዜ…¯ዒዛ—–‹ዅ…ዚƒ–Š”‡ƒ†ȋ–ዛዓ‰¯ዎ‹ •‘˜ዔ‹ዜ…¯ዒዛ—–‹²…ዚƒ’”‘…‡••Ȍȋǡ‘” Ȍ ± GetThreadPriority: ኸ›ዜ…¯ዒዛ—–‹²…ዚƒ–Š”‡ƒ†Ǥ ኼ’–”¿Š‹†‘™• 271 271
  272. 9/13/2011 3.2.3ǤЗ‘‘”‡”Š”‡ƒ† ‡ –‘…ዙ…AfxBeginThreadǡ„ኽ–¯ኹ—ዒ––Š”‡ƒ† trong MFC ‡ ×ʹ†኶‰…Š‘ʹ‹዇—–Š”‡ƒ†Ǥ ‡ ï’Šž’†኶‰–኶‘‘”‡”Š”‡ƒ†ǣ CWinThread* AfxBeginThread( AFX_THREADPROC ThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL ); ± ʹ–Šƒ•ዎ¯ኹ—ǣ“—ƒ–”ዌ‰ ± Ͷ–Šƒ•ዎ•ƒ—ǣ–î›…Šዌȋ…×–Š዇•ዞ†ዙ‰Š‘዁…ŠØ‰Ȍ ኼ’–”¿Š‹†‘™• 272 272
  273. 9/13/2011 З‘‘”‡”Š”‡ƒ†ȋʹȌ ‡ z‰ŠÂƒ…ž…–Šƒ•ዎŠˆš‡‰‹Š”‡ƒ† Các –Šƒ•Я Mô –И controlling function, cannot be NULL. This function must be declared ThreadProc as follows: UINT MyControllingFunction( LPVOID pParam ); Parameter to be passed to the controlling function as shown in the pParam parameter to the function declaration in pfnThreadProc. nPriority Thread priority. Specifies the size in bytes of the stack for the new thread. If 0, the nStackSize stack size defaults to the same size stack as the creating thread. additional flag and can be any of these two CREATE_SUSPENDED or 0. CREATE_SUSPENDED starts the thread with a suspend count of dwCreateFlags one. The thread will not execute until ResumeThread is called. 0 Start the thread immediately after creation. Points to a SECURITY_ATTRIBUTES structure. See lpSecurityAttrs SECURITY_ATTRIBUTES for more details. ኼ’–”¿Š‹†‘™• 273 273
  274. 9/13/2011 Thread Function (1) ‡ –Šዠ…–Š‹…ዚƒዒ––Š”‡ƒ†ȋŠ”‡ƒ† Function/ThreadProc) ‡ Š…ƒŽŽ„ƒ…ȋ0ዛዘ…‰ዌ‹„ዖ‹Šዉ¯‹዆—ŠŠȌǡ…ኹ Šƒ‹„ž‘•–ƒ–‹…Š‘዁…–‘…ዙ…ȋ„²‰‘‹…ž…Žዔ’ȌǤ ‡ ׆኶‰ǣ UINT ThreadFunc (LPVOID pParam) ‡ ’ƒ”ƒŽ…‘–”ው¯ዅͳ…ኸ—–”ï…†ዟŽ‹ዉ—…ዚƒ–Š”‡ƒ† †‘‰ዛዕ‹Žኼ’–”¿Š¯ዋŠ‰ŠÂƒǤ0ዛዘ…–”—›዆…Š‘ Š–ŠØ‰“—ƒ–Šƒ•ዎ–Šዜʹ…ዚƒŠ AfxBeginThread ኼ’–”¿Š‹†‘™• 274 274
  275. 9/13/2011 Thread Function (2) ‡ À†ዙǣ CwinThread *pThread = AfxBeginThread( ThreadFunction, &data); UINT ThreadFunction(LPVOID param) { UINT nIterations = (UINT) pParam; for (int i=0; i<nIterations; i++); ȀȀ†‘•‘‡–Š‹‰ǥ return 0; } ኼ’–”¿Š‹†‘™• 275 275
  276. 9/13/2011 Thread Priorities (1) ‡ ž…–‹ዅ–”¿Šȋ’”‘…‡••Ȍ¯ዛዘ…Šዉ¯‹዆—ŠŠŽኼ’Žዋ…Š –Šዠ…–Š‹˜ዔ‹…ኸ’¯ዒዛ—–‹²Šž…Šƒ—ȋ…Š‹ዅ–Šዕ‹ ‰‹ƒŠž…Šƒ—ȌǤƒ‘‰ዏǣ REALTIME_PRIORITY_CLASS HIGH_PRIORITY_CLASS NORMAL_PRIORITY_CLASS IDLE_PRIORITY_CLASS ‡ ኸ’¯ዒዛ—–‹²…ዚƒ–‹ዅ–”¿Š¯ዛዘ…–Š‹ዅ–Žኼ’–ዛዓ‰ ¯ዎ‹˜ዔ‹…ž…¯ዒዛ—–‹²…ዚƒ’”‘…‡••×–Š—ዒ…˜዆Ǥ ‡ ×–Š዇–Š‹ዅ–Žኼ’Š‹‰ዌ‹Šˆš‡‰‹Š”‡ƒ†ȋȌǡ Š‘዁…†î‰Šǣ‹Š”‡ƒ†ǣǣ‡–Š”‡ƒ†”‹‘”‹–› ኼ’–”¿Š‹†‘™• 276 276
  277. 9/13/2011 Thread Priorities (2) Normal for HIGH, NORMAL, or THREAD_PRIORITY_TIME_CRITICAL IDLE. Double for REALTIME. THREAD_PRIORITY_HIGHEST = Process Priority + 2. THREAD_PRIORITY_ABOVE_NORMAL = Process Priority +1. THREAD_PRIORITY_NORMAL = Process Priority. THREAD_PRIORITY_BELOW_NORMAL = Process Priority -1. THREAD_PRIORITY_LOWEST = Process Priority -2. Normal for REALTIME, 1 for THREAD_PRIORITY_IDLE HIGH, NORMAL, ኼ’–”¿Š‹†‘™• 277 277
  278. 9/13/2011 Suspending and Resuming Threads ‡ ዑ‹–Š”‡ƒ†…×–Š዇…Š—›዇¯ዐ‹“—ƒ…ž…Š‹዆—–”኶‰–Šž‹Š‘኶– ¯ዒ‰Šዛ•ዓ¯ዏǤ ‡ ኶‰ዝ‰ǣ —•’‡†Š”‡ƒ† ‡ ŠØ‹’Šዙ…ǣ ‡•—‡Š”‡ƒ† ‡ ”኶‰–Šž‹Dz‰ዚdzǣ Ž‡‡’ CWinThread* pThread = AfxBeginThread (ThreadFunc, &threadInfo, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED); pThread->ResumeThread (); // Start the thread ኼ’–”¿Š‹†‘™• 278 278
  279. 9/13/2011 3.2.4ǤЗ‘•‡” –‡”ˆƒ…‡Š”‡ƒ†ȋͳȌ ‡  Š”‡ƒ†…׉‹ƒ‘†‹ዉ  ˜ዔ‹˜Ö‰Ž዁’–ŠØ‰¯‹ዉ’ (message loop). ‡ ž…„ዛዔ……ŠÀŠ–኶‘ Š”‡ƒ†ǣ ± ኶‘Žዔ’†ኻš—ኸ––ዝ‹Š”‡ƒ†˜‘˜‡””‹†‡…ž…Šǣ Function name Purpose InitInstance Perform thread instance initialization. Must be overridden. Controlling function for the thread. Contains the message pump. Run Rarely overridden. OnIdle Perform thread-specific idle-time processing. Not usually overridden. Filter messages before they are dispatched to TranslateMessage and PreTranslateMessage DispatchMessage. Not usually overridden. ProcessWndProcExcept Intercept unhandled exceptions thrown by the thread's message and ion command handlers. Not usually overridden. ExitInstance Perform cleanup when thread terminates. Usually overridden. ኼ’–”¿Š‹†‘™• 279 279
  280. 9/13/2011 З‘•‡” –‡”ˆƒ…‡Š”‡ƒ†ȋʹȌ ± ዌ‹ŠAfxBeginThread cho UI Thread CWinThread* AfxBeginThread( CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL ); ± Šƒ•ዎ¯ኹ—–‹²Ž…‘–”ውŽዔ’Š”‡ƒ††ኻš—ኸ– ¯ –኶‘Ǥ ± ž…–Šƒ•ዎ…֐Ž኶‹Ž–î›…Šዌȋ…×–Š዇†î‰Š‘዁… không) ኼ’–”¿Š‹†‘™• 280 280
  281. 9/13/2011 3.2.5ǤЦ––Šï…г–Š”‡ƒ† ‡ ዔ‹‘”‡”Š”‡ƒ†ǣʹ–”ዛዕ‰Šዘ’ዅ––Šï…ǣ ± Š‹Š–Šዠ…–Š‹…ዚƒ–Š”‡ƒ†ȋŠ”‡ƒ† —…–‹‘Ȍ ዅ––Šï…ȋ–”኷˜዆”‡–—”Ȍ ± Š‹…׊ዖ„ኸ–ü¯ኹ—‰ዌ‹Šዅ––Šï…–Š”‡ƒ† ˆš†Š”‡ƒ†ȋȌȋŠ–‘…ዙ…Ȍ CwinThread *pThread = AfxBeginThread(ThreadFunction, &data); UINT ThreadFunction(LPVOID param) { DWORD result =0 ; // do somthing ǥ AfxEndThread(exitCode); return result; } ኼ’–”¿Š‹†‘™• 281 281
  282. 9/13/2011 Ц––Šï…г–Š”‡ƒ†ȋʹȌ ‡ ዔ‹ Š”‡ƒ†ǣ ± ዅ––Šï…Š‹…×–ŠØ‰¯‹ዉ’̴ ‰ዞ‹¯ዅ Š‰¯ዘ‹–ŠØ‰¯‹ዉ’…ዚƒ×Ǥ ± ‘዁…Š‹„኷–ŠŸ–Š”‡ƒ†‰ዌ‹Šˆš†Š”‡ƒ† ‡ Š‹ዅ––Šï…–”኷˜዆ ȋ‡š‹–…‘†‡͵ʹ„‹–Ȍǡ…×–Š዇ ‹዇–”ƒ ›ǣ DWORD dwExitCode; ::GetExitCodeThread (pThread->m_hThread, &dwExitCode); if (dwExitCode == STILL_ACTIVE) { // The thread is still running. } else { // The thread has terminated. Delete the CWinThread object. delete pThread; } ኼ’–”¿Š‹†‘™• 282 282
  283. 9/13/2011 3.2.6Ǥ0а‰„г…ž…–Š”‡ƒ† ‡ Thread Synchronization ‡ ኸ¯዆ǫ ‡ ‹኷‹’Šž’ǫ ‡ ‹†‘™•…—‰…ኸ’Ͷ…ዓ…Šዅ¯ዏ‰„ዒ…ž…–Š”‡ƒ†ǣ ± Critical sections ± Mutexes ± Events ± Semaphores ‡ ‹Ž‹ዉ—ǣ ± Chapter 17, Programming Windows with MFC ± MSDN ኼ’–”¿Š‹†‘™• 283 283
  284. 9/13/2011 3.2.7ǤÀ†кŠ”‡ƒ† ‡ ž…˜À†ዙ¯ዓ‰‹኷‹ŠŠዌƒ•ዞ†ዙ‰–Š”‡ƒ††î‰ Win32 API, MFC ‡ ‹ŠŠዌƒ‘”‡”Š”‡ƒ† ± ‹዇—–”¿ŠšዞŽýዒ‹ȋ‘”‡”Š”‡ƒ†Ȍ…‹¯዁–ዒ–Š…× Š‹ዉ˜ዙ–Šዠ…Š‹ዉ…Š኶›…Šዟ–”‘‰†Ö‰–ŠØ‰„ž‘…Š‘ ¯ዅŠ‹…×–ÀŠ‹ዉ—‰ዝ‰Ǥ ± ‹዇—–”¿Š…ŠÀŠ…ዚƒዜ‰†ዙ‰–Šዠ…Š‹ዉ‰‹ƒ‘–‹ዅ’‰ዛዕ‹ dùng. Nút Start/Stop, Close ኼ’–”¿Š‹†‘™• 284 284
  285. 9/13/2011 Ex3-?. Prime Sieve ‡ Ÿ›†ዠ‰ዜ‰†ዙ‰•‰•ዎ‰—›²–ዎȋ”‹‡ ‹‡˜‡Ȍ˜ዔ‹–‹዇—–”¿ŠšዞŽýዒ‹ȋ‘”‡” Š”‡ƒ†Ȍ…Š‘˜‹ዉ…šዞŽý‰‹኷‹–Š—ኼ–•‰•ዎ ‰—›²–ዎǤ ኼ’–”¿Š‹†‘™• 285 285
  286. 9/13/2011 3.3 Н’–”¿Š‹†‘™•‘…‡– ‡ 3.3.1Ǥዒ–•ዎŠž‹‹ዉ ‡ 3.3.2Ǥኼ’–”¿Š‘…‡–˜ዔ‹  ‡ 3.3.3Ǥኼ’–”¿Š‘…‡–˜ዔ‹  ‡ 3.3.4ǤÀ†ዙ‹ŠŠዌƒ ኼ’–”¿Š‹†‘™• 286 286
  287. 9/13/2011 3.3.1Ǥг–•ЯŠž‹‹ЪȋͳȌ ‡ Winsock (Windows Socket): ± —ኸ–šዜ–ዝȋ‡”‡Ž‡›‘ˆ–™ƒ”‡‹•–”‹„—–‹‘Ȃ UNIX) ± ‹ƒ‘†‹ዉ…ž…†ዋ…Š˜ዙšŸ›†ዠ‰–”²዆‰‹ƒ‘ –Šዜ…–”—›዆–ŠØ‰ȋȌȀ  ‡ Port: ± ዎŠ‹ዉ—…ዐ‰†ዋ…Š˜ዙǡ’ŠŸ„‹ዉ–‰‹ዟƒ…ž…–‹ዅ –”¿Š–”²…ዒ–Š‘•–¯ዏ‰–Šዕ‹•ዞ†ዙ‰‰‹ƒ‘ –Šዜ…Ȁ Ǥ ኼ’–”¿Š‹†‘™• 287 287
  288. 9/13/2011 г–•ЯŠž‹‹ЪȋʹȌ ‡ Socket ? (= IP + Port) ± 0‹዇–”—›዆–ŠØ‰¯ኹ—…—ዎ‹…Š‘’Š±’ዜ‰†ዙ‰ ‰ዞ‹˜Šኼ…ž…‰×‹–‹“—ƒ¯ዛዕ‰–”—›዆኶‰Ǥ ± —‰…ኸ’‰‹ƒ‘†‹ዉ¯዇ዒ–…Šዛዓ‰–”¿Šዅ–ዎ‹ǡ –”ƒ‘¯ዐ‹†ዟŽ‹ዉ—˜ዔ‹ዒ–…Šዛዓ‰–”¿Š–”²ዒ– ž›–ÀŠŠž…–”‘‰኶‰ ኼ’–”¿Š‹†‘™• 288 288
  289. 9/13/2011 г–•ЯŠž‹‹Ъȋ͵Ȍ ‡ ž…Ž‘኶‹‘…‡–ǣ ± Datagram socket Ȃ •ዞ†ዙ‰ UDP ‡ Ít –‹…ኼ› ‡ ŠØ‰‰ዞ‹Ž኶‹ ‡ ŠØ‰ዅ–ዎ‹ȋ‘‡…–‹‘Ž‡••Ȍ ± Stream socket Ȃ using TCP ‡ ‹…ኼ› ‡ ׉ዞ‹Ž኶‹ ‡ ዛዔ‰ዅ–ዎ‹ȋ‘‡…–‹‘- oriented) ± Raw and others ኼ’–”¿Š‹†‘™• 289 289
  290. 9/13/2011 Н’–”¿Š‘…‡––”²‹†‘™• ‡ ኮ‰†ዙ‰‹͵ʹǣዞ†ዙ‰–Šዛ˜‹ዉ‹•‘… ‡ ኮ‰†ዙ‰ ǣ•ዞ†ዙ‰Žዔ’•›…‘…‡– ȋ¯×‰‰×‹…ž…Š‹•‘… Ȍ ኼ’–”¿Š‹†‘™• 290 290
  291. 9/13/2011 3.3.2ǤН’–”¿Š‘…‡–˜е‹‹•‘… ‡ Šƒ‹„ž‘–Šዛ˜‹ዉ™‹•‘…ʹǤŠ˜ˆ‹Ž‡ǤŽ‹„ȋ™•ʹ̴ʹǤŽ‹„Ȍ #include #pragma comment (lib,"ws2_32.lib") ‡ Šዖ‹–኶‘‹•‘… int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData); ኼ’–”¿Š‹†‘™• 291 291
  292. 9/13/2011 À†кŠз‹–З‘‹•‘… #include "stdafx.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { WSADATA SData; int iResult = WSAStartup(0x0202,&SData); if (iResult!=0){ cout << "KHONG THE KHOI DONG WINSOCK"; return 1; } cout << "KHOI TAO SOCKET THANH CONG: \n"; cout << "Phien ban: "<< SData.wVersion << "\n"; cout << "Phien ban co the ho tro: "<< SData.wHighVersion << "\n"; cout << "Ghi chu: " << SData.szDescription << "\n"; cout << "Thong tin cau hinh: " << SData.szSystemStatus << "\n"; WSACleanup(); return 0; } ኼ’–”¿Š‹†‘™• 292 292
  293. 9/13/2011 Giao –‹Ц’ Client Ȃ Server ˜е‹ TCP 293 293
  294. 9/13/2011 294
  295. 9/13/2011 ‹ƒ‘–‹Ц’Ž‹‡–Ȃ ‡”˜‡”˜е‹ UDP Server socket() bind() UDP Client recvfrom() socket() blocks until datagram received from a client sendto() data request data reply sendto() recvfrom() close() 295
  296. 9/13/2011 296
  297. 9/13/2011 Các hàm Winsock (1) ‡ SOCKET create a new socket ‡ BIND attach a local address to a socket ‡ LISTEN announce a willingness to accept connections ‡ ACCEPT block the caller process until a connection attempt arrives ‡ CONNECT actively attempt to establish a connection ‡ SEND send some data over the connection ‡ RECEIVE receive some data from the connection ‡ CLOSE release the connection (the port) ኼ’–”¿Š‹†‘™• 297 297
  298. 9/13/2011 Hàm socket( ) ‡ ኶‘‘…‡–„ኾ‰Šǣ SOCKET socket ( int af, //giao WKӫFNӁWQӓL ,)B1(7FKR7&3,3 int W\SHJLDRWKӫF YүQFKX\ӅQ 62&.B',$*5$0 SOCK_STREAM, SOCK_RAW) int SURWRFROFKӋ ÿӍQKU}OңLJLDRWKӫF ); ‡ À†ዙǣ SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_IP); ኼ’–”¿Š‹†‘™• 298 298
  299. 9/13/2011 Hàm bind( ) int bind( SOCKET s, const struct sockaddr FAR* name, int namelen ); Tác †ዙ‰ †ዙ‰ ዚƒ BIND là •ዄ giúp cho SOCKET ዚƒ SERVER „‹ዅ– ”ኾ‰ nó •ዄ Šዕ ¯ዘ‹ ዅ– ዎ‹ và Šኼ †ዟ Ž‹ዉ— trên IP nào và PORT bao nhiêu? Hàm Bind ‰ዏ có 3 thông •ዎ: - SOCKET s: Socket ¯ዛዘ –Š‹ዅ– Žኼ’ - sockaddr name: ኸ— trúc ADDR bao ‰ዏ ¯ዋƒ Šዊ IP và PORT - int namelen: Kích –Šዛዔ ዚƒ ኸ— trúc sockaddr ኼ’–”¿Š‹†‘™• 299 299
  300. 9/13/2011 Hàm listen( ) ‡ int listen( SOCKET s, int backlog ); ‡ ዇ –ዝ khi ‰ዌ‹ hàm này thì SERVER •ዄ „ኽ– ¯ኹ— Žኽ‰ nghe ዅ– ዎ‹ ዚƒ mình. Hàm LISTEN ‰ዏ có 2 thông •ዎ: - SOCKET s: Socket ¯ ¯ዛዘ –Š‹ዅ– Žኼ’ IP và PORT. - int backlog: ዎ ዅ– ዎ‹ cho phép Šዕ trong hàng ¯ዘ‹ khi Server Šዛƒ Šኸ’ Šኼ ዅ– ዎ‹. (vì ¯Ø‹ lúc có –Š዇ có –ዔ‹ 2 hay 3 client ዅ– ዎ‹ –ዔ‹ cùng 1 lúc). Giá –”ዋ –ዎ– Šኸ– là Š‘኷‰ –ዝ 5 Ȃ 10. ኼ’–”¿Š‹†‘™• 300 300