Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.15 KB | None | 0 0
  1. // WinApiLAB2.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "WinApiLAB2.h"
  6. #define w_width 400
  7. #define w_height 600
  8. #define MAX_LOADSTRING 100
  9. const int ScreenX = (GetSystemMetrics(SM_CXSCREEN) - w_width) / 2; //defines positions such that window will be in the middle
  10. const int ScreenY = (GetSystemMetrics(SM_CYSCREEN) - w_height) / 2;
  11. // Global Variables:
  12. HINSTANCE hInst; // current instance
  13. WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  14. WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
  15. HWND hMainWnd;
  16. HWND hBlockWnd;
  17. void UpdateBlock();
  18. struct Block {
  19. HWND BlockWnd;
  20. int size = 32;
  21. int x = 350;
  22. int y = 140;
  23. };
  24. Block block;
  25. // Forward declarations of functions included in this code module:
  26. ATOM MyRegisterClass(HINSTANCE hInstance);
  27. ATOM MyRegisterBlockClass(HINSTANCE hInstance);
  28. BOOL InitInstance(HINSTANCE, int);
  29. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  30. LRESULT CALLBACK WndProcBlock(HWND, UINT, WPARAM, LPARAM);
  31. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  32.  
  33. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  34. _In_opt_ HINSTANCE hPrevInstance,
  35. _In_ LPWSTR lpCmdLine,
  36. _In_ int nCmdShow)
  37. {
  38. UNREFERENCED_PARAMETER(hPrevInstance);
  39. UNREFERENCED_PARAMETER(lpCmdLine);
  40.  
  41.  
  42. // TODO: Place code here.
  43.  
  44. // Initialize global strings
  45. LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  46. LoadStringW(hInstance, IDC_WINAPILAB2, szWindowClass, MAX_LOADSTRING);
  47. MyRegisterClass(hInstance);
  48. MyRegisterBlockClass(hInstance);
  49. // Perform application initialization:
  50. if (!InitInstance (hInstance, nCmdShow))
  51. {
  52. return FALSE;
  53. }
  54.  
  55. HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINAPILAB2));
  56.  
  57. MSG msg;
  58.  
  59. // Main message loop:
  60. while (GetMessage(&msg, nullptr, 0, 0))
  61. {
  62. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  63. {
  64. TranslateMessage(&msg);
  65. DispatchMessage(&msg);
  66. }
  67. }
  68.  
  69. return (int) msg.wParam;
  70. }
  71.  
  72.  
  73.  
  74. //
  75. // FUNCTION: MyRegisterClass()
  76. //
  77. // PURPOSE: Registers the window class.
  78. //
  79. ATOM MyRegisterClass(HINSTANCE hInstance)
  80. {
  81. WNDCLASSEXW wcex;
  82.  
  83. wcex.cbSize = sizeof(WNDCLASSEX);
  84.  
  85. wcex.style = CS_HREDRAW | CS_VREDRAW;
  86. wcex.lpfnWndProc = WndProc;
  87. wcex.cbClsExtra = 0;
  88. wcex.cbWndExtra = 0;
  89. wcex.hInstance = hInstance;
  90. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINAPILAB2));
  91. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  92. HBRUSH hb = CreateSolidBrush(RGB(0, 128, 255));
  93. wcex.hbrBackground = hb;
  94. wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINAPILAB2);
  95. wcex.lpszClassName = szWindowClass;
  96. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  97.  
  98. return RegisterClassExW(&wcex);
  99. }
  100. ATOM MyRegisterBlockClass(HINSTANCE hInstance)
  101. {
  102. WNDCLASSEXW wcex;
  103.  
  104. wcex.cbSize = sizeof(WNDCLASSEX);
  105.  
  106. wcex.style = CS_HREDRAW | CS_VREDRAW;
  107. wcex.lpfnWndProc = WndProcBlock;
  108. wcex.cbClsExtra = 0;
  109. wcex.cbWndExtra = 0;
  110. wcex.hInstance = hInstance;
  111. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINAPILAB2));
  112. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  113. HBRUSH hb = CreateSolidBrush(RGB(255, 0, 0));
  114. wcex.hbrBackground = hb;
  115. wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINAPILAB2);
  116. wcex.lpszClassName = L"BlockWnd";
  117. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  118.  
  119. return RegisterClassExW(&wcex);
  120. }
  121. //
  122. // FUNCTION: InitInstance(HINSTANCE, int)
  123. //
  124. // PURPOSE: Saves instance handle and creates main window
  125. //
  126. // COMMENTS:
  127. //
  128. // In this function, we save the instance handle in a global variable and
  129. // create and display the main program window.
  130. //
  131. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  132. {
  133. hInst = hInstance; // Store instance handle in our global variable
  134.  
  135. hMainWnd = CreateWindowW(szWindowClass, L"IcyTower", WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^ WS_MAXIMIZEBOX | WS_CLIPCHILDREN, //WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
  136. ScreenX, ScreenY, w_width, w_height, nullptr, nullptr, hInstance, nullptr);
  137. SetWindowPos(hMainWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  138. if (!hMainWnd)
  139. {
  140. return FALSE;
  141. }
  142. ShowWindow(hMainWnd, nCmdShow);
  143. UpdateWindow(hMainWnd);
  144. hBlockWnd = CreateWindow(L"BlockWnd", L"Block",
  145. WS_CHILD,
  146. 180,510, 820, 40,
  147. hMainWnd, NULL, hInstance, NULL);
  148. if (!hBlockWnd)
  149. {
  150. return FALSE;
  151. }
  152. SetWindowRgn(hBlockWnd, CreateRectRgn(0, 0, block.size, block.size), true);
  153.  
  154. ShowWindow(hBlockWnd, nCmdShow);
  155. UpdateWindow(hBlockWnd);
  156. return TRUE;
  157. }
  158.  
  159. //
  160. // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  161. //
  162. // PURPOSE: Processes messages for the main window.
  163. //
  164. // WM_COMMAND - process the application menu
  165. // WM_PAINT - Paint the main window
  166. // WM_DESTROY - post a quit message and return
  167. //
  168. //
  169. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  170. {
  171. switch (message)
  172. {
  173. case WM_COMMAND:
  174. {
  175. int wmId = LOWORD(wParam);
  176. // Parse the menu selections:
  177. switch (wmId)
  178. {
  179. case IDM_ABOUT:
  180. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  181. break;
  182. case IDM_EXIT:
  183. DestroyWindow(hWnd);
  184. break;
  185. default:
  186. return DefWindowProc(hWnd, message, wParam, lParam);
  187. }
  188. }
  189. break;
  190. case WM_SETFOCUS: {SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) |
  191. WS_EX_LAYERED);
  192. // Make this window 50% alpha
  193. SetLayeredWindowAttributes(hWnd, 0, (500 * 50) / 100, LWA_ALPHA); }break;
  194. case WM_KILLFOCUS: { SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) |
  195. WS_EX_LAYERED);
  196. // Make this window 50% alpha
  197. SetLayeredWindowAttributes(hWnd, 0, (255* 50) / 100, LWA_ALPHA); }break;
  198. case WM_PAINT:
  199. {
  200. PAINTSTRUCT ps;
  201. HDC hdc = BeginPaint(hWnd, &ps);
  202. // TODO: Add any drawing code that uses hdc here...
  203. EndPaint(hWnd, &ps);
  204. }
  205. break;
  206. case WM_KEYDOWN: {
  207. if (wParam == 65) // A
  208. {
  209. block.x -= 20;
  210. UpdateBlock();
  211. }
  212. if (wParam == 83)
  213. {
  214. block.y += 20;
  215. UpdateBlock();
  216. }
  217. if (wParam == 68)
  218. {
  219. block.x += 20;
  220. UpdateBlock();
  221. }
  222. if (wParam == 68) // D
  223. {
  224. block.x += 20;
  225. UpdateBlock();
  226. }
  227. }break;
  228. case WM_DESTROY:
  229. PostQuitMessage(0);
  230. break;
  231. default:
  232. return DefWindowProc(hWnd, message, wParam, lParam);
  233. }
  234. return 0;
  235. }
  236.  
  237. // Message handler for about box.
  238. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  239. {
  240. UNREFERENCED_PARAMETER(lParam);
  241. switch (message)
  242. {
  243. case WM_INITDIALOG:
  244. return (INT_PTR)TRUE;
  245.  
  246. case WM_COMMAND:
  247. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  248. {
  249. EndDialog(hDlg, LOWORD(wParam));
  250. return (INT_PTR)TRUE;
  251. }
  252. break;
  253. }
  254. return (INT_PTR)FALSE;
  255. }
  256. LRESULT CALLBACK WndProcBlock(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  257. {
  258. RECT rc;
  259. static int X, Y;
  260. switch (message)
  261. {
  262.  
  263. case WM_PAINT:
  264. {
  265. PAINTSTRUCT ps;
  266. HDC hdc = BeginPaint(hWnd, &ps);
  267.  
  268.  
  269. EndPaint(hWnd, &ps);
  270. }
  271. break;
  272. case WM_DESTROY:
  273. PostQuitMessage(0);
  274. break;
  275. default:
  276. return DefWindowProc(hWnd, message, wParam, lParam);
  277. }
  278. return 0;
  279. return 0;
  280. }
  281.  
  282. void UpdateBlock() {
  283. if (block.x < 0)
  284. block.x = 0;
  285. if (block.x > 800 - 16 - block.size)
  286. block.x = 800 - 16 - block.size;
  287. if (block.y < 0)
  288. block.y = 0;
  289. if (block.y > 600 - 77 - block.size)
  290. block.y = 600 - 77 - block.size;
  291.  
  292. MoveWindow(hBlockWnd, block.x, block.y, block.size, block.size, true);
  293. UpdateWindow(hBlockWnd);
  294. SetLayeredWindowAttributes(hMainWnd, 0, (255 * 100) / 100, LWA_ALPHA);
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement