// -// Chương trình demo: Application đơn giản Windows // Sử dụng thư viện API Windows // GENERIC_APP.CPP // Nguyen Tri Tuan - Khoa CNTT – ĐH.KHTN Tp.HCM // -#include "stdafx.h" #include "resource.h" #define MAX_LOADSTRING 100 // Các biến HINSTANCE TCHAR TCHAR toàn cục hInst; szTitle[MAX_LOADSTRING]; szWindowClass[MAX_LOADSTRING]; // instance application // Tiêu đề cửa sổ // Tên lớp cửa sổ (window class name) // Prototype hàm xử lý ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); // Hàm xử lý application int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Khởi tạo biến chuỗi toàn cục LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_C, szWindowClass, MAX_LOADSTRING); // Đăng ký lớp cửa sổ cho ứng dụng MyRegisterClass(hInstance); // Khởi tạo ứng dụng if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } // Khởi tạo MSG HACCEL hAccelTable bảng phím tắt (cho menu item) msg; hAccelTable; = LoadAccelerators(hInstance, (LPCTSTR)IDC_C); // Vòng lặp tiếp nhận xử lý thông điệp while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } // // Hàm: MyRegisterClass() // Đăng ký lớp cho cửa sổ (Frame window) // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; Nguyen Tri Tuan – Khoa CNTT – DHKHTN Tp.HCM 1/6 wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_C); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCSTR)IDC_C; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // Hàm: InitInstance(HANDLE, int) // Khởi tạo cửa sổ giao diện cho ứng dụng // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // Hàm: WndProc(HWND, unsigned, WORD, LONG) // Hàm nhận xử lý thơng điệp cho cửa sổ giao diện // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; TCHAR szHello[MAX_LOADSTRING]; LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Kiểm tra xem user chọn menu item nào: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: Nguyen Tri Tuan – Khoa CNTT – DHKHTN Tp.HCM 2/6 DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // Cập nhật lại nội dung hình client RECT rt; GetClientRect(hWnd, &rt); DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER); EndPaint(hWnd, &ps); break; case WM_DESTROY: // Kết thúc ứng dụng PostQuitMessage(0); break; default: // Gọi xử lý thông điệp mặc định return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // // Hàm: About(HWND, UINT, WPARAM, LPARAM) // Xử lý thông điệp cho cửa sổ hộp thoại About // LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; } Nguyen Tri Tuan – Khoa CNTT – DHKHTN Tp.HCM 3/6 // -// File mô tả tài nguyên ứng dụng (Resource of application) // Generic_App.rc // Nguyen Tri Tuan - Khoa CNTT – ĐH.KHTN Tp.HCM // -#include "resource.h" #define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE resource // #define APSTUDIO_HIDDEN_SYMBOLS #include "windows.h" #undef APSTUDIO_HIDDEN_SYMBOLS #include "resource.h" ///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // English (U.S.) resources #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) #ifdef _WIN32 LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US #pragma code_page(1252) #endif //_WIN32 // -// Mô tả icon // -IDI_C ICON DISCARDABLE "c.ICO" IDI_SMALL ICON DISCARDABLE "SMALL.ICO" // -// Mô tả menu // -IDC_C MENU DISCARDABLE BEGIN POPUP "&File" BEGIN MENUITEM "E&xit", IDM_EXIT END POPUP "&Help" BEGIN MENUITEM "&About ", IDM_ABOUT END END // -// Mơ tả phím tắt (Accelerator) // -IDC_C ACCELERATORS MOVEABLE PURE BEGIN "?", IDM_ABOUT, ASCII, ALT "/", IDM_ABOUT, ASCII, ALT END // -// Mô tả dialog // -IDD_ABOUTBOX DIALOG DISCARDABLE 22, 17, 178, 39 STYLE DS_MODALFRAME | WS_CAPTION | WS_SYSMENU CAPTION "About" Nguyen Tri Tuan – Khoa CNTT – DHKHTN Tp.HCM 4/6 FONT 8, "System" BEGIN ICON LTEXT LTEXT DEFPUSHBUTTON IDI_C,IDC_MYICON,14,9,20,20 "Generic App Version 1.0",IDC_STATIC,39,10,119,8, SS_NOPREFIX "Copyright (C) 2004",IDC_STATIC,39,20,83,8 "OK",IDOK,138,25,30,11,WS_GROUP END #ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // TEXTINCLUDE // TEXTINCLUDE DISCARDABLE BEGIN "#define APSTUDIO_HIDDEN_SYMBOLS\r\n" "#include ""windows.h""\r\n" "#undef APSTUDIO_HIDDEN_SYMBOLS\r\n" "#include ""resource.h""\r\n" "\0" END TEXTINCLUDE DISCARDABLE BEGIN "\r\n" "\0" END TEXTINCLUDE DISCARDABLE BEGIN "resource.h\0" END #endif // APSTUDIO_INVOKED // -// Mô tả chuỗi (String table) // -STRINGTABLE DISCARDABLE BEGIN IDS_APP_TITLE "Generic Application - API" IDS_HELLO "Hello World!" IDC_C "About Generic Application" END #endif // English (U.S.) resources ///////////////////////////////////////////////////////////////////////////// #ifndef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE resource // ///////////////////////////////////////////////////////////////////////////// #endif // not APSTUDIO_INVOKED Nguyen Tri Tuan – Khoa CNTT – DHKHTN Tp.HCM 5/6 // -// File header mô tả số chương trình // Generic_App.h // Sử dụng cho: Generic_App.cpp, Generic_App.rc // Nguyen Tri Tuan - Khoa CNTT – ĐH.KHTN Tp.HCM // -// Used by C.RC // #define IDR_MAINFRAME 128 #define IDD_C_DIALOG 102 #define IDD_ABOUTBOX 103 #define IDS_APP_TITLE 103 #define IDM_ABOUT 104 #define IDM_EXIT 105 #define IDS_HELLO 106 #define IDI_C 107 #define IDI_SMALL 108 #define IDC_C 109 #define IDC_MYICON #define IDC_STATIC -1 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define #define #define #define #endif #endif _APS_NEXT_RESOURCE_VALUE _APS_NEXT_COMMAND_VALUE _APS_NEXT_CONTROL_VALUE _APS_NEXT_SYMED_VALUE Nguyen Tri Tuan – Khoa CNTT – DHKHTN Tp.HCM 129 32771 1000 110 6/6 ... Nguyen Tri Tuan – Khoa CNTT – DHKHTN Tp.HCM 5/6 // -// File header mô tả số chương trình // Generic_ App. h // Sử dụng cho: Generic_ App. cpp, Generic_ App. rc // Nguyen... Nguyen Tri Tuan – Khoa CNTT – DHKHTN Tp.HCM 3/6 // -// File mô tả tài nguyên ứng dụng (Resource of application) // Generic_ App. rc // Nguyen Tri Tuan - Khoa CNTT –... -STRINGTABLE DISCARDABLE BEGIN IDS _APP_ TITLE "Generic Application - API" IDS_HELLO "Hello World!" IDC_C "About Generic Application" END #endif // English (U.S.) resources /////////////////////////////////////////////////////////////////////////////