Advertisement
Laudanum-user

this code creates a window

Apr 17th, 2023
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.93 KB | None | 0 0
  1. // test4c.cpp : Defines the entry point for the application.
  2. //
  3. #define _CRT_SECURE_NO_WARNINGS
  4.  
  5. #include "framework.h"
  6. #include "test4c.h"
  7. #include <stdio.h>
  8.  
  9. #define MAX_LOADSTRING 100
  10. #define ID_EDIT1 1
  11. #define ID_EDIT2 2
  12. #define ID_BUTTON 3
  13.  
  14. // Global Variables:
  15. HINSTANCE hInst;                                // current instance
  16.  
  17. HWND hEditBox1;
  18. HWND hEditBox2;
  19. HWND hButton;
  20.  
  21. // Forward declarations of functions included in this code module:
  22. ATOM                MyRegisterClass(HINSTANCE hInstance);
  23. BOOL                InitInstance(HINSTANCE, int);
  24. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  25. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  26.  
  27. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  28.                      _In_opt_ HINSTANCE hPrevInstance,
  29.                      _In_ LPWSTR    lpCmdLine,
  30.                      _In_ int       nCmdShow)
  31. {
  32.     UNREFERENCED_PARAMETER(hPrevInstance);
  33.     UNREFERENCED_PARAMETER(lpCmdLine);
  34.  
  35.     // TODO: Place code here.
  36.  
  37.     // Initialize global strings
  38.     MyRegisterClass(hInstance);
  39.  
  40.     // Perform application initialization:
  41.     if (!InitInstance (hInstance, nCmdShow))
  42.     {
  43.         return FALSE;
  44.     }
  45.  
  46.  
  47.     MSG msg;
  48.  
  49.     // Main message loop:
  50.     while (GetMessage(&msg, nullptr, 0, 0))
  51.     {
  52.         if (!TranslateMessage(&msg))
  53.         {
  54.             TranslateMessage(&msg);
  55.             DispatchMessage(&msg);
  56.         }
  57.     }
  58.  
  59.     return (int) msg.wParam;
  60. }
  61.  
  62. //
  63. //  FUNCTION: MyRegisterClass()
  64. //
  65. //  PURPOSE: Registers the window class.
  66. //
  67. ATOM MyRegisterClass(HINSTANCE hInstance)
  68. {
  69.     WNDCLASSEXW wcex;
  70.  
  71.     wcex.cbSize = sizeof(WNDCLASSEX);
  72.  
  73.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  74.     wcex.lpfnWndProc    = WndProc;
  75.     wcex.cbClsExtra     = 0;
  76.     wcex.cbWndExtra     = 0;
  77.     wcex.hInstance      = hInstance;
  78.     wcex.hIcon          = LoadIcon(hInstance, IDI_WINLOGO);
  79.     wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
  80.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW);
  81.     wcex.lpszMenuName   = 0;
  82.     wcex.lpszClassName  = L"MYWINDOW";
  83.     wcex.hIconSm        = LoadIcon(hInstance, IDI_WINLOGO);
  84.  
  85.     return RegisterClassExW(&wcex);
  86. }
  87.  
  88. //
  89. //   FUNCTION: InitInstance(HINSTANCE, int)
  90. //
  91. //   PURPOSE: Saves instance handle and creates main window
  92. //
  93. //   COMMENTS:
  94. //
  95. //        In this function, we save the instance handle in a global variable and
  96. //        create and display the main program window.
  97. //
  98. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  99. {
  100.     hInst = hInstance; // Store instance handle in our global variable
  101.  
  102.     HWND hWnd = CreateWindowW(
  103.         L"MYWINDOW",
  104.         L"MYWINDOW",
  105.         WS_OVERLAPPEDWINDOW,
  106.         CW_USEDEFAULT, 0,
  107.         319, 85,
  108.         nullptr, nullptr,
  109.         hInstance,
  110.         nullptr
  111.     );
  112.  
  113.     if (!hWnd)
  114.     {
  115.         return FALSE;
  116.     }
  117.  
  118.     ShowWindow(hWnd, nCmdShow);
  119.     UpdateWindow(hWnd);
  120.  
  121.     //ShowWindow(hEditBox1, nCmdShow);
  122.  
  123.     return TRUE;
  124. }
  125.  
  126. //
  127. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  128. //
  129. //  PURPOSE: Processes messages for the main window.
  130. //
  131. //  WM_COMMAND  - process the application menu
  132. //  WM_PAINT    - Paint the main window
  133. //  WM_DESTROY  - post a quit message and return
  134. //
  135. //
  136. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  137. {
  138.     switch (message)
  139.     {
  140.     case WM_CREATE: {
  141.         // edit box 1
  142.         hEditBox1 = CreateWindowEx(
  143.             WS_EX_CLIENTEDGE,
  144.             L"EDIT",
  145.             NULL,
  146.             WS_BORDER | WS_VISIBLE | WS_CHILD | ES_LEFT,
  147.             12, 12,
  148.             89, 23,
  149.             hWnd,
  150.             (HMENU)ID_EDIT1,
  151.             NULL,
  152.             NULL
  153.         );
  154.  
  155.         // text
  156.         CreateWindow(
  157.             L"STATIC",
  158.             L"+",
  159.             WS_VISIBLE | WS_CHILD | SS_LEFT,
  160.             108, 15,
  161.             15, 15,
  162.             hWnd,
  163.             NULL,
  164.             NULL,
  165.             NULL
  166.         );
  167.  
  168.         // edit box 3
  169.         hEditBox2 = CreateWindowEx(
  170.             WS_EX_CLIENTEDGE,
  171.             L"EDIT",
  172.             NULL,
  173.             WS_BORDER | WS_VISIBLE | WS_CHILD | ES_LEFT,
  174.             125, 12,
  175.             89, 23,
  176.             hWnd,
  177.             (HMENU)ID_EDIT2,
  178.             NULL,
  179.             NULL
  180.         );
  181.  
  182.         // button
  183.         hButton = CreateWindow(
  184.             L"BUTTON",
  185.             L"=",
  186.             WS_VISIBLE | WS_CHILD,
  187.             220, 12,
  188.             75, 23,
  189.             hWnd,
  190.             (HMENU)ID_BUTTON,
  191.             NULL,
  192.             NULL
  193.         );
  194.  
  195.         break;
  196.     }
  197.     case WM_PAINT:
  198.         {
  199.             PAINTSTRUCT ps;
  200.             HDC hdc = BeginPaint(hWnd, &ps);
  201.             // TODO: Add any drawing code that uses hdc here...
  202.             EndPaint(hWnd, &ps);
  203.         }
  204.         break;
  205.     case WM_COMMAND: {
  206.         if (LOWORD(wParam) == ID_BUTTON && HIWORD(wParam) == BN_CLICKED) {
  207.             int len;
  208.             wchar_t* wtext;
  209.             char* text;
  210.  
  211.             len = GetWindowTextLengthW(hEditBox1);
  212.             wtext = new wchar_t[len + 2];
  213.             GetWindowTextW(hEditBox1, wtext, len + 1);
  214.             text = new char[len + 1];
  215.             sprintf(text, "%ws", wtext);
  216.             int number1 = atoi(text);
  217.             delete[] wtext;
  218.             delete[] text;
  219.  
  220.             len = GetWindowTextLengthW(hEditBox2);
  221.             wtext = new wchar_t[len + 2];
  222.             GetWindowTextW(hEditBox2, wtext, len + 1);
  223.             text = new char[len + 1];
  224.             sprintf(text, "%ws", wtext);
  225.             int number2 = atoi(text);
  226.             delete[] wtext;
  227.             delete[] text;
  228.  
  229.             char buf[256];
  230.             sprintf(buf, "%d", number1 + number2);
  231.             MessageBoxA(hWnd, buf, "Hi :)", MB_OK);
  232.         }
  233.         break;
  234.     }
  235.     case WM_DESTROY:
  236.         PostQuitMessage(0);
  237.         break;
  238.     default:
  239.         return DefWindowProc(hWnd, message, wParam, lParam);
  240.     }
  241.     return 0;
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement