Guest User

Untitled

a guest
Oct 18th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.17 KB | None | 0 0
  1. // Win32Project1.cpp : 定義應用程式的進入點。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Win32Project1.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8.  
  9. // 全域變數:
  10. HINSTANCE hInst;                                // 目前執行個體
  11. WCHAR szTitle[MAX_LOADSTRING];                  // 標題列文字
  12. WCHAR szWindowClass[MAX_LOADSTRING];            // 主視窗類別名稱
  13.  
  14. // 這個程式碼模組中所包含之函式的向前宣告:
  15. ATOM                MyRegisterClass(HINSTANCE hInstance);
  16. BOOL                InitInstance(HINSTANCE, int);
  17. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  18. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  19.  
  20. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  21.                      _In_opt_ HINSTANCE hPrevInstance,
  22.                      _In_ LPWSTR    lpCmdLine,
  23.                      _In_ int       nCmdShow)
  24. {
  25.     UNREFERENCED_PARAMETER(hPrevInstance);
  26.     UNREFERENCED_PARAMETER(lpCmdLine);
  27.  
  28.     // TODO: 在此置入程式碼。
  29.  
  30.     // 初始化全域字串
  31.     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  32.     LoadStringW(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
  33.     MyRegisterClass(hInstance);
  34.  
  35.     // 執行應用程式初始設定:
  36.     if (!InitInstance (hInstance, nCmdShow))
  37.     {
  38.         return FALSE;
  39.     }
  40.  
  41.     HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
  42.  
  43.     MSG msg;
  44.  
  45.     // 主訊息迴圈:
  46.     while (GetMessage(&msg, nullptr, 0, 0))
  47.     {
  48.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  49.         {
  50.             TranslateMessage(&msg);
  51.             DispatchMessage(&msg);
  52.         }
  53.     }
  54.  
  55.     return (int) msg.wParam;
  56. }
  57.  
  58.  
  59.  
  60. //
  61. //  函式: MyRegisterClass()
  62. //
  63. //  用途: 註冊視窗類別。
  64. //
  65. ATOM MyRegisterClass(HINSTANCE hInstance)
  66. {
  67.     WNDCLASSEXW wcex;
  68.  
  69.     wcex.cbSize = sizeof(WNDCLASSEX);
  70.  
  71.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  72.     wcex.lpfnWndProc    = WndProc;
  73.     wcex.cbClsExtra     = 0;
  74.     wcex.cbWndExtra     = 0;
  75.     wcex.hInstance      = hInstance;
  76.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
  77.     wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
  78.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  79.     wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_WIN32PROJECT1);
  80.     wcex.lpszClassName  = szWindowClass;
  81.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  82.  
  83.     return RegisterClassExW(&wcex);
  84. }
  85.  
  86. //
  87. //   函式: InitInstance(HINSTANCE, int)
  88. //
  89. //   用途: 儲存執行個體控制代碼並且建立主視窗
  90. //
  91. //   註解:
  92. //
  93. //        在這個函式中,我們會將執行個體控制代碼儲存在全域變數中,
  94. //        並且建立和顯示主程式視窗。
  95. //
  96. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  97. {
  98.    hInst = hInstance; // 將執行個體控制代碼儲存在全域變數中
  99.  
  100.    HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  101.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
  102.  
  103.    if (!hWnd)
  104.    {
  105.       return FALSE;
  106.    }
  107.  
  108.    ShowWindow(hWnd, nCmdShow);
  109.    UpdateWindow(hWnd);
  110.  
  111.    return TRUE;
  112. }
  113.  
  114. //
  115. //  函式: WndProc(HWND, UINT, WPARAM, LPARAM)
  116. //
  117. //  用途:     處理主視窗的訊息。
  118. //
  119. //  WM_COMMAND  - 處理應用程式功能表
  120. //  WM_PAINT    - 繪製主視窗
  121. //  WM_DESTROY  - 顯示結束訊息然後返回
  122. //
  123. //
  124. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  125. {
  126.     switch (message)
  127.     {
  128.     case WM_COMMAND:
  129.         {
  130.             int wmId = LOWORD(wParam);
  131.             // 剖析功能表選取項目:
  132.             switch (wmId)
  133.             {
  134.             case IDM_ABOUT:
  135.                 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  136.                 break;
  137.             case IDM_EXIT:
  138.                 DestroyWindow(hWnd);
  139.                 break;
  140.             default:
  141.                 return DefWindowProc(hWnd, message, wParam, lParam);
  142.             }
  143.         }
  144.         break;
  145.     case WM_PAINT:
  146.         {
  147.             PAINTSTRUCT ps;
  148.             RECT rect;
  149.             HDC hdc = BeginPaint(hWnd, &ps);  //this will return display device id
  150.             // TODO: 在此加入任何使用 hdc 的繪圖程式碼...
  151.             GetClientRect(hWnd, &rect);
  152.            
  153.             rect.left = 40;
  154.             rect.top = 10;
  155.             DrawText(hdc, TEXT("hello\nworld"), -1, &rect, DT_SINGLELINE | DT_NOCLIP);
  156.  
  157.             //TextOut(hdc, 10, 30, TEXT("123456"), 6);
  158.             EndPaint(hWnd, &ps);
  159.         }
  160.         break;
  161.     case WM_DESTROY:
  162.         PostQuitMessage(0);
  163.         break;
  164.     default:
  165.         return DefWindowProc(hWnd, message, wParam, lParam);
  166.     }
  167.     return 0;
  168. }
  169.  
  170. // [關於] 方塊的訊息處理常式。
  171. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  172. {
  173.     UNREFERENCED_PARAMETER(lParam);
  174.     switch (message)
  175.     {
  176.     case WM_INITDIALOG:
  177.         return (INT_PTR)TRUE;
  178.  
  179.     case WM_COMMAND:
  180.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  181.         {
  182.             EndDialog(hDlg, LOWORD(wParam));
  183.             return (INT_PTR)TRUE;
  184.         }
  185.         break;
  186.     }
  187.     return (INT_PTR)FALSE;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment