|
| 1 | +#ifndef UNICODE |
| 2 | +#define UNICODE |
| 3 | +#endif |
| 4 | + |
| 5 | +#include <windows.h> |
| 6 | +#include <cstdio> |
| 7 | +#include "window.h" |
| 8 | + |
| 9 | +#define ID_CLOSE_TIMER 1001 |
| 10 | + |
| 11 | +LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); |
| 12 | + |
| 13 | +Window::Window(int x, int y, int width, int height) { |
| 14 | + this->_x = x; |
| 15 | + this->_y = y; |
| 16 | + this->_width = width; |
| 17 | + this->_height = height; |
| 18 | +} |
| 19 | + |
| 20 | +void Window::show(int duration, float opacity) { |
| 21 | + // Register the window class. |
| 22 | + const wchar_t CLASS_NAME[] = L"Highlight Window Class"; |
| 23 | + |
| 24 | + WNDCLASS wc = { }; |
| 25 | + |
| 26 | + wc.lpfnWndProc = WindowProc; |
| 27 | + wc.hInstance = NULL; |
| 28 | + wc.lpszClassName = CLASS_NAME; |
| 29 | + wc.hbrBackground = CreateSolidBrush(RGB(255, 0, 0)); |
| 30 | + |
| 31 | + RegisterClass(&wc); |
| 32 | + |
| 33 | + HWND hwnd = CreateWindowEx( |
| 34 | + WS_EX_LAYERED|WS_EX_TOPMOST|WS_EX_TRANSPARENT|WS_EX_TOOLWINDOW, |
| 35 | + CLASS_NAME, |
| 36 | + 0, |
| 37 | + WS_POPUP, |
| 38 | + this->_x, |
| 39 | + this->_y, |
| 40 | + this->_width, |
| 41 | + this->_height, |
| 42 | + NULL, |
| 43 | + NULL, |
| 44 | + NULL, |
| 45 | + NULL |
| 46 | + ); |
| 47 | + |
| 48 | + SetLayeredWindowAttributes(hwnd, 0, 255 * opacity, LWA_ALPHA); |
| 49 | + |
| 50 | + if (hwnd == NULL) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + SetTimer(hwnd, ID_CLOSE_TIMER, 2000, NULL); |
| 55 | + ShowWindow(hwnd, 1); |
| 56 | + |
| 57 | + MSG msg = { }; |
| 58 | + while (GetMessage(&msg, NULL, 0, 0)) { |
| 59 | + TranslateMessage(&msg); |
| 60 | + DispatchMessage(&msg); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) |
| 65 | +{ |
| 66 | + switch (uMsg) |
| 67 | + { |
| 68 | + case WM_DESTROY: |
| 69 | + KillTimer(hwnd, ID_CLOSE_TIMER); |
| 70 | + PostQuitMessage(0); |
| 71 | + return 0; |
| 72 | + case WM_TIMER: |
| 73 | + switch(wParam) { |
| 74 | + case ID_CLOSE_TIMER: |
| 75 | + KillTimer(hwnd, ID_CLOSE_TIMER); |
| 76 | + CloseWindow(hwnd); |
| 77 | + PostQuitMessage(0); |
| 78 | + return 0; |
| 79 | + } |
| 80 | + } |
| 81 | + return DefWindowProc(hwnd, uMsg, wParam, lParam); |
| 82 | +} |
0 commit comments