Professional Documents
Culture Documents
M t
Minh ho di y cho php to mt ng dng nh di y: - Hin dng ch Hello World! trn vng client. - H thng menu gm 2 drop-down menu. Mi menu item c th c nhn chn. - Hin th dialog n gin (About dialog).
1/10
M chng trnh
Tp tin Win32Demo.cpp S dng cc header file cn thit
// Win32Demo.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h"
Cc hm cn thit
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings khi to cc chui ton cc LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_WIN32DEMO, szWindowClass, MAX_LOADSTRING); //ng k lp ca s
MyRegisterClass(hInstance);
// Perform application initialization: Khi to ng dng if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } // Np accelerator table hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN32DEMO); // Main message loop: Vng lp thng ip chnh while (GetMessage(&msg, NULL, 0, 0)) Vn Ch Nam Khoa CNTT H KHTN TP.HCM 2/10
{ if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. ng k lp ca s // // COMMENTS: ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; // Ch ra hm x l thng ip wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32DEMO); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCSTR)IDC_WIN32DEMO; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HANDLE, int) // // PURPOSE: Saves instance handle and creates main window // Lu li gi tr handle ca instance v to ca s chnh BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Lu gi tr handle trong bin ton cc hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) // Nu to khng thnh cng { return FALSE; } Vn Ch Nam Khoa CNTT H KHTN TP.HCM 3/10
ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. X l thng ip gi // n ca s // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // 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: // X l cc menu item c chn wmId = LOWORD(wParam); // Ly gi tr ID ca menu item wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; // V mn hnh case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... RECT rt; GetClientRect(hWnd, &rt); DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER); Vn Ch Nam Khoa CNTT H KHTN TP.HCM 4/10
EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Mesage handler for about box. // Hm x l cho about box. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { // Dialog c khi to case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; }
5/10
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) #ifdef _WIN32 LANGUAGE 9, 1 #pragma code_page(1252) #endif //_WIN32 ///////////////////////////////////////////////////////////////////////////// // // Icon // // Icon with lowest ID value placed first to ensure application icon // remains consistent on all systems. IDI_WIN32DEMO IDI_SMALL ICON DISCARDABLE "Win32Demo.ICO" ICON DISCARDABLE "SMALL.ICO"
///////////////////////////////////////////////////////////////////////////// // // Menu // nh ngha menu IDC_WIN32DEMO MENU DISCARDABLE BEGIN POPUP "&File" BEGIN MENUITEM "E&xit", END POPUP "&Help" BEGIN MENUITEM "&About ...", END END
IDM_EXIT
IDM_ABOUT
///////////////////////////////////////////////////////////////////////////// // // Accelerator // IDC_WIN32DEMO ACCELERATORS MOVEABLE PURE BEGIN "?", IDM_ABOUT, "/", IDM_ABOUT, END
ASCII, ASCII,
ALT ALT
///////////////////////////////////////////////////////////////////////////// // // Dialog // Cc thng tin to lp dialog IDD_ABOUTBOX DIALOG STYLE DS_MODALFRAME CAPTION "About" FONT 8, "System" BEGIN ICON LTEXT LTEXT DEFPUSHBUTTON END DISCARDABLE 22, 17, 230, 75 | WS_CAPTION | WS_SYSMENU
// // TEXTINCLUDE // 2 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 3 TEXTINCLUDE DISCARDABLE BEGIN "\r\n" "\0" END #endif // APSTUDIO_INVOKED
///////////////////////////////////////////////////////////////////////////// // // String Table // Khai bo cc chui s dng trong ng dng STRINGTABLE DISCARDABLE BEGIN IDC_WIN32DEMO "WIN32DEMO" IDS_APP_TITLE "Win32Demo" IDS_HELLO "Hello World!" END #endif /////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 3 resource. // ///////////////////////////////////////////////////////////////////////////// #endif // not APSTUDIO_INVOKED
7/10
8/10
Ph lc
Pht sinh ng dng Demo dng dng project Win32 Application. (IDE: Visual C++ 6.0) To v t tn ng dng
9/10
10/10