Bài giảng Lập trình Windows - Phạm Ngọc Hưng
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:
- bai_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
- 9/13/2011 ክኧ 0ኈኞኤ ኛW ኛW ኘW ϯ@WINDOWS ዌ ዛ ዒ môn ዣ ኼ Máy tính hungpn@soict.hut.edu.vn 1
- 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
- 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
- 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
- 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
- 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
- 9/13/2011 Các thành К л г п б ኼ¿ 7 7
- 9/13/2011 д ЦН¿-driven ኼ¿ 8 8
- 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
- 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
- 9/13/2011 Windows Message (cont.) ኼ¿ 11 11
- 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
- 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
- 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
- 9/13/2011 Microsoft style (1) ኸ ዟዉዖዒ ዚǡ² ǡ² ǡ²ኾዎ¯ዋዀ¯¯ዛዘ ዅ Àዙǣ T²ዟዉǣǡǡǡǡǡǥǤ T² ኸï ǣ ǡǡǡǤǤǤǤ T²ኾዎ¯ዋዀǣǡ̴ǡ ̴ǡǥǤǤ ኸ ȋ ǡ ዛዓዜ Ȍ¯¯ዛዘ ዅ hoa (pascal notation). Àዙǣ ȋǥȌ ȋǥȌ ȋǥȌ ኼ¿ 15 15
- 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
- 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
- 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
- 9/13/2011 1.2. Й trúc л мд trình Windows ኼ¿ 19 19
- 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
- 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
- 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
- 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
- 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
- 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
- 9/13/2011 0£ ký е п б Sau khi ¯ዋ  ዔ ዞ ዐ ¯£ ký ኾ hàm RegisterClass: ATOM RegisterClass(CONST WNDCLASS * lpWndClass); ኼ¿ 26 26
- 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
- 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
- 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
- 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
- 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
- 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
- 9/13/2011 Multitasking ኼ¿ 33 33
- 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
- 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
- 9/13/2011 ×О ኼ¿ 36 36
- 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
- 9/13/2011 Win32 Application in Visual C++ Developer Studio ኼ¿ 38 38
- 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
- 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
- 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
- 9/13/2011 HelloWorld (run) ኼ¿ 42 42
- 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
- 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
- 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
- 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
- 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
- 9/13/2011 ኼ¿ 48 48
- 9/13/2011 Йï нк ኮ ዙ MFC cung ኸ: ± ዒ ዔ ወ ዜ ዙȋApplication Class) ± ዒዔ ወ ዞ ዐ chính ዚ ዜ ዙ (Main Window) ± ȋȌȋ¯ዛዘ ኺ¯Ȍ (WinMain() Function) ኼ¿ 49 49
- 9/13/2011 1.4. Н trình Windows е VC++ 2008 ͵ ዜዙዔΪΪǣ ± ኮዙዞዙዠ ዅ (Win32 Project) ± ኮዙዞዙ (MFC Application) ± ኮዙ (CLR/Windows Form Application) ኼ¿ 50 50
- 9/13/2011 Môi мж phát Ш À йΪΪȋʹͲͲͺȌ IDE: The Solution (Workspace) Output Pane Editor Area Menu Bar Rearranging on the IDE ኼ¿ 51 51
- 9/13/2011 VC++ 2008 Developer Studio Environment -Solution ToolBox Explorer -Class View -Resource View Editor Area Properties Output ኼ¿ 52 52
- 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
- 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
- 9/13/2011 Menu Bar + Rearranging IDE Menu bar + Tool bars Standard toolbar Build toolbar Text Editor ǥǤ Add/remove Toolbars ±ǡዎÀ ዞዐዉ ǡ ǡǡǥǤ ኼ¿ 55 55
- 9/13/2011 КнкШеΪΪ 56 56
- 9/13/2011 Character Sets, và _T macro (1) ǣኼýዠͺ ǣኼýዠ ͳȋ trùm ANSI). ²ዋ ዜዙΪΪ² × ዌዞዙ ØǤ ዅዒ ዑDzdz¿¿²ዋ ዄ ዉȋͺȀͳýዠȌǤ ዅ ዑdzdz¿¿²ዋ ዄ ዉ ዑ Ǥ ኼ¿ 57 57
- 9/13/2011 Character Sets, và _T macro (2) î ̴ȋ ዚ Ȍ ዑ ̴ȋDzdzȌ¿ዅዄ¯ዛዘ ዉ ዅýዉዞý̴ ¯ዛዘ ¯ዋÂȋ ዌ ዑýዠ ዜዙȌǡዅØ ¯ዋ ýዠǤ 58 58
- 9/13/2011 Character Sets và _T macro (3) TCHAR ȋî Ȍǣ ± ̴ȋ ͳȌዅ̴¯ዛዘ ¯ዋ  ± ȋͺȌዅ̴دዛዘ ¯ዋ  ዛዓዠǡ²îTCHAR* LPTSTR (con ው ዑȌ ̴ȗǡ ȗǤ LPCTSTR: ውዔ ዑኾ ዞዙȋȌ¯ ¯ዋ¯ዒዒýዠ (8 hay 16 bit) ዞዙ ዛዉ Ǥ ዞýýዠȀ ዑ ዚዛዉ-runtime. Àዙǣ̴ ȋȌ 59 59
- 9/13/2011 Exercises Chapter 1. Ex1-1. HelloWorld ± ͵ʹ ዛÀዙýዅ ± ǡ ǡ ዓ ዅØ ¯ዉ Ex1-2. HelloMFC ± ¿ዞዙ ± ǡዞዙ ± Code view, design view ± Compile, debug, run ኼ¿ 60 60
- 9/13/2011 Create a project Tên ዜ ዙ ኼ¿ 61 61
- 9/13/2011 пк &KӑQNLӇXӭQJ GөQJ 6ӱGөQJWKѭYLӋQ Unicode ኼ¿ 62 62
- 9/13/2011 ЦЦЪнкȋȌ ኼ¿ 63 63
- 9/13/2011 Add code to greeting (1) DzdzǣȀ ዌ ዠ ዉ, tên hàm ዞ lý ኼ¿ 64 64
- 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
- 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
- 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
- 9/13/2011 2.1. Message Map trong MFC (1) ኼ¿ ±ዅ ¯ዞý دዉȋȌዎǤȋ ዞ ý ¯ዋ¯ዛዘ ኸȌǤ ዌ message handlers ȋ handlers). ȋ ñ² ዚዔ¯ዛዘ ዉ ኾዎ̴ȌǤ ዓ ዅ²ዅዟدዉዞý¯ዛዘ ¯ዋዖMessage Map ȋدዉȌ ኼ¿ 68 68
- 9/13/2011 2.1. Message Map trong MFC (2) ዑዔ ×ዞýدዉዄዒ Ǥዞዙ ǣ ± DECLARE_MESSAGE_MAP() trong file .h ± ̴ ̴ȋȌ̴ ̴ȋȌ trong file .cpp ȋዒ ዞýዛዓዜዔ دዉȌ ኼ¿ 69 69
- 9/13/2011 2.1. Message Map trong MFC (3) ±ዒÀዙዜዙ ¯ዓǣ ኼ¿ 70 70
- 9/13/2011 Message Map trong MFC (2) ኼ¿ 71 71
- 9/13/2011 ¿пýØ¯Ъ ኼ¿ 72 72
- 9/13/2011 ЗØ¯Ъ Message Description Category Windows Thông ¯ዉ ኺ ዚǡኽ¯ኹዔ̴ Messages ȋዝ دዉ̴Ȍ Àዙǣ̴ǡ̴ǡǥ Control Các دዉ̴¯ዛዘ ዞዝ ¯ Notification ȋȌዛǡ ǡǥ¯ዅ ዞዐ Messages ዜ ïǡ ዝ ዞዐ ¯ዅ ዞዐ Ǥ ዎ¯°ȋȌ ዅዞዝ Command ñ دዉ̴ዛ¯ዛዘ Messages ዝ ኹዉዛዕîዛ ǡǤȋ0ዛዘ ¯ዋ ኺǡዛዕî ײȌ ኼ¿ 73 73
- 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
- 9/13/2011 2.2. Document/View trong MFC ǣኼዘዟዉ ዚዜዙ ȋኻኸዝዔ Ȍ ǣዓ ዅ ዋዟዉ trong Document ȋኻኸዝዔ Cview) ኼ¿ 75 75
- 9/13/2011 Document/View trong MFC ×ዠ ዌ ǣ ± ȋ Ȍǣዋዒ ዉȋ Ȍዑኹ²ዒ ዞዐȋȌ ± MDI (Multiple Document Interface): Cho phép ዖዉዒï 0¯ዛዘ ዘዖ ኹ CDocument và Cview ×ዜዙ ዅï SDI/MDI ኼ¿ 76 76
- 9/13/2011 нкЗ ዊዖዒዉ ኼ¿ 77 77
- 9/13/2011 нкЗ ±ዖዉ ኼ¿ 78 78
- 9/13/2011 Џк Ȁ ¿ዜዙ ǡ ¯ዛዘ ኹዅ¯ዛዘ ዅ ዝȋኻኸȌዝ ዛǣ ± ዔ: CWinApp, CFrameWnd, CDocument, CView ± ዔ: CWinApp, CMDIFrameWnd, CMDIChildWnd, CDocument, CView ኼ¿ 79 79
- 9/13/2011 Џк Ȁ 80 80
- 9/13/2011 Mô И vai trò các class ኼ¿ 81 81
- 9/13/2011 Й trúc Document/View З SDI ኼ¿ 82 82
- 9/13/2011 Й trúc Document/View З MDI ኼ¿ 83 83
- 9/13/2011 с мд tác р Document và View ዔ ʹǡዠ²ዅዉØ ͳዅ¯ዎዛዘዔCDocument ȋዅዛዕ ײ ȌǤØዅ ǡ ׯ Øȋዝ ዔ ȌኸØ ȋዝዔ ዔȌǤ Øዛዕǡዔ ዜዙ ¿ዔØ ×GetDocument ኾዑዘዉ ኸዅ ውǤ ኼ¿ 84 84
- 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
- 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
- 9/13/2011 е ዟዒዉǤ0ዋǡዔ ው¯ዅዔ ± CDocument* GetDocument() const; OnDrawǣዄ ዞዐ OnInitialUpdateǣ0ዛዘ ዌዛዔ ዒዖ ዋǤ OnUpdateǣ0ዛዘ ዌዟዉ¯ ¯ዐ¯ ኼ ኼ Xem MSDN ኼ¿ 87 87
- 9/13/2011 е ЙС ዠዀ ዔዑዘ ዉ ዋዟዉȋ ዌኻኸዝዔዛዓዜȌ ኼ¿ 88 88
- 9/13/2011 Ex2-ͳǤЏк ዠዜዙ ዅï × À£¯ዓȋዛዓዠ Notepad) ኼ¿ 89 89
- 9/13/2011 App Wizard ዠዜዙዅï ኼ¿ 90 90
- 9/13/2011 App Wizard ዌዔዄዅ ዝȋ Ȍ ኼ¿ 91 91
- 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
- 9/13/2011 App Wizard ኼ¿ 93 93
- 9/13/2011 2.3. Menu và Toolbar 2.3.1. Menu ± ዒዉ ± ኼ ± ዞý ± ኼ ኼ¿ 94 94
- 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
- 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
- 9/13/2011 пк ኼ¿ 97 97
- 9/13/2011 Add Handler for menu messages ኼ¿ 98 98
- 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
- 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
- 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
- 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
- 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
- 9/13/2011 З Н menu cho dialog ዒ tài nguyên menu cho ዜ ዙ ዌ menu ኹ ኽ vào dialog trong ዒ Dialog Properties. ዞዙ CMenu::LoadMenu() ኼ¿ 104 104
- 9/13/2011 Ex2-3a. Menu ኼ ዜዙ Ex2-ʹ × ኹǣ ± ዜ ǣǡ ǡ Circle, Curve ± ዜ ǣ ǡǡ ǡ Blue ȋዙ ዙዉ ዄ ¿ዔኽ nhau) ኼ¿ 105 105
- 9/13/2011 2.3. Menu và Toolbar (cont.) 2.3.2. Toolbar ± ዒዎ ± ዅዅ ± ²ዜዙ ± ዞýدዉ ïዉ² ± ዞýዒዎ¯ ዉ ኼ¿ 106 106
- 9/13/2011 гЯЗ ኼ¿ 107 107
- 9/13/2011 ЦЦ ዔዜዙዅï Ȁǣዀ ¯ዋǤ ×ዞዙȋዖ ȀȌ¯ዞ¯ዐ¯ × ዔǤ ×ዞዙȋ² Ȍ¯ዅዅǤȋዞዙ ȋȌ ዚዔ¯Ȍ ኼ¿ 108 108
- 9/13/2011 п к toolbar editor Resource/Toolbar ኼ¿ 109 109
- 9/13/2011 Thêm Toolbar vào н к ዞዙዔǣ ȋ Ȍ ± ¯ዎዛዘዔ ± ዌኼ ዞዐ ± ¯ ዅዅ ± ዅኼዒ À ± ኽ ዞዐ À ኼ¿ 110 110
- 9/13/2011 Khai báo ¯ЯмйеCToolBar ኼ¿ 111 111
- 9/13/2011 пýtrong hàm OnCreate Ȃ Эͳ ኼ¿ 112 112
- 9/13/2011 пýȂ Эʹ ኼ¿ 113 113
- 9/13/2011 Toolbar Style ኼ¿ 114 114
- 9/13/2011 пý Ȃ ЭʹȋȌ ኼ¿ 115 115
- 9/13/2011 Toolbar docking style ኼ¿ 116 116
- 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
- 9/13/2011 пýгЯЗ¯Т Ъ ×× ×× Có Combo box trên Toolbar ኼ¿ 118 118
- 9/13/2011 Nhóm button З checkbox và radio ǣ ዌͳ× ǣዊ ዌͳͳ× ǣ0 ዅ ዕǡዎዘዞýዠዉ ̴̴ ዚ ዛዓዠዛዔ items ኼ¿ 119 119
- 9/13/2011 Àкпlý Ш ȋͳȌ ኼ¿ 120 120
- 9/13/2011 Àкпlý Ш ȋʹȌ ኼ¿ 121 121
- 9/13/2011 Àкпlý ШȋͳȌ ኼ¿ 122 122
- 9/13/2011 Àкпlý ШȋʹȌ ኼ¿ 123 123
- 9/13/2011 Enable/Disable button ኼ¿ 124 124
- 9/13/2011 Combo box trên toolbar ²ý ዞዐ ȋȌኽ ዞዐ ȋȌ ኼ¿ 125 125
- 9/13/2011 Khai báo ¯Ямй ኼ¿ 126 126
- 9/13/2011 О combo box vào toolbar ኼ¿ 127 127
- 9/13/2011 Ex2-3b. Toolbar ዠ ዜዙ Ǥ ዉ ዚ ዌ ዛዓዜǤ ዝ ×ዀ ዚዜዙ ኼ¿ 128 128
- 9/13/2011 2.4. п lý Х (Drawing) ʹǤͶǤͳǤዓዄ² ʹǤͶǤʹǤኸዟ ዅዋ ʹǤͶǤ͵Ǥዒዎ ዄ ዓ ʹǤͶǤͶǤዞዙ ¯ዎዛዘዄ ኼ¿ 129 129
- 9/13/2011 ʹǤͶǤͳǤдИЧХ² ዉዌ¯ዒ ኸ ዞýዄ² ዞዐǫ îዜዙ ዚ ዞዐȋ Ȍǫ ኼ¿ 130 130
- 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
- 9/13/2011 д ЦХ ኮዙ ȋዅï ȀȌ ዜዔኻ ኸዝዔȋýዉ ዋዟዉ²î ዉ ȋ Ȍ ዚ ዞዐዜዙȌǤ ዞýዄ²îዋ¯ዛዘ ዠ ዉ hàm OnDraw( ) ȋ¯ዜدዉ ̴¯ዛዘ ዌኸ ዜî ዞዐ ኹ¯ዛዘ ዄ ȌǤዌǣ 132 132
- 9/13/2011 ʹǤͶǤʹǤЙр И Ц Ь (1) ዔǣ ± ዜͳዟ ዅዋȋ Ȃ dc) và ዛዓዜ ኹዅ¯ዞýዄዜ ዙǤȋ̱ͳͲͲȌǤ ± Àዙǣዄ¯ዛዕȋȌǡዄ¿ ዟኼ ȋ Ȍǡዄ¿ǡǥ ± ዔ ዓዖ ኼ¿ 133 133
- 9/13/2011 Йр И Ц ЬȋʹȌ ኸዒዎዔዟ ዅዋ ²ዉȋዅዝዝዔȌǣ ± CPaintDCǣዞዙ ዉ ዄîዜዙ ዚ ዞዐȋ ዊ ዔዠዉȌ ± CClientDCǣዞዙ ዉ ዄîዜዙ ዞ ዐȋዄኸüዓዛ ዊ ዔዠዉ OnPaint) ± CWindowDC: ȋዞዙ ዉ ዄ ዞዐǡ ዏ îØîዜዙ ዞዐȌ ± CMetaFileDCǣȋዞዙ ዉ ዄዒ Ȍ ኼ¿ 134 134
- 9/13/2011 Truy Й р И Ц Ьȋ͵Ȍ 0ኸዟ ዅዋǡዅ¯ዎዛዘ ዔǡîǣ CDC dc(this); ዅ ውȗα ȋȌǢ 0×ዟ ዅዋǡîǣ ReleaseDC(pDC); delete pDC; ኼ¿ 135 135
- 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
- 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
- 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
- 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
- 9/13/2011 ʹǤͶǤͶǤпк ¯ЯмйХȋͳȌ ዅ¯ǣ ± ዄ ¯ዛዕዄ ×ኽ ǡ ȋȌ khác nhau ± Ø ¯ዎዛዘዄዔኽ ǡØ khác nhau ± ዄ ዟዔ ኽ ǡ ኹዞዙ ¯ዎዛዘዄȋïዄǡ ዐØ ǡ ዟȌ ኼ¿ 140 140
- 9/13/2011 пк ¯ЯмйХȋʹȌ ïዄȋȌǣî¯ዄ¯ዛዕ ዐዄȀ ዌዄȋȌǣîØ Font ¯ዎዛዘ¯¯ዛዘ ýዖ ዔ ዛዓዜǤ ኼ¿ 141 141
- 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
- 9/13/2011 пкïХȋʹȌ VÀዙዛïዄዔ ዌïዄ ዉዕǡîǣ ኼ¿ 143 143
- 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
- 9/13/2011 пк ЭХȋͳȌ ዔ × ዌዄኾ îǡ î hàm CreateBrush CBrush( ); CBrush( COLORREF crColor ); CBrush( int nIndex, COLORREF crColor ); explicit CBrush( CBitmap* pBitmap ); ኼ¿ 145 145
- 9/13/2011 пк ЭХȋʹȌ Àዙ ዌዄዔ ዌ ዌዄዉዕ: ኼ¿ 146 146
- 9/13/2011 GDI Fonts và е CFont ዉ ዞዙ ዟ ¯ዋǡዛዕî Ö × ዟ ዅ¯ዒ¯ዏዌ tùy ዌǤ ኼ¿ 147 147
- 9/13/2011 Ex2-ͶǤХнк ዠ ዉ các thao tác ዄ ዓ²ዜ ዙ ǣ ± ዄ¯ዛዕǡ¿Öǡ¿ ዟኼ ± ȋ ¯ዎዛዘዄዋÀዉዕȌ Thay ¯ዐ bút ዄ (CPen), ዐ ዄ (CBrush) khác nhau ኼ¿ 148 148
- 9/13/2011 2.5. Giao Ц е гǡÀ ʹǤͷǤͳǤ ዅዔ ዒ ʹǤͷǤʹǤ ዅዔÀ ኼ¿ 149 149
- 9/13/2011 ʹǤͷǤͳǤ Це г ኹዞýዠዉ ዒ¿ǫ ± À ዒȀ¯ዛዘ ኸȀȋȀȌ ± ዟÀ ዒȋȌ ± À ዒዟǡÀ ዒዖዒȋዅ ×Ȍ ± ኊÀ ዒ°À ዜ £ȋǡȌ ዞý¿ǫȋዙዒ ዜዙȌ ዓȋዔȌኼዞýዠዉ ዒǫ Àዙǣዔዜዙ ǣ ± ዎዄ¯ዎዛዘኽ¯ኹኸ ዒȋȌǡዟ Ϊ ዒȋȌǡ ዒȋȌǤ ± ¯ዎዛዘ¯ዛዘ ዄ²ዔ ǡ ኹኽ ዠዉ ዒ²ዔǤ ኼ¿ 150 150
- 9/13/2011 دЪосЪ г ዌዔ ኹዞýØ ¯ዉዝ ዒ ዌ ዎዞýȋ Properties) ዌዞýȋ Ȍ ዒ² ዛዓዜ ዎዞý ኼ¿ 151 151
- 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
- 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
- 9/13/2011 Mouse Message Handlers (3) ʹዎǣ ± ǣዌ¯ዒ ዒï ዠዉ ± ǣዕȋ͵ʹȌ ዅï ዒ¯ዛዘ ኸኼዅ ÀȀዅ ¯ዛዘ ኸ°Ǥ ± × ዋኾȋȌ¯ዛዘ ¯ዋ Âዙ ዙ ¯ዋ Ǥ ኼ¿ 154 154
- 9/13/2011 nFlags 0ኼዅዋ ዕǡî±ƬȋȌ ዔǤÀዙኼዅÀ ׯዛዘ ኸ î ዒǣ ȋȋ Ƭ̴Ȍαα̴ȌȓǥȔ ኼ¿ 155 155
- 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
- 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
- 9/13/2011 ʹǤͷǤʹǤ ЦеÀ ዠዉÀǣ ± ÀØዛዕ¯ዛዘ ኸዎȀ² (down/up) ± À ዜ £¯ዛዘ ኸዎȀ² ± À¯ዛዘ ኸዟȋȌǡ¯ዌ ¯ዛዘ ኹǤ ± ዐዘÀ¯ዛዘ ኸȋኸ° À ዜ £ǣǡǡȌ ዙ ²ǣኽ¯ዛዘ ዠዉÀǡ¯ዌ À¯ ×ዞýÀ ዘ ኼ¿ 158 158
- 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
- 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
- 9/13/2011 п lý دЪоÀȋ͵Ȍ ×îǣ afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) ዎǣ ± ǣ ¯ዋ À¯ዛዘ ኸȋ ȌǤ ȋ0ዛዘ ¯ዋÂǤȌ ± ǣዎኹȋዟÀȌ ± ǣ ¯ዋ ǡÀዛዔ ǡ ÀኸȀǡǥ ± Àዙǣ ͳ͵ȋȌαͳዅÀ¯ዛዘ ኸ° Bit 15 (Key-ȌαͲዅኸȋȌǡαͳዅ (released) ኼ¿ 161 161
- 9/13/2011 nChar (virtual key code) ȂÀк ኼ¿ 162 162
- 9/13/2011 Ví кЦÀ ዠ ዉ thay ¯ዐ con ው ዛዓ ዜ ዔ các phím ዌ ዞ lý ዠ ዉ: OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) ኼ¿ 163 163
- 9/13/2011 2.6. г ЗȋȌ ʹǣ ± ȋዒØዛዕȀîዅȌ ± ȋ ዒ ²ዙȌ ኼ¿ 164 164
- 9/13/2011 ʹǤǤͳǤгЗȋȌ Dialog box: ± ዒ ዞዐȋȌ ± ኮዙዛዕî ዜ ¯ ȋ Ȍȋ ኸ ñ ዞዐȌ ʹዅዎ ዚͳዜዙ ǣ ± ዉኼý ዚȋ¯ዛዘ ¯ዋ resource) ± 0ዎዛዘዔȋî¯ý ¯ዒ ዚ ¯¯²×Ȍ ኸዔ ዓዖǡ ×ዞዙዔ ኻኸዝ× ¯ ȋዉዅዅȌǤ ኼ¿ 165 165
- 9/13/2011 ЦЦȋ Ȍ ዖ ǡ ±ǣ ± ዅዅͳ²îዉ ± ኸ ¯ȋ Ȍዝ ± ዔኾ 0ዋǣ̴ 166 166
- 9/13/2011 Н¿ ȋͳȌ ዏʹÀ ኹዅǣ ± ዋȋዌȌ¯×² ± ኼ¿ዞýዠዉ ¯¯ ²¯× ኼ¿ 167 167
- 9/13/2011 Н¿ ȋʹȌ ኽዔý cho dialog sau ¯ ዅዅ ኾ ዌ ȋ ዌ ዒ² ኹȌ ኼ¿ 168 168
- 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
- 9/13/2011 Modal và Modaless Dialogs ×ʹǣȋ ¯ዒȌ Modal dialog: ± ዋኸ ዞዐ ዚዜዙዄ ¯ዅׯዛዘ ¯×ȋዛዕ ዌǡ Cancel) ± ኾ ¯ዎዛዘዔýǡ ዌȋȌǤ ǣȋዛዘ Ȍ ± ዋ×ǡኻ × ዌ ዞዐ ዚ ዜዙǤ ± ×ኾ îǣǣ ኼ¿ 170 170
- 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
- 9/13/2011 0×ȋ Ȍ ïዉǡ ȋȌዛዓዜǣ ± ǣǣȋȌïȋ ¯ዋȌ ± ǣǣ ȋȌï ȋȌ ×ዅዅǡዞýዠዉ ï¯ዠ ዉዟዞÀ ዘ ኼ¿ 172 172
- 9/13/2011 Hàm MessageBox (1) nType: what buttons display ? ኼ¿ 173 173
- 9/13/2011 Hàm MessageBox (2) nType: button OR Icon ኼ¿ 174 174
- 9/13/2011 2.6.2. Common Dialogs (1) ኸ ዒ ²ዙዑ ዘዟ ዐዅ ዜ ዙ²ǣ ዌ ǡ ዌǡ Ȁǡኸǡǥ ዒ¯ ×ዔ lý ኼ¿ 175 175
- 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
- 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
- 9/13/2011 CFileDialog (2) bOpenFileDialog: ± TRUE: Open ± FALSE: Save As lpszDefExt: ± 0Øዖዒ ¯ዋ ዚ²ኼǤዅዛዕî Øኼ°¯ØዖዒØ ዚ ǡኹዖዒ¯ዛዘ ዊ¯ዋዖዄ ¯ዛዘ ዠ¯ዒ²² ዚኼǤዅዋ ǡዄØ ×ኹዖዒ¯ዛዘ ²Ǥ lpszFileName: ± ²ኼዖዄ¯ዛዘ ኸዉØ ዚ ǤዅዋǡዄØ ×² ኼኸዉǤ ኼ¿ 178 178
- 9/13/2011 CFileDialog (3) dwFlags: ± ዒ±ዅዘ ዚዒዅ ዕ ± î ዊዒǤKhi ዊዞዅ² ኸ ï ̴Ǥ ǡîዒዞዟ ¯ዐ¯ዟዟዅኼ ¯ዋØዋ ዛዖǤ lpszFilter: ± ዒ ዑ ዊ¯ዋዟዒዌ × ዙ ኼǤዅ ዊ¯ዋዒዌ ኼǡ ዊ ዟኼ ×ዒዌ ዔኸዉ box. pParentWnd: ± ዒ ውውዔ¯ዎዛዘዒኼ ዞዐ ዚǤ ኼ¿ 179 179
- 9/13/2011 Các мд н е CFileDialog CFileDialog::DoModal() - Ǥ(ዛዓ ዜ ዛዕî ዌïï ). - ዛዕî ï ǡ ዌ ዜ £ዝ¯ ዚዒǡ¯ ¯ዛዘ ዜዙgǤ¯×ǡ ×ዌ ² ¯ኼ ዅኼ Ø ዛዕîኼዒǤ - ዒዅዝዝዔǤ ኼ¿ 180 180
- 9/13/2011 Các мд н е CFileDialog GetFileExt: ኸኹዖዒ ዚኼ¯ዛዘ ዌ GetFileName: ኸ²ኼ¯ዛዘ ዌ GetFileTitle: ኸኹ²ȋØ ×ኹዖ ዒȌ ዚኼ¯ዛዘ ዌ GetFolderPath: ኸዛዙ ዚኼ¯ዛዘ ዌ GetPathName: ኸ¯ዛዕኻ ዚኼ¯ዛዘ ዌ ኼ¿ 181 181
- 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
- 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
- 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
- 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
- 9/13/2011 CFontDialog Ȁ ǣ ± CFontDialog( const CHARFORMAT& charformat, DWORD dwFlags = CF_SCREENFONTS, CDC* pdcPrinter = NULL, CWnd* pParentWnd = NULL ); ኼ¿ 186 186
- 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
- 9/13/2011 ʹǤǤ ¯ЧШØк 2.7.1. Các ¯ Øዙ ʹǤǤʹǤዅዅ ¯²ዒ ʹǤǤ͵Ǥ ኸ¯ Àዔ ¯ ʹǤǤͶǤዔ ዚ ¯ ʹǤǤͷǤኼ ኼ¿ 188 188
- 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
- 9/13/2011 ʹǤǤʹǤЦЦ ¯ЧШ²гЗȋͳȌ ዠ ዌ¯ዎîǤ ±Ǥ ዅዅ ¯¯²ȋኽዅǡ £ ዊȌ ኼ¿ 190 190
- 9/13/2011 ЦЦ¯ЧШ²гЗȋʹȌ ዅኼ ዒ À ¯ ዕ¯ዅዅǤ ዞዐ ዚ¯ ኼ¿ 191 191
- 9/13/2011 ЦЦ ¯ЧШ²гЗȋ͵Ȍ ዞዙǣ ± Format/Tab Order (Ctrl+D): ዅ ኼ ዜ ዠ ዌȋ Ȍ ኾ phím Tab cho các ¯ ± Click ዒ ¯ ዌ ዜ ዠ. 192 192
- 9/13/2011 ЦЦ ¯ЧШ²гЗȋ͵Ȍ ዞዙ ǣ ± î¯ ዔ¯ኾÀኽ (Alt + ?) ± Ký ዠ chân trong caption ዚ ¯ Ví ዙ: ± ǣDzƬdzAlt+M tra: Format/Check Mnemonic ኼ¿ 193 193
- 9/13/2011 ʹǤǤ͵Ǥ Й¯Ч Àе ¯ЧШȋͳȌ ዟ ዉ ¯ዛዘ ዛ trên ¯là gì ? ȋኹኸȀዉȀዟ ዉ) ኸȀዉዟዉ ዚ ¯ኾ cách nào ? ዟዉ ኸዝ¯ ×ዘ ዉ ? ዠዉ¿ ኹîዛዓ ዔ ¯ǫȋዞýዠዉ ኹîȌ Có nhu ኹ ኼý¯ዎዔ¯ ¯×ØǫȋThay ¯ዐ ዟ, di , ȀǡȀǡǥȌ ኼ¿ 194 194
- 9/13/2011 Й¯Ч Àе ¯ЧШȋʹȌ 0ኸȀ¯ዛዟዉዝȀ¯ዅ ¯ ኹØ ዅýȋȌ¯ዛዘ ¯£ýȋኽȌ ዔ¯Ǥ ኹዞዙ ±¯ዏዒዟዉዟ ዋዅኽዟዒዋ²¯ ȋዛዓ ዔዛዕîȌ 0ዠ ዉ ኼýዔ¯ ኹዌ ዛዓዜ À ዘȋ ዚዔ ዞዐȌǤØ ዅ¯ዛዘ ¯£ýዔ¯Ǥ ዅዞýዠዉ ¯ ኼ¿ 195 195
- 9/13/2011 0£ ký Ц cho ¯ЧШȋͳȌ ×ʹዅ ×ኽዔ¯î ዙ ¯À : ± 0 tham ዅ ¯ዅ ¯ (thay ¯ዐ ዉ ) îዅ ± 0 ኼ/¯ዛ ዟ ዉ ዝ/¯ዅ ¯ îዅ ዠ ዉ: ± ዌ ¯ ዎ¯£ýዅ ± Format/Add variable Right Mouse/Add variable ኼ¿ 196 196
- 9/13/2011 0£ ký Ц cho ¯ЧШȋʹȌ Tênǡዟ ዌ ዅǣ ዉ ዚዅ Control/Value ²ዅ Chú thích ኸ ዉ trong code ኼ¿ 197 197
- 9/13/2011 0а г рЪр¯ЧШ/Ц ዙ ¯À : ± 0ዏ ዒ giá ዋ ዚዒ ¯ ዔ ዅ giá ዋ ዚ nó ± ኼ ኼ khi ኹ ኼ ዟ ዉ ዝ ¯ ¯ዛ ዟ ዉ ra ¯ . ዠ ዉኾǣ BOOL UpdateData(BOOL bSaveAndValidate) ± αȋͳȌǣዟዉ²¯ ¯ዛዘ ኼኼዅǤ ± α ȋͲȌǣዟዉዛዟ ዅ¯ዛዘ ¯ዛ¯Ǥ ኼ¿ 198 198
- 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
- 9/13/2011 0£ýп lý с Ъ (2) Ví ዙ: Button ShowMSG Add Event Handler ዌ ዠ ዌ ዔኼØ ዉ ¯ዉዛዓዜ ²ዞý ¯ዛዘ ዘý ኼ¿ 200 200
- 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
- 9/13/2011 ʹǤǤͶǤе л ¯ЧШ ኸዔ¯ዉ ¯Ǥ ዒዎዔÀዙǣ ኸ ¯ȋ Ȍ ñ ዞዐ¯ ዉǤ ዔ ዚ¯ኻኸዝዔ ዓዖǤ ¯× ×ዞዙ¯ዅ À ዘ ዚዔǤ ¯ï ዅዅȋ Ȍǡ ׯ ኾCreate ȋዔ ዓዖȌï ዠ Ǥ ኼ¿ 202 202
- 9/13/2011 ʹǤǤͶǤͳǤеȋͳȌȂ зЗ ýዒኼǡዞý¯ዒኼዉǡØ ¿¿ኼዉ ዞዐ Ǥ ǣȋȌ ǣዖ¯ዎዛዘዒኼዔ ዎǤ BOOL Create ( ǡȀȀዒኼȋ̴ǡ ES_PASSWORD, ES_READONLY) Ƭ ǡȀȀዌ¯ዒÀ ዛዔ ዒኼ ȗǡȀȀውዔ¯ዎዛዘ ዞዐ ȀȀ0ዋዒኼ ); ኼ¿ 203 203
- 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
- 9/13/2011 еȋ͵ȌȂ пýсЪ Ø¯ዉዠዉ²ዒኼዉ ¯ዛዘ ዞ¯ዅ ዞዐ Ǥ ዙ ዞýدዛዘ ¯ዛዘ ² دዉȋȌዔ ዞዐ ×ǣ ± ON_Notification (ID, memberFxn ) ± ¯×ǣ ¯ዋ¯ዋዒኼǢ ²ዞýدዉዛዓዜǤ ± ÀዙǣON_EN_UPDATE(IDC_EDIT1, &CMyDialogDlg::OnEnUpdateEdit1) ኼ¿ 205 205
- 9/13/2011 еȋͶȌȂ пýсЪ Ø¯ዉ²ዒ ኼǣ (EN = Event Notification) ኼ¿ 206 206
- 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
- 9/13/2011 ʹǤǤͶǤʹǤеȋͳȌȂ зЗ ý ¯ï ዌȋǡ ǡ ǡǥȌȋȌ ȋȌ Hàm Create() BOOL Create ( ǡȀȀዒØ ǡȀȀïȋÀዙ̴Ȍ Ƭ ǡȀȀዌ¯ዒǡÀ ዛዔ ȗǡȀȀው ዞዐ ȀȀ0ዋï ዌ ); ኼ¿ 208 208
- 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
- 9/13/2011 еȋ͵ȌȂ пýсЪ Ø¯ዉ²ï ዌ ON_BN_CLICKED(IDOK, &CMyDialogDlg::OnBnClickedOk) ኼ¿ 210 210
- 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
- 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
- 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
- 9/13/2011 еȋ͵ȌȂ пýсЪ ዠዉ² ዞýዛዓዜ ON_LBN_SELCHANGE(IDC_LIST1, &CControlDemoDlg::OnLbnSelchangeList1) ኼ¿ 214 214
- 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
- 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
- 9/13/2011 ʹǤǤͶǤͶǤеȋͳȌ ዔý ȋዒኼኹዑዘኼዔ ኹዞ ዛዔ Ȍ ȋȌ Hàm Create() BOOL Create ( ǡȀȀ const RECT& rect, CWnd* pParentWnd, UINT nID ); ǣ ǣ ± ̴ǣ ዋኼ ዊ¯ዛዘ ዌዝ Ǥ ± ̴ǣዛ ዋዛዕዠ ± ̴ǣዛ ×ኼዒዔ ኼ¿ 217 217
- 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
- 9/13/2011 еȋ͵ȌȂ пýсЪ ዠዉ²ዞý ON_CBN_SELCHANGE(IDC_COMBO1, &CControlDemoDlg::OnCbnSelchangeCombo1) ኼ¿ 219 219
- 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
- 9/13/2011 ʹǤǤͶǤͷǤеȋͳȌ ዔý¯ ±ዠ ዌ ዋȋ£ȀȌዒ¯ ¯Ǥ ዛዕ¯°ዔዒȋ¯ዉ ዋዎ ዌȌ Hàm Create() virtual BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ); ኼ¿ 221 221
- 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
- 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
- 9/13/2011 ʹǤǤͶǤǤеȋͳȌ ዔýዛዘ ዛዓዠ ዒ Àǣ ± Tick Marks ± ǣ ± Orientation: Horizontal & vertical Hàm Create() virtual BOOL Create( ǡȀȀ̴ǡ̴ǡǥ const RECT& rect, CWnd* pParentWnd, UINT nID ); ኼ¿ 224 224
- 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
- 9/13/2011 ʹǤǤͶǤǤеȋͳȌ ዔýዅ¯ዒዞý ȋȌ Hàm Create() virtual BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ); ± dwStyle: ̴ǣዅ¿ኾ¯ዜ ̴ǣዊ¯ዒዅ¯ዒ²ዙ ኼ¿ 226 226
- 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
- 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
- 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
- 9/13/2011 пýсЪе ¯ЧШЗ г ዠዉዛዕî ± CWnd::OnHScroll() ዅ¯¯ዜǡ ± CWnd::OnVScroll() ዅ¯ ዠዉ¯ዛዘ ዞ¯ዅዔ ዞዐ ዜ¯ ȋዅዝዝዔȌ ʹዞýዠዉ ¯ × ዒ (Spin, Progress, slider, scroll), ኹ ¯ዋ¯ ኾ ኼ¿ 230 230
- 9/13/2011 2.7.5. Bài Н Ȃ Common Controls (1) Ex2-Ǥዞዙዒዎ¯Øዙ ኼ¿ 231 231
- 9/13/2011 Ц Н г tính cho các control Ví ዙǣDzdz ± ID: IDC_BT_SHOWMSG ± Caption: Show &Message ኼ¿ 232 232
- 9/13/2011 Ц Н г tính cho các control Ví ዙ: Combo box ± ID: IDC_CB_PROG ± Data: Notepad;Spider;Solitaire ኼ¿ 233 233
- 9/13/2011 НȋʹȌ Ex2-7b. UserProfile : ± Combo box ± Listbox ± Radio button ± Spin ± Slider ኼ¿ 234 234
- 9/13/2011 мд 3. г Я л ¯Ч nâng cao 3.1. DLL 3.2. Thread 3.3. Windows Socket ኼ¿ 235 235
- 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
- 9/13/2011 3.1.1Ǥ еЪ ȋ Ȍǣዛዉ²ዅ ¯ዒǤ ዒዋ ዜዒ ዠ Ǥ±ዜዙ ዄ ×ዌ¯ዅǤ ዒ ׯዛዘ ዜዙዞዙ¯ዏ ዕǤ ¯ዛዘ ʹǣ ± ȋኸǣ ±ዜዙ² ዞዙȌǤ ± ȋዒዒǣዟ ዊዞዙዒ ዒ ዚȌ ኼ¿ 237 237
- 9/13/2011 Static Library ዛዉ²ዅ Âȋ link library) ? Àዙǣ ȋȌ ዛዉ ዌ Ǥ ኼ¿ 238 238
- 9/13/2011 How DLL Works ? ዊ ¯ዛዘ ዒ ዔ ዛዓ ¿ዠ thi. ኼ¿ 239 239
- 9/13/2011 Ў¯Шпк À ዛዔ ዜዙውǤ ዜዙ ×î ͳǡዅዉ ዒዔǤȋ ዜዙ ×ዒዟዉ²ǡዛ × ዃ ዉȌǤ Ø Öዞዙǡ ××ው ዒዔ ኹ ኸǡ ዊ ኹዅǡ ዛዓ¿ ØዋዛዖǤ ዛዘ ¯ǣኼ¿ዞý ዜ ዓǤ ኼ¿ 240 240
- 9/13/2011 г ዉ ዚ ȋ Ȍ ዔȋȌ ዅ ዙ ȋȌ Resource (bitmap, font) ÀዙǣዞዙǤ ዜ cards images + functions ЯфО¯рн п й ×Ь¯рн Нт² ngoài. ኼ¿ 241 241
- 9/13/2011 Hàm DllMain() Øዠ ዛዒ ዛዓ¿ ¯ዒ ኼǡዛ × ዜ¯ ዉ ዌȋȌǤ 0ዛዘ ዌዖׯዛዘ ዒዔኹ¯ኹ²Ǥ0ዠ ዉዖ ዛዔ ዞዙዒ ዚǤ ñ¯ዛዘ ዌ×ውዒዔ¯ ዠ ዉዟ× ኹዅǤ ኼ¿ 242 242
- 9/13/2011 3.1.2ǤЗ ×͵ǣȋ Ȍ ± ዖዒȋ Ȍ ± Øዛዕዔ²ዅÂ (Regular DLL with MFC statically linked). ± Øዛዕዔ²ዅ¯ዒ (Regular DLL with MFC dynamically linked) ኼ¿ 243 243
- 9/13/2011 MFC Extension DLL ±ዞዙ ዔȋȌኻኸዝ ዔ Ǥ ±ዜዙî ×ዞዙ ዔ ዚ×Ǥ ²ዅዔ ዚ ²ዅ¯ዒǤ ዊዞዙ ዜዙ ×À£Dz dz Øî¯ዛዘ ዖ ዜዙዞዙ²ዅ ¯ዅዛዉ ȋ Ȍ ኼ¿ 244 244
- 9/13/2011 Regular DLL Ȃ Statically linked to MFC ዞዙ ዔ ዚ ዛዔ² ዅÂǤȋ ¯ ዚ ¯ዛዘ °ዑዛዓዜ¯ዛዘ ዞዙ¯ዅȌǤ × À ዛዔ ዔǤ ׯዛዘ ዞዙዖ ዜዙ ͵ʹ ȋØ ኹØዛዕ Ȍ ዊ ǡØ ዔ có trong DLL. ኼ¿ 245 245
- 9/13/2011 Regular DLL Ȃ Dynamically linked to MFC ዞዙ ዔ ²ዅ ¯ዒȋز ዉ ዚዔ ǡ ዊዌ¯ዅ¯ዛዘ îȌǤ À ዛዔ ዔ² ዅ Ǥ ׯዛዘ ዞዙዖዜዙ ኸüዜዙ͵ʹȋዛ ኹ × ዚ ዛዓዜ¯°Ȍ ኼ¿ 246 246
- 9/13/2011 3.1.3Ǥ Энк ʹǣ ± ዌï ዜዙ¯ዛዘ ȋ linking hay early binding). ± ዌï ዠ ȋ late binding) ኼ¿ 247 247
- 9/13/2011 Эï нк¯мй ЗȋȌ ¯ዛዘ ዒዔዜዙዞ ዙׯዛዘ ዒዔ¯ዠ Ǥ ¯ዛዘ ዝ¯ኹኸዜዙ ² ኹ× ×ዞዙ¯ዅ ዚ ׿ዠ ØǤ ²ዅ¯ዅ ዎî¯ዛዘ ዅኼዝ¯ኹǤ ×ኸዉǣዎዒዔǡዜ ዙ ×دዒዅ² ኹØዏǤ ኼ¿ 248 248
- 9/13/2011 Эï с ȋȌ ኼ¿²ዅ¯ዋ ኹ² ዒዔǤȋ0ዛዘ ×ዒ ዚ¯ዛዘ ዜዙ² ኹዞዙȌǤ ×£ǣዎ Øዜ ኼ¿ ዉ ǡ ዌǤ ± ኹîLoadLibrary() ¯× ¯ዛዘ ² ኹǤ ± Hàm GetProcAddress() ¯ኸ¯ዋ ዊȋ ውȌ ዚ ኹîǤ ± Hàm FreeLibrary() ¯ ዜዙØ Öî¯ዅǤȋዄ¯ዛዘ ×Ø Ö ዜዙî¯ዅȌ ኼ¿ 249 249
- 9/13/2011 ዌዌï ዠ ዜዙ ኼ¿ 250 250
- 9/13/2011 3.1.4ǤЦ ዌዠዒȋ ²ዅ¯ዒዔ Ȍ ዜʹÀȋ ዒȌ ±ዜዙ ዌ¯ዅǤ Ȁ ዌ DLL ኼ¿ 251 251
- 9/13/2011 ЭЦ ኼ¿ 252 252
- 9/13/2011 Й ኼ¿ 253 253
- 9/13/2011 ኸ¯ዉǤ .h ዒዒ ዚ ዛ ዚዜዙØዛዕ ኼ¿ 254 254
- 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
- 9/13/2011 с ЙȋЭȌ ኹዠ ዒǣ ኹዠ ǣ ኼ¿ 256 256
- 9/13/2011 ²Ь ዛ²ዋ ዜዙØዛዕ ኼዅǣǤǤ ዜዙዞዙ ኹî¯ዅʹ ኼ²Ǥ ኼ¿ 257 257
- 9/13/2011 3.1.5ǤЭнкпк ዌ¯ ዅï ዜዙȋ time) ዜዙ¯ዓȋȌ ዜኼǤǤ îዛዙ ኼǤ ȋ Ȁ Items) ኼ¿ 258 258
- 9/13/2011 нк ዎዞዙኸȋȌዝǡ ዜዙዄ ¯×Ǥ ዐ ኼ¿ 259 259
- 9/13/2011 Ц с н £ ዅ ዞýï ዒǡዌ ǡ ዛዓዜ ዚ Àዙǣዞýዠዉ ዒï ዒ ኼ¿ 260 260
- 9/13/2011 ЭнкЭШ ዜ ዝǣ ± ኾLoadLibraryǤ ×ዏØኾ ዅ × Ø ± ኹዅኾGetProcAddress. ዉ ዌ ØØǤ ± ×ውዒዔኾ FreeLibrary. ኼ¿ 261 261
- 9/13/2011 ЏкЭШ ው ዎ ዜዙǤȋዙዒ trong DLL) ዔÀዙǡኸ ×ǣ 0ዋÂ ው ʹǣ ኼ¿ 262 262
- 9/13/2011 н Зо ኼ¿ 263 263
- 9/13/2011 Àкǣ Э ኼ¿ 264 264
- 9/13/2011 пЪ ЭШ Ø ±Ǥ îዛዙ ዜ ዙዞዙǤዜዙ¯ዛዘ Ø ǫ×ዠ ¯ዛዘ ዜ £ȋ ዒǡ Ȍ ዚዜዙØǫ Øኽዜዙǡ ±Ǥ î ዛዙ ዜዙǤዠ ¯ዛዘ ዜ £ ዚዜዙØǫ ኼ¿ 265 265
- 9/13/2011 Ex-3.? DLL ዅዒ ዜዙ Sketcher ኼ¿ 266 266
- 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
- 9/13/2011 3.2.1ǤгЯЪȋͳȌ ²ǡዑዜዙ¯ ዒዅ trình (Process) ዒዅ¿ × ዜዒ ¿ȀዏȋthreadȌዠ Ǥȋ Ȍ Main Thread/ Primary Thread: là ÀȀ ¯ዋ ዚዒዜዙǤ Secondary Thread: các ȋዅ ዛዓ¿¯ዛዘ ኼ ¿²) ኼ¿ 268 268
- 9/13/2011 гЯЪȋʹȌ Multithreading: î ¯ዏዕ ȋ0ዛዘ ኼዋ - schedule) ዉ¯ ዕ thread Ȃ ዓ ዅ¯ዉȋmultitasking) ኹ¯ዏǫ ± ኮዙዎዠ ዉዉዙ¯ዏዕǤ ± ዛዕ ዉዙዞý¯ዛዘ Dzኼ dz ȋ Ȍ ዞýዛዓ ዛዕî Dz dzȋȌ ± Àዙǣዜዙዔ grammar. ኼ¿ 269 269
- 9/13/2011 3.2.2ǤН¿¯а ኼ¿¯ዏȋȌǣ ± Multithreading in pure C ± Multithreading in Win32 API ± Multithreading in MFC ዔǣዔ ዓዖ¯×× ¯ዒ ዠ ዚǤ ዉʹǣ ± Worker Thread ȋ¿ዞýዒȌǣዑዘዠ ዉ ዞý²ǡDzኼ dzȋ ȌǤ ± ȋ¿ዉȌǣዠ ዅ ዅኼ² ኹዛዕîȋ ዓ ዅ Messages) ኼ¿ 270 270
- 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
- 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
- 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
- 9/13/2011 Thread Function (1) ዠ ዚዒȋ Function/ThreadProc) ȋ0ዛዘ ዌዖዉ¯Ȍǡ ኹ ዙ ȋ² ዔȌǤ ×ǣ UINT ThreadFunc (LPVOID pParam) ው¯ዅͳ ኸï ዟዉ ዚ ዛዕኼ¿¯ዋÂǤ0ዛዘ Øዎዜʹ ዚ AfxBeginThread ኼ¿ 274 274
- 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
- 9/13/2011 Thread Priorities (1) ዅ¿ȋ Ȍ¯ዛዘ ዉ¯ኼዋ ዠ ዔ ኸ¯ዒዛ² ȋ ዅዕ ȌǤዏǣ REALTIME_PRIORITY_CLASS HIGH_PRIORITY_CLASS NORMAL_PRIORITY_CLASS IDLE_PRIORITY_CLASS ኸ¯ዒዛ² ዚዅ¿¯ዛዘ ዅኼዛዓ ¯ዎዔ ¯ዒዛ² ዚ ×ዒ Ǥ ×ዅኼዌȋȌǡ îǣǣǣ ኼ¿ 276 276
- 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
- 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
- 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
- 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
- 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
- 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
- 9/13/2011 3.2.6Ǥ0аг Thread Synchronization ኸ¯ǫ ǫ ኸͶ ዓ ዅ¯ዏዒ ǣ ± Critical sections ± Mutexes ± Events ± Semaphores ዉǣ ± Chapter 17, Programming Windows with MFC ± MSDN ኼ¿ 283 283
- 9/13/2011 3.2.7ǤÀк Àዙ¯ዓዌዞዙî Win32 API, MFC ዌ ± ¿ዞýዒȋȌ ¯ዒ × ዉዙዠ ዉ ዟÖØ ¯ዅ ×ÀዉዝǤ ± ¿ À ዚዜዙዠ ዉዅዛዕ dùng. Nút Start/Stop, Close ኼ¿ 284 284
- 9/13/2011 Ex3-?. Prime Sieve ዠዜዙዎ²ዎȋ Ȍዔ¿ዞýዒȋ Ȍ ዉ ዞýኼዎ ²ዎǤ ኼ¿ 285 285
- 9/13/2011 3.3 Н¿ 3.3.1Ǥዒዎዉ 3.3.2Ǥኼ¿ ዔ 3.3.3Ǥኼ¿ ዔ 3.3.4ǤÀዙዌ ኼ¿ 286 286
- 9/13/2011 3.3.1ǤгЯЪȋͳȌ Winsock (Windows Socket): ± ኸዜዝȋȂ UNIX) ± ዉ ዋ ዙዠ² ዜ ØȋȌȀ Port: ± ዎዉ ዐዋ ዙǡዉዟ ዅ ¿² îዒ¯ዏዕዞዙ ዜ ȀǤ ኼ¿ 287 287
- 9/13/2011 гЯЪȋʹȌ Socket ? (= IP + Port) ± 0دኹ ዎ ±ዜዙ ዞኼ ׯዛዕǤ ± ኸዉ¯ዒ ዛዓ¿ዅዎǡ ¯ዐዟዉዔዒ ዛዓ¿²ዒ À ኼ¿ 288 288
- 9/13/2011 гЯЪȋ͵Ȍ ǣ ± Datagram socket Ȃ ዞዙ UDP Ít ኼ Øዞ Øዅዎȋ Ȍ ± Stream socket Ȃ using TCP ኼ ×ዞ ዛዔዅዎȋ - oriented) ± Raw and others ኼ¿ 289 289
- 9/13/2011 Н¿ ² ኮዙ͵ʹǣዞዙዛዉ ኮዙ ǣዞዙዔ ȋ¯×× Ȍ ኼ¿ 290 290
- 9/13/2011 3.3.2ǤН¿ е ዛዉ ʹǤǤȋʹ̴ʹǤȌ #include #pragma comment (lib,"ws2_32.lib") ዖ int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData); ኼ¿ 291 291
- 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
- 9/13/2011 Giao Ц Client Ȃ Server е TCP 293 293
- 9/13/2011 294
- 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
- 9/13/2011 296
- 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
- 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\ӅQ62&.B',$*5$0 SOCK_STREAM, SOCK_RAW) int SURWRFROFKӋ ÿӍQKU}OңLJLDRWKӫF ); Àዙǣ SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_IP); ኼ¿ 298 298
- 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
- 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