You are on page 1of 10

V D MINH HO S DNG WIN 32 API

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).

Vn Ch Nam Khoa CNTT H KHTN TP.HCM

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"

Khai bo hm v khai bo bin ton cc


#define MAX_LOADSTRING 100 // Khai bo bin ton cc: HINSTANCE hInst; // Instance hin hnh TCHAR szTitle[MAX_LOADSTRING]; // Tiu ca s TCHAR szWindowClass[MAX_LOADSTRING];// Tn lp // Khai bo prototye cho cc hm: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

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; }

File Win32Demo.rc File m t ti nguyn ca ng dng


//Microsoft Visual C++ generated resource script. // #include "resource.h" #define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 2 resource. // #define APSTUDIO_HIDDEN_SYMBOLS #include "windows.h" #undef APSTUDIO_HIDDEN_SYMBOLS ///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS

Vn Ch Nam Khoa CNTT H KHTN TP.HCM

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

IDI_WIN32DEMO,IDC_MYICON,14,9,16,16 "Win32Demo Version 1.0",IDC_STATIC,49,10,119,8,SS_NOPREFIX "Copyright (C) 2006",IDC_STATIC,49,20,119,8 "OK",IDOK,195,6,30,11,WS_GROUP

#ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// Vn Ch Nam Khoa CNTT H KHTN TP.HCM 6/10

// // 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

Vn Ch Nam Khoa CNTT H KHTN TP.HCM

7/10

File resource.h Cc gi tr ID c nh ngha trong file ny.


//{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. // Used by WIN32DEMO.RC // #define IDR_MAINFRAME 128 #define IDD_WIN32DEMO_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_WIN32DEMO 107 #define IDI_SMALL 108 #define IDC_WIN32DEMO 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 129 32771 1000 110

Vn Ch Nam Khoa CNTT H KHTN TP.HCM

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

Chn loi project (A typical Hello World application)

Vn Ch Nam Khoa CNTT H KHTN TP.HCM

9/10

Mn hnh chnh sau khi ng dng c pht sinh

Bin dch v thc thi ng dng

Vn Ch Nam Khoa CNTT H KHTN TP.HCM

10/10

You might also like