Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <tchar.h>
  2. #include <Windows.h>
  3.  
  4. #pragma comment(linker, "\"/manifestdependency:type='win32' \
  5. name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' \
  6. publicKeyToken='6595b64144ccf1df' language='*'\"")
  7.  
  8.  
  9. LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
  10. void CALLBACK Move(HWND hWnd, int dx, int dy, bool center = false);
  11. INT WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR szCmdLine, INT nCmdShow)
  12. {
  13. TCHAR szClassWindow[] = TEXT("Win32Application");
  14.  
  15. WNDCLASSEX wc = {};
  16. wc.cbClsExtra = 0;
  17. wc.cbSize = sizeof(wc);
  18. wc.cbWndExtra = 0;
  19. wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  20. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  21. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  22. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  23. wc.hInstance = hInstance;
  24. wc.lpfnWndProc = WindowProcedure;
  25. wc.lpszClassName = szClassWindow;
  26. wc.lpszMenuName = NULL;
  27. wc.style = CS_HREDRAW | CS_VREDRAW;
  28.  
  29. INT nExitCode = 0;
  30.  
  31. if (RegisterClassEx(&wc))
  32. {
  33. HWND hWindow = CreateWindowEx(0, szClassWindow, TEXT("Поиграй со мной"),
  34. WS_OVERLAPPEDWINDOW, 250, 250, 200, 200, NULL,
  35. NULL, hInstance, NULL);
  36.  
  37. ShowWindow(hWindow, nCmdShow);
  38. UpdateWindow(hWindow);
  39.  
  40.  
  41. MSG message = {};
  42.  
  43. while (GetMessage(&message, NULL, 0, 0))
  44. {
  45. TranslateMessage(&message);
  46. DispatchMessage(&message);
  47. }
  48.  
  49. nExitCode = message.wParam;
  50. }
  51.  
  52. return nExitCode;
  53. }
  54.  
  55.  
  56. LRESULT CALLBACK WindowProcedure(HWND hWindow, UINT nMessage, WPARAM wParam, LPARAM lParam)
  57. {
  58. LRESULT result = 0;
  59.  
  60. switch (nMessage)
  61. {
  62. case WM_CHAR:
  63.  
  64. break;
  65. case WM_KEYDOWN:
  66. if (wParam == VK_ESCAPE)
  67. {
  68. ShowWindow(hWindow, SW_NORMAL);
  69. SetWindowPos(hWindow, NULL, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, 1);
  70. }
  71. else if (wParam == VK_LEFT)
  72. Move(hWindow, -50, 0);
  73. else if (wParam==VK_RIGHT)
  74. Move(hWindow, 50, 0);
  75. else if (wParam == VK_UP)
  76. Move(hWindow, 0, -50);
  77. else if (wParam == VK_DOWN)
  78. Move(hWindow, 0, 50);
  79. else if (wParam == VK_RETURN)
  80. ShowWindow(hWindow, SW_SHOWMAXIMIZED);
  81. else if (wParam == VK_SPACE)
  82. {
  83. Move(hWindow, 0, 0, true);
  84. }
  85.  
  86. break;
  87. case WM_DESTROY:
  88. PostQuitMessage(0);
  89. break;
  90.  
  91. default:
  92. result = DefWindowProc(hWindow, nMessage, wParam, lParam);
  93. break;
  94. }
  95.  
  96. return result;
  97. }
  98. void CALLBACK Move(HWND hWnd, int dx, int dy, bool center)
  99. {
  100. int width = GetSystemMetrics(SM_CXSCREEN); // граница ширины экрана
  101. int height = GetSystemMetrics(SM_CYSCREEN); // граница высоты экрана
  102. RECT rect;
  103. GetWindowRect(hWnd, &rect);
  104. if (center == false)
  105. {
  106. if (rect.top <= 0 && dy < 0) dy = 0;
  107. else if (rect.bottom >= height&&dy>0)dy = 0;
  108. if (rect.left <= 0 && dx < 0) dx = 0;
  109. else if (rect.right >= width&&dx>0)dx = 0;
  110. dx += rect.left;
  111. dy += rect.top;
  112. SetWindowPos(hWnd, NULL, dx, dy, CW_USEDEFAULT, CW_USEDEFAULT, 1);
  113. }
  114. else
  115. SetWindowPos(hWnd, NULL, ((width/2)-((rect.right-rect.left))/2), ((height/2)-((rect.bottom-rect.top))/2), CW_USEDEFAULT, CW_USEDEFAULT, 1);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement