|
|
- //: 這裡我為大家加上了中文註解,比較容易讀懂程式碼。
- #include "stdafx.h"
- #include "HelloWin32SDK.h"
- #define MAX_LOADSTRING 100
- // 全域變數
- HINSTANCE hInst; // 視窗實體變數
- TCHAR szTitle[MAX_LOADSTRING]; // 視窗標題列
- TCHAR szWindowClass[MAX_LOADSTRING]; // 主視窗類別
- // 向前宣告
- ATOM MyRegisterClass(HINSTANCE hInstance); //註冊視窗
- BOOL InitInstance(HINSTANCE, int); //初始化變數群
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //視窗訊息處理函示
- LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); //「關於」視窗的處理函示
- /*視窗程式的進入點,舊版的SDK寫法為:
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {...}
- */
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- MSG msg;
- HACCEL hAccelTable;
- // 初始化全域變數(字串)
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_HELLOWIN32SDK, szWindowClass, MAX_LOADSTRING);
- // 註冊視窗
- MyRegisterClass(hInstance);
- // 處理應用程式的初始化
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_HELLOWIN32SDK);
- // 訊息迴圈
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
- //
- // 函數: MyRegisterClass()
- //
- // 目的: 註冊視窗類別
- //
- // COMMENTS:
- //
- // This function and its usage are only necessary if you want this code
- // to be compatible with Win32 systems prior to the 'RegisterClassEx'
- // function that was added to Windows 95. It is important to call this function
- // so that the application will get 'well formed' small icons associated
- // with it.
- //
- // 這是新版的視窗類別結構 WNDCLASSEX ,這個結構只比舊的WNDCLASS多了兩個資料成員。
- // 一個為 cbSize 用來計算結構大小,另外一個 hIconSm 記錄視窗縮小化後的大小。
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
-
- //視窗樣式
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- //訊息處理函數
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- //額外要求註冊視窗所需要的記憶體(byte)
- wcex.cbClsExtra = 0;
- //額外要求生成視窗所需要的記憶體(byte)
- wcex.cbWndExtra = 0;
- //包含處理這個視窗類別的訊息處理程式的 HANDLE。
- wcex.hInstance = hInstance;
- //視窗圖示
- wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_HELLOWIN32SDK);
- //視窗中使用的游標
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- //視窗的背景
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- /*
- 背景可以是事先定義好的色彩常數。
- COLOR_ACTIVEBORDER
- COLOR_ACTIVECAPTION
- COLOR_APPWORKSPACE
- COLOR_BACKGROUND
- COLOR_BTNFACE
- COLOR_BTNSHADOW
- COLOR_BTNTEXT
- COLOR_CAPTIONTEXT
- COLOR_GRAYTEXT
- COLOR_HIGHLIGHT
- COLOR_HIGHLIGHTTEXT
- COLOR_INACTIVEBORDER
- COLOR_INACTIVECAPTION
- COLOR_MENU
- COLOR_MENUTEXT
- COLOR_SCROLLBAR
- COLOR_WINDOW
- COLOR_WINDOWFRAME
- COLOR_WINDOWTEXT
- */
- //功能表
- wcex.lpszMenuName = (LPCTSTR)IDC_HELLOWIN32SDK;
- //註冊視窗的字串
- wcex.lpszClassName = szWindowClass;
- //視窗最小化後的ICON
- wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
- //舊版的 WNDCLASS 需改用 RegisterClass 函數。
- return RegisterClassEx(&wcex);
- }
- //
- // FUNCTION: InitInstance(HANDLE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- // 記住視窗程式的記憶位置實體
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- // 將 Instance Handle 放置到全域變數
- hInst = hInstance;
- // 建立視窗
- 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;
- }
- //
- // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // WM_COMMAND - 處理視窗的功能表
- // WM_PAINT - 繪製視窗的訊息代號
- // WM_DESTROY - 結束視窗
- //
- // ☆這個函數是用來處理所有的視窗事件。(主視窗)
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
- switch (message)
- {
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- 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;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- // TODO: Add any drawing code here...
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // Message handler for about box.
- 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;
- }
複製代碼 |
|