Advertisement
Guest User

Untitled

a guest
May 20th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // WinAPI.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "WinAPI.h"
  6. #include <string>
  7. #include <sstream>
  8.  
  9. #define MAX_LOADSTRING 100
  10.  
  11. using namespace std;
  12.  
  13. #ifdef UNICODE
  14. typedef std::wostringstream tstringstream;
  15. #else
  16. typedef std::ostringstream tstringstream;
  17. #endif
  18.  
  19. // Global Variables:
  20. HINSTANCE hInst;                                // current instance
  21. WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
  22. WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
  23.  
  24.                                                 // Forward declarations of functions included in this code module:
  25. ATOM                MyRegisterClass(HINSTANCE hInstance);
  26. BOOL                InitInstance(HINSTANCE, int);
  27. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  28. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  29.  
  30. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  31.     _In_opt_ HINSTANCE hPrevInstance,
  32.     _In_ LPWSTR    lpCmdLine,
  33.     _In_ int       nCmdShow)
  34. {
  35.     UNREFERENCED_PARAMETER(hPrevInstance);
  36.     UNREFERENCED_PARAMETER(lpCmdLine);
  37.  
  38.     // TODO: Place code here.
  39.  
  40.  
  41.     // Initialize global strings
  42.     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  43.     LoadStringW(hInstance, IDC_WINAPI, szWindowClass, MAX_LOADSTRING);
  44.     MyRegisterClass(hInstance);
  45.  
  46.     // Perform application initialization:
  47.     if (!InitInstance(hInstance, nCmdShow))
  48.     {
  49.         return FALSE;
  50.     }
  51.  
  52.     HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINAPI));
  53.  
  54.     MSG msg;
  55.  
  56.     // Main message loop:
  57.     while (GetMessage(&msg, nullptr, 0, 0))
  58.     {
  59.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  60.         {
  61.             TranslateMessage(&msg);
  62.             DispatchMessage(&msg);
  63.         }
  64.     }
  65.  
  66.     return (int)msg.wParam;
  67. }
  68.  
  69.  
  70.  
  71. //
  72. //  FUNCTION: MyRegisterClass()
  73. //
  74. //  PURPOSE: Registers the window class.
  75. //
  76. ATOM MyRegisterClass(HINSTANCE hInstance)
  77. {
  78.     WNDCLASSEXW wcex;
  79.  
  80.     wcex.cbSize = sizeof(WNDCLASSEX);
  81.  
  82.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  83.     wcex.lpfnWndProc = WndProc;
  84.     wcex.cbClsExtra = 0;
  85.     wcex.cbWndExtra = 0;
  86.     wcex.hInstance = hInstance;
  87.     wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINAPI));
  88.     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  89.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  90.     wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINAPI);
  91.     wcex.lpszClassName = szWindowClass;
  92.     wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  93.  
  94.     return RegisterClassExW(&wcex);
  95. }
  96.  
  97. //
  98. //   FUNCTION: InitInstance(HINSTANCE, int)
  99. //
  100. //   PURPOSE: Saves instance handle and creates main window
  101. //
  102. //   COMMENTS:
  103. //
  104. //        In this function, we save the instance handle in a global variable and
  105. //        create and display the main program window.
  106. //
  107. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  108. {
  109.     hInst = hInstance; // Store instance handle in our global variable
  110.  
  111.     HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  112.         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
  113.  
  114.     if (!hWnd)
  115.     {
  116.         return FALSE;
  117.     }
  118.  
  119.     ShowWindow(hWnd, nCmdShow);
  120.     UpdateWindow(hWnd);
  121.  
  122.     return TRUE;
  123. }
  124.  
  125. int w_left = 100;
  126. int w_right = 200;
  127. int w_top = 100;
  128. int w_bottom = 200;
  129.  
  130. bool collision = false;
  131. POINT coordinates;
  132. SYSTEMTIME previousTime;
  133.  
  134. //
  135. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  136. //
  137. //  PURPOSE:  Processes messages for the main window.
  138. //
  139. //  WM_COMMAND  - process the application menu
  140. //  WM_PAINT    - Paint the main window
  141. //  WM_DESTROY  - post a quit message and return
  142. //
  143. //
  144. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  145. {
  146.     switch (message)
  147.     {
  148.     case WM_COMMAND:
  149.     {
  150.         int wmId = LOWORD(wParam);
  151.         // Parse the menu selections:
  152.         switch (wmId)
  153.         {
  154.         case IDM_ABOUT:
  155.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  156.             break;
  157.         case IDM_EXIT:
  158.             DestroyWindow(hWnd);
  159.             break;
  160.         default:
  161.             return DefWindowProc(hWnd, message, wParam, lParam);
  162.         }
  163.     }
  164.     break;
  165.     case WM_PAINT:
  166.     {
  167.         // TODO: Add any drawing code that uses hdc here...
  168.         PAINTSTRUCT ps;
  169.         HDC hDC = BeginPaint(hWnd, &ps);
  170.         HPEN hpenOld = static_cast<HPEN>(SelectObject(hDC, GetStockObject(DC_PEN)));
  171.         HBRUSH hbrushOld = static_cast<HBRUSH>(SelectObject(hDC, GetStockObject(NULL_BRUSH)));
  172.  
  173.         // Calculate the dimensions of the 4 equal rectangles.
  174.         RECT rcWindow;
  175.         GetClientRect(hWnd, &rcWindow);
  176.  
  177.         // Draw (differently-colored) borders around these rectangles.
  178.         if (collision)
  179.         {
  180.             SetDCPenColor(hDC, RGB(255, 0, 0));
  181.         }
  182.         else
  183.         {
  184.             SetDCPenColor(hDC, RGB(0, 0, 0));
  185.         }
  186.  
  187.         Rectangle(hDC, w_left, w_top, w_right, w_bottom);
  188.  
  189.         tstringstream posX;
  190.         posX << coordinates.x;
  191.  
  192.         tstringstream posY;
  193.         posY << coordinates.y;
  194.  
  195.         std::string space = ", ";
  196.         std::wstring wsSpace(space.begin(), space.end());
  197.  
  198.         std::wstring stringPosX = posX.str();
  199.         std::wstring stringPosY = posY.str();
  200.         stringPosX.append(wsSpace);
  201.         stringPosX.append(stringPosY);
  202.  
  203.         DrawText(hDC, stringPosX.c_str(), -1, &rcWindow, DT_TOP | DT_SINGLELINE | DT_LEFT);
  204.  
  205.         // Draw the text into the center of each of the rectangles.
  206.         SetBkMode(hDC, TRANSPARENT);
  207.         SetBkColor(hDC, RGB(0, 0, 0));
  208.  
  209.         // Clean up after ourselves.
  210.         SelectObject(hDC, hpenOld);
  211.         SelectObject(hDC, hbrushOld);
  212.         EndPaint(hWnd, &ps);
  213.     }
  214.     break;
  215.     case WM_MOUSEMOVE:
  216.         GetCursorPos(&coordinates);
  217.         ScreenToClient(hWnd, &coordinates);
  218.  
  219.         if (coordinates.x >= w_left && coordinates.x <= w_right && coordinates.y >= w_top && coordinates.y <= w_bottom)
  220.         {
  221.             if (!collision)
  222.             {
  223.                 SetTimer(hWnd, 15, 5000, (TIMERPROC)NULL);
  224.             }
  225.  
  226.             collision = true;
  227.         }
  228.         else
  229.         {
  230.             KillTimer(hWnd, 15);
  231.             collision = false;
  232.         }
  233.  
  234.         RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
  235.         break;
  236.     case WM_DESTROY:
  237.         PostQuitMessage(0);
  238.         break;
  239.     case WM_TIMER:
  240.         PostQuitMessage(0);
  241.         break;
  242.     default:
  243.         return DefWindowProc(hWnd, message, wParam, lParam);
  244.     }
  245.     return 0;
  246. }
  247.  
  248. VOID CALLBACK MyTimerProc(
  249.     HWND hwnd,        // handle to window for timer messages
  250.     UINT message,     // WM_TIMER message
  251.     UINT idTimer,     // timer identifier
  252.     DWORD dwTime)     // current system time
  253. {
  254. }
  255.  
  256. // Message handler for about box.
  257. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  258. {
  259.     UNREFERENCED_PARAMETER(lParam);
  260.     switch (message)
  261.     {
  262.     case WM_INITDIALOG:
  263.         return (INT_PTR)TRUE;
  264.  
  265.     case WM_COMMAND:
  266.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  267.         {
  268.             EndDialog(hDlg, LOWORD(wParam));
  269.             return (INT_PTR)TRUE;
  270.         }
  271.         break;
  272.     }
  273.     return (INT_PTR)FALSE;
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement