Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Win32Project1.cpp : 定義應用程式的進入點。
- //
- #include "stdafx.h"
- #include "Win32Project1.h"
- #define MAX_LOADSTRING 100
- // 全域變數:
- HINSTANCE hInst; // 目前執行個體
- WCHAR szTitle[MAX_LOADSTRING]; // 標題列文字
- WCHAR szWindowClass[MAX_LOADSTRING]; // 主視窗類別名稱
- // 這個程式碼模組中所包含之函式的向前宣告:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // TODO: 在此置入程式碼。
- // 初始化全域字串
- LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadStringW(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // 執行應用程式初始設定:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
- MSG msg;
- // 主訊息迴圈:
- while (GetMessage(&msg, nullptr, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
- //
- // 函式: MyRegisterClass()
- //
- // 用途: 註冊視窗類別。
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEXW wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
- wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32PROJECT1);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassExW(&wcex);
- }
- //
- // 函式: InitInstance(HINSTANCE, int)
- //
- // 用途: 儲存執行個體控制代碼並且建立主視窗
- //
- // 註解:
- //
- // 在這個函式中,我們會將執行個體控制代碼儲存在全域變數中,
- // 並且建立和顯示主程式視窗。
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- hInst = hInstance; // 將執行個體控制代碼儲存在全域變數中
- HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // 函式: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // 用途: 處理主視窗的訊息。
- //
- // WM_COMMAND - 處理應用程式功能表
- // WM_PAINT - 繪製主視窗
- // WM_DESTROY - 顯示結束訊息然後返回
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_COMMAND:
- {
- int wmId = LOWORD(wParam);
- // 剖析功能表選取項目:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- }
- break;
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- RECT rect;
- HDC hdc = BeginPaint(hWnd, &ps); //this will return display device id
- // TODO: 在此加入任何使用 hdc 的繪圖程式碼...
- GetClientRect(hWnd, &rect);
- rect.left = 40;
- rect.top = 10;
- DrawText(hdc, TEXT("hello\nworld"), -1, &rect, DT_SINGLELINE | DT_NOCLIP);
- //TextOut(hdc, 10, 30, TEXT("123456"), 6);
- EndPaint(hWnd, &ps);
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // [關於] 方塊的訊息處理常式。
- INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(lParam);
- switch (message)
- {
- case WM_INITDIALOG:
- return (INT_PTR)TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return (INT_PTR)TRUE;
- }
- break;
- }
- return (INT_PTR)FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment