Guest User

Hello Windows!

a guest
Mar 11th, 2021
4,933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | None | 0 0
  1. //
  2. // HelloWindows.cpp : Defines the entry point for the application.
  3. //
  4.  
  5. #include "framework.h"
  6. #include "HelloWindows.h"
  7.  
  8. #define MAX_LOADSTRING 100
  9.  
  10. // Global Variables:
  11. HINSTANCE hInst;                                // current instance
  12. WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
  13. WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
  14.  
  15. // Forward declarations of functions included in this code module:
  16. ATOM                MyRegisterClass(HINSTANCE hInstance);
  17. BOOL                InitInstance(HINSTANCE, int);
  18. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  19. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  20.  
  21. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  22.                      _In_opt_ HINSTANCE hPrevInstance,
  23.                      _In_ LPWSTR    lpCmdLine,
  24.                      _In_ int       nCmdShow)
  25. {
  26.     UNREFERENCED_PARAMETER(hPrevInstance);
  27.     UNREFERENCED_PARAMETER(lpCmdLine);
  28.  
  29.     // TODO: Place code here.
  30.  
  31.     // Initialize global strings
  32.     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  33.     LoadStringW(hInstance, IDC_HELLOWINDOWS, szWindowClass, MAX_LOADSTRING);
  34.     MyRegisterClass(hInstance);
  35.  
  36.     // Perform application initialization:
  37.     if (!InitInstance (hInstance, nCmdShow))
  38.  
  39.     {
  40.         return FALSE;
  41.     }
  42.  
  43.     HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_HELLOWINDOWS));
  44.  
  45.     MSG msg;
  46.     return 0;
  47.  
  48.     // Main message loop:
  49.     while (GetMessage(&msg, nullptr, 0, 0))
  50.     {
  51.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  52.         {
  53.             TranslateMessage(&msg);
  54.             DispatchMessage(&msg);
  55.         }
  56.     }
  57.  
  58.     return (int) msg.wParam;
  59. }
  60.  
  61.  
  62.  
  63. //
  64. //  FUNCTION: MyRegisterClass()
  65. //
  66. //  PURPOSE: Registers the window class.
  67. //
  68. ATOM MyRegisterClass(HINSTANCE hInstance)
  69. {
  70.     WNDCLASSEXW wcex;
  71.  
  72.     wcex.cbSize = sizeof(WNDCLASSEX);
  73.  
  74.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  75.     wcex.lpfnWndProc    = WndProc;
  76.     wcex.cbClsExtra     = 0;
  77.     wcex.cbWndExtra     = 0;
  78.     wcex.hInstance      = hInstance;
  79.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_HELLOWINDOWS));
  80.     wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
  81.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  82.     wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_HELLOWINDOWS);
  83.     wcex.lpszClassName  = szWindowClass;
  84.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  85.  
  86.     return RegisterClassExW(&wcex);
  87. }
  88.  
  89. //
  90. //   FUNCTION: InitInstance(HINSTANCE, int)
  91. //
  92. //   PURPOSE: Saves instance handle and creates main window
  93. //
  94. //   COMMENTS:
  95. //
  96. //        In this function, we save the instance handle in a global variable and
  97. //        create and display the main program window.
  98. //
  99. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  100. {
  101.    hInst = hInstance; // Store instance handle in our global variable
  102.  
  103.    HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  104.       CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, nullptr, nullptr, hInstance, nullptr);
  105.  
  106.    if (!hWnd)
  107.    {
  108.       return FALSE;
  109.    }
  110.  
  111.    ShowWindow(hWnd, nCmdShow);
  112.    UpdateWindow(hWnd);
  113.  
  114.    return TRUE;
  115. }
  116.  
  117. //
  118. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  119. //
  120. //  PURPOSE: Processes messages for the main window.
  121. //
  122. //  WM_COMMAND  - process the application menu
  123. //  WM_PAINT    - Paint the main window
  124. //  WM_DESTROY  - post a quit message and return
  125. //
  126. //
  127. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  128. {
  129.     switch (message)
  130.     {
  131.         case WM_COMMAND:
  132.         {
  133.             int wmId = LOWORD(wParam);
  134.             // Parse the menu selections:
  135.             switch (wmId)
  136.             {
  137.             case IDM_ABOUT:
  138.                 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  139.                 break;
  140.             case IDM_EXIT:
  141.                 DestroyWindow(hWnd);
  142.                 break;
  143.             default:
  144.                 return DefWindowProc(hWnd, message, wParam, lParam);
  145.             }
  146.         }
  147.         break;
  148.  
  149.         case WM_PAINT:
  150.         {
  151.             PAINTSTRUCT ps;
  152.             RECT rect;
  153.             HDC hdc = BeginPaint(hWnd, &ps);
  154.  
  155.             GetClientRect(hWnd, &rect);
  156.             DrawText(hdc, TEXT("Hello, Windows!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  157.             EndPaint(hWnd, &ps);
  158.         }
  159.         break;
  160.  
  161.         case WM_DESTROY:
  162.             PostQuitMessage(0);
  163.         break;
  164.  
  165.         default:
  166.             return DefWindowProc(hWnd, message, wParam, lParam);
  167.     }
  168.     return 0;
  169. }
  170.  
  171. // Message handler for about box.
  172. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  173. {
  174.     UNREFERENCED_PARAMETER(lParam);
  175.     switch (message)
  176.     {
  177.         case WM_INITDIALOG:
  178.             return (INT_PTR)TRUE;
  179.  
  180.         case WM_COMMAND:
  181.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  182.             {
  183.                 EndDialog(hDlg, LOWORD(wParam));
  184.                 return (INT_PTR)TRUE;
  185.             }
  186.             break;
  187.     }
  188.     return (INT_PTR)FALSE;
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment