Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <string>
  4.  
  5. LRESULT CALLBACK WindowProcess(HWND, UINT, WPARAM, LPARAM);
  6.  
  7. int WINAPI WinMain(HINSTANCE hInst,
  8. HINSTANCE hPrevInst,
  9. LPSTR pCommandLine,
  10. int nCommandShow) {
  11. TCHAR className[] = L"Мой класс";
  12. HWND hWindow;
  13. MSG message;
  14. WNDCLASSEX windowClass;
  15.  
  16. windowClass.cbSize = sizeof(windowClass);
  17. windowClass.style = CS_HREDRAW | CS_VREDRAW;
  18. windowClass.lpfnWndProc = WindowProcess;
  19. windowClass.lpszMenuName = NULL;
  20. windowClass.lpszClassName = className;
  21. windowClass.cbWndExtra = NULL;
  22. windowClass.cbClsExtra = NULL;
  23. windowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  24. windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
  25. windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  26. windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  27. windowClass.hInstance = hInst;
  28.  
  29. if (!RegisterClassEx(&windowClass))
  30. {
  31. MessageBox(NULL, L"Не получилось зарегистрировать класс!", L"Ошибка", MB_OK);
  32. return NULL;
  33. }
  34. hWindow = CreateWindow(className,
  35. L"Программа ввода символов",
  36. WS_OVERLAPPEDWINDOW,
  37. CW_USEDEFAULT,
  38. NULL,
  39. CW_USEDEFAULT,
  40. NULL,
  41. (HWND)NULL,
  42. NULL,
  43. HINSTANCE(hInst),
  44. NULL
  45. );
  46. if (!hWindow) {
  47. MessageBox(NULL, L"Не получилось создать окно!", L"Ошибка", MB_OK);
  48. return NULL;
  49. }
  50. ShowWindow(hWindow, nCommandShow);
  51. UpdateWindow(hWindow);
  52. // Цикл обработки событий
  53. while (GetMessage(&message, NULL, NULL, NULL)) {
  54. TranslateMessage(&message);
  55. // Косвенно вызывает WindowProcess
  56. DispatchMessage(&message);
  57. }
  58. return message.wParam;
  59. }
  60.  
  61. // Строка глобальна, так как она должна сохраняться между вызовами WindowProcess, а мы не можем передавать туда аргументы.
  62. // В ней будем хранить выводимый в центре окна текст (нажатую клавишу и координаты мыши)
  63. static std::string text = " ";
  64.  
  65. LRESULT CALLBACK WindowProcess(HWND hWindow,
  66. UINT uMessage,
  67. WPARAM wParameter,
  68. LPARAM lParameter)
  69. {
  70. HDC hDeviceContext;
  71. PAINTSTRUCT paintStruct;
  72. RECT rectPlace;
  73. HFONT hFont;
  74. TRACKMOUSEEVENT tr;
  75. tr.cbSize = sizeof(TRACKMOUSEEVENT);
  76. tr.hwndTrack = hWindow;
  77. tr.dwHoverTime = HOVER_DEFAULT;
  78.  
  79.  
  80. switch (uMessage)
  81. {
  82. case WM_CREATE:
  83. MessageBox(NULL,
  84. L"Пожалуйста, вводите символы и они будут отображаться на экране!",
  85. L"ВНИМАНИЕ!!!", MB_ICONASTERISK | MB_OK);
  86. ///*TrackMouseEvent*/(&tr);
  87. break;
  88. case WM_PAINT: {
  89.  
  90. hDeviceContext = BeginPaint(hWindow, &paintStruct);
  91. GetClientRect(hWindow, &rectPlace);
  92. SetTextColor(hDeviceContext, NULL);
  93. hFont = CreateFont(90, 0, 0, 0, 0, 0, 0, 0,
  94. DEFAULT_CHARSET,
  95. 0, 0, 0, 0,
  96. L"Arial Bold"
  97. );
  98. SelectObject(hDeviceContext, hFont);
  99. OutputDebugStringW(L"Draw");
  100.  
  101. // Winapi выводит WCHAR* строки, производим преобразование из std::string
  102. // c_str - метод std::string, возвращающий эквивалетную C-строку.
  103. WCHAR wtext[30] = { 0 };
  104. mbstowcs_s(NULL, wtext, 30, text.c_str(), text.size());
  105.  
  106. DrawText(hDeviceContext, wtext, -1, &rectPlace, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  107. EndPaint(hWindow, &paintStruct);
  108. break;
  109. }
  110. case WM_MOUSEMOVE: {
  111. POINT cursor;
  112. //OutputDebugStringW(L"HOVER");
  113. GetCursorPos(&cursor);
  114. // Оставляем только первый символ строки
  115. text.resize(1);
  116. // Добавляем координаты
  117. text += '(';
  118. text += std::to_string(cursor.x);
  119. text += ',';
  120. text += std::to_string(cursor.y);
  121. text += ')';
  122.  
  123.  
  124. //int w = sprintf_s(text + 1, 20, "(%d,", cursor.x);
  125. //snprintf(text + 1 + w, 30, "%d)", cursor.y);
  126.  
  127. InvalidateRect(hWindow, NULL, TRUE);
  128. break;
  129. }
  130. case WM_KEYDOWN:
  131. switch (wParameter)
  132. {
  133. case VK_HOME:case VK_END:case VK_PRIOR:
  134. case VK_NEXT:case VK_LEFT:case VK_RIGHT:
  135. case VK_UP:case VK_DOWN:case VK_DELETE:
  136. case VK_SHIFT:case VK_SPACE:case VK_CONTROL:
  137. case VK_CAPITAL:case VK_MENU:case VK_TAB:
  138. case VK_BACK:case VK_RETURN:
  139. break;
  140. default:
  141. text[0] = (char)wParameter;
  142.  
  143. InvalidateRect(hWindow, NULL, TRUE);
  144. break;
  145. }
  146. break;
  147. case WM_DESTROY:
  148. PostQuitMessage(0);
  149. break;
  150. default:
  151. return DefWindowProc(hWindow, uMessage, wParameter, lParameter);
  152. }
  153. return NULL;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement