| 1 | /* |
| 2 | * Copyright (C) 2018 Damir Porobic <https://github.com/damirporobic> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "WinWrapper.h" |
| 21 | |
| 22 | QRect WinWrapper::getFullScreenRect() const |
| 23 | { |
| 24 | auto height = GetSystemMetrics(SM_CYVIRTUALSCREEN); |
| 25 | auto width = GetSystemMetrics(SM_CXVIRTUALSCREEN); |
| 26 | auto x = GetSystemMetrics(SM_XVIRTUALSCREEN); |
| 27 | auto y = GetSystemMetrics(SM_YVIRTUALSCREEN); |
| 28 | |
| 29 | return {x, y, width, height}; |
| 30 | } |
| 31 | |
| 32 | QRect WinWrapper::getActiveWindowRect() const |
| 33 | { |
| 34 | RECT window, frame; |
| 35 | auto handle = GetForegroundWindow(); |
| 36 | GetWindowRect(handle, &window); |
| 37 | |
| 38 | DwmGetWindowAttribute(handle, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT)); |
| 39 | |
| 40 | return {QPoint(frame.left, frame.top), QPoint(frame.right, frame.bottom)}; |
| 41 | } |
| 42 | |
| 43 | CursorDto WinWrapper::getCursorWithPosition() const |
| 44 | { |
| 45 | CURSORINFO cursorInfo = { sizeof(cursorInfo) }; |
| 46 | GetCursorInfo(&cursorInfo); |
| 47 | auto cursorPosition = getCursorPosition(QRect(), cursorInfo); |
| 48 | auto cursorPixmap = getCursorPixmap(cursorInfo); |
| 49 | |
| 50 | return {cursorPixmap, cursorPosition}; |
| 51 | } |
| 52 | |
| 53 | QPoint WinWrapper::getCursorPosition(const QRect &rect, const CURSORINFO &cursor) const |
| 54 | { |
| 55 | ICONINFOEXW iconInfo = { sizeof(iconInfo) }; |
| 56 | GetIconInfoExW(cursor.hCursor, &iconInfo); |
| 57 | |
| 58 | auto x = cursor.ptScreenPos.x - (int)iconInfo.xHotspot; |
| 59 | auto y = cursor.ptScreenPos.y - (int)iconInfo.yHotspot; |
| 60 | |
| 61 | return { x, y }; |
| 62 | } |
| 63 | |
| 64 | QPixmap WinWrapper::getCursorPixmap(const CURSORINFO &cursor) const |
| 65 | { |
| 66 | // Get Cursor Size |
| 67 | auto cursorWidth = GetSystemMetrics(SM_CXCURSOR); |
| 68 | auto cursorHeight = GetSystemMetrics(SM_CYCURSOR); |
| 69 | |
| 70 | // Get your device contexts. |
| 71 | auto screenHandle = GetDC(nullptr); |
| 72 | auto memoryHandle = CreateCompatibleDC(screenHandle); |
| 73 | |
| 74 | // Create the bitmap to use as a canvas. |
| 75 | auto canvasBitmap = CreateCompatibleBitmap(screenHandle, cursorWidth, cursorHeight); |
| 76 | |
| 77 | // Select the bitmap into the device context. |
| 78 | auto oldBitmap = SelectObject(memoryHandle, canvasBitmap); |
| 79 | |
| 80 | // Draw the cursor into the canvas. |
| 81 | DrawIcon(memoryHandle, 0, 0, cursor.hCursor); |
| 82 | |
| 83 | // Convert to QPixmap |
| 84 | auto cursorPixmap = fromHBITMAP(canvasBitmap, QtWin::HBitmapAlpha); |
| 85 | |
| 86 | // Clean up after yourself. |
| 87 | SelectObject(memoryHandle, oldBitmap); |
| 88 | DeleteObject(canvasBitmap); |
| 89 | DeleteDC(memoryHandle); |
| 90 | ReleaseDC(nullptr, screenHandle); |
| 91 | |
| 92 | return cursorPixmap; |
| 93 | } |