Advertisement
Deth12

Untitled

Oct 7th, 2019
1,415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2.  
  3. const wchar_t text[50] = L"Enter text and click the button.";
  4.  
  5. static HWND textField;
  6. static HWND button;
  7. static HWND field1;
  8. static HWND field2;
  9.  
  10. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  11.  
  12. int CALLBACK wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR szCmdLine, int nCmdShow)
  13. {
  14.     // структура, содержащая информацию о сообщениях
  15.     MSG msg{};
  16.     // структура, в которой хранитс¤ дескриптор окна (указатель на область пам¤ти в ¤дре)
  17.     HWND hwnd{};
  18.     // структура, отвечающа¤ за параметры окна
  19.     WNDCLASSEX wc{ sizeof(WNDCLASSEX) };
  20.     // параметры окна
  21.     wc.hbrBackground = CreateSolidBrush(RGB(240, 240, 240));
  22.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  23.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  24.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  25.     wc.hInstance = hInstance;
  26.     wc.lpfnWndProc = WndProc;
  27.     wc.lpszClassName = L"MyClass";
  28.     wc.style = CS_VREDRAW | CS_HREDRAW;
  29.     // проверка регистрации окна
  30.     if (!RegisterClassEx(&wc))
  31.         return EXIT_FAILURE;
  32.     // создание окна
  33.     if (hwnd = CreateWindowW(wc.lpszClassName, L"Lab_1", WS_OVERLAPPEDWINDOW, 0, 0, 500, 200, NULL, NULL, wc.hInstance, NULL); hwnd == INVALID_HANDLE_VALUE)
  34.         return EXIT_FAILURE;
  35.     ShowWindow(hwnd, nCmdShow);
  36.     UpdateWindow(hwnd);
  37.     // цикл сообщений
  38.     while (GetMessage(&msg, NULL, 0, 0))
  39.     {
  40.         TranslateMessage(&msg);
  41.         DispatchMessage(&msg);
  42.     }
  43.     return EXIT_SUCCESS;
  44. }
  45.  
  46. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  47. {
  48.     switch (uMsg)
  49.     {
  50.     case WM_DESTROY:
  51.         PostQuitMessage(EXIT_SUCCESS);
  52.         break;
  53.     case WM_CREATE:
  54.         textField = CreateWindowW(L"STATIC", text, WS_CHILD | WS_VISIBLE | SS_CENTER, 50, 20, 350, 70, hWnd, NULL, NULL, NULL);
  55.         button = CreateWindowW(L"BUTTON", L"Click!", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 180, 40, 100, 20, hWnd, (HMENU) 1, NULL, NULL);
  56.         field1 = CreateWindowW(L"EDIT", L"", WS_BORDER | WS_CHILD | WS_VISIBLE | !ES_READONLY, 50, 70, 350, 20, hWnd, NULL, NULL, NULL);
  57.         field2 = CreateWindowW(L"EDIT", L"", WS_BORDER | WS_CHILD | WS_VISIBLE | ES_READONLY, 50, 100, 350, 20, hWnd, NULL, NULL, NULL);
  58.         break;
  59.     case WM_COMMAND:
  60.         switch (LOWORD(wParam))
  61.         {
  62.         case 1:
  63.             WCHAR* buffer = new WCHAR[150];
  64.             GetWindowText(field1, buffer, 150);
  65.             // изменяет название окна
  66.             SetWindowText(hWnd, buffer);
  67.             // изменяет значение поля field2
  68.             SetWindowText(field2, buffer);
  69.             break;
  70.         }
  71.         break;
  72.     }
  73.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement