Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // WinAPI.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "WinAPI.h"
- #include <string>
- #include <sstream>
- #define MAX_LOADSTRING 100
- using namespace std;
- #ifdef UNICODE
- typedef std::wostringstream tstringstream;
- #else
- typedef std::ostringstream tstringstream;
- #endif
- // Global Variables:
- HINSTANCE hInst; // current instance
- WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
- WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
- // Forward declarations of functions included in this code module:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // TODO: Place code here.
- // Initialize global strings
- LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadStringW(hInstance, IDC_WINAPI, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // Perform application initialization:
- if (!InitInstance(hInstance, nCmdShow))
- {
- return FALSE;
- }
- HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINAPI));
- MSG msg;
- // Main message loop:
- while (GetMessage(&msg, nullptr, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int)msg.wParam;
- }
- //
- // FUNCTION: MyRegisterClass()
- //
- // PURPOSE: Registers the window class.
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEXW wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINAPI));
- wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINAPI);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassExW(&wcex);
- }
- //
- // FUNCTION: InitInstance(HINSTANCE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- hInst = hInstance; // Store instance handle in our global variable
- HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- int w_left = 100;
- int w_right = 200;
- int w_top = 100;
- int w_bottom = 200;
- bool collision = false;
- POINT coordinates;
- SYSTEMTIME previousTime;
- //
- // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // WM_COMMAND - process the application menu
- // WM_PAINT - Paint the main window
- // WM_DESTROY - post a quit message and return
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_COMMAND:
- {
- int wmId = LOWORD(wParam);
- // Parse the menu selections:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- }
- break;
- case WM_PAINT:
- {
- // TODO: Add any drawing code that uses hdc here...
- PAINTSTRUCT ps;
- HDC hDC = BeginPaint(hWnd, &ps);
- HPEN hpenOld = static_cast<HPEN>(SelectObject(hDC, GetStockObject(DC_PEN)));
- HBRUSH hbrushOld = static_cast<HBRUSH>(SelectObject(hDC, GetStockObject(NULL_BRUSH)));
- // Calculate the dimensions of the 4 equal rectangles.
- RECT rcWindow;
- GetClientRect(hWnd, &rcWindow);
- // Draw (differently-colored) borders around these rectangles.
- if (collision)
- {
- SetDCPenColor(hDC, RGB(255, 0, 0));
- }
- else
- {
- SetDCPenColor(hDC, RGB(0, 0, 0));
- }
- Rectangle(hDC, w_left, w_top, w_right, w_bottom);
- tstringstream posX;
- posX << coordinates.x;
- tstringstream posY;
- posY << coordinates.y;
- std::string space = ", ";
- std::wstring wsSpace(space.begin(), space.end());
- std::wstring stringPosX = posX.str();
- std::wstring stringPosY = posY.str();
- stringPosX.append(wsSpace);
- stringPosX.append(stringPosY);
- DrawText(hDC, stringPosX.c_str(), -1, &rcWindow, DT_TOP | DT_SINGLELINE | DT_LEFT);
- // Draw the text into the center of each of the rectangles.
- SetBkMode(hDC, TRANSPARENT);
- SetBkColor(hDC, RGB(0, 0, 0));
- // Clean up after ourselves.
- SelectObject(hDC, hpenOld);
- SelectObject(hDC, hbrushOld);
- EndPaint(hWnd, &ps);
- }
- break;
- case WM_MOUSEMOVE:
- GetCursorPos(&coordinates);
- ScreenToClient(hWnd, &coordinates);
- if (coordinates.x >= w_left && coordinates.x <= w_right && coordinates.y >= w_top && coordinates.y <= w_bottom)
- {
- if (!collision)
- {
- SetTimer(hWnd, 15, 5000, (TIMERPROC)NULL);
- }
- collision = true;
- }
- else
- {
- KillTimer(hWnd, 15);
- collision = false;
- }
- RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_TIMER:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- VOID CALLBACK MyTimerProc(
- HWND hwnd, // handle to window for timer messages
- UINT message, // WM_TIMER message
- UINT idTimer, // timer identifier
- DWORD dwTime) // current system time
- {
- }
- // Message handler for about box.
- INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(lParam);
- switch (message)
- {
- case WM_INITDIALOG:
- return (INT_PTR)TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return (INT_PTR)TRUE;
- }
- break;
- }
- return (INT_PTR)FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement