Advertisement
Guest User

Untitled

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