1 | /* $Id: VBoxUtils-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Declarations of utility classes and functions for handling Windows specific tasks.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* GUI includes: */
|
---|
29 | #include "VBoxUtils-win.h"
|
---|
30 |
|
---|
31 |
|
---|
32 | /** Namespace for native window sub-system functions. */
|
---|
33 | namespace NativeWindowSubsystem
|
---|
34 | {
|
---|
35 | /** Enumerates visible always-on-top (top-most) windows. */
|
---|
36 | BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) RT_NOTHROW_PROTO;
|
---|
37 | /** Contains visible top-most-window rectangles. */
|
---|
38 | QList<QRect> topMostRects;
|
---|
39 | }
|
---|
40 |
|
---|
41 | BOOL CALLBACK NativeWindowSubsystem::EnumWindowsProc(HWND hWnd, LPARAM) RT_NOTHROW_DEF
|
---|
42 | {
|
---|
43 | /* Ignore NULL HWNDs: */
|
---|
44 | if (!hWnd)
|
---|
45 | return TRUE;
|
---|
46 |
|
---|
47 | /* Ignore hidden windows: */
|
---|
48 | if (!IsWindowVisible(hWnd))
|
---|
49 | return TRUE;
|
---|
50 |
|
---|
51 | /* Get window style: */
|
---|
52 | LONG uStyle = GetWindowLong(hWnd, GWL_STYLE);
|
---|
53 | /* Ignore minimized windows: */
|
---|
54 | if (uStyle & WS_MINIMIZE)
|
---|
55 | return TRUE;
|
---|
56 |
|
---|
57 | /* Get extended window style: */
|
---|
58 | LONG uExtendedStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
|
---|
59 | /* Ignore non-top-most windows: */
|
---|
60 | if (!(uExtendedStyle & WS_EX_TOPMOST))
|
---|
61 | return TRUE;
|
---|
62 |
|
---|
63 | /* Get that window rectangle: */
|
---|
64 | RECT rect;
|
---|
65 | GetWindowRect(hWnd, &rect);
|
---|
66 | topMostRects << QRect(QPoint(rect.left, rect.top), QPoint(rect.right - 1, rect.bottom - 1));
|
---|
67 |
|
---|
68 | /* Proceed to the next window: */
|
---|
69 | return TRUE;
|
---|
70 | }
|
---|
71 |
|
---|
72 | const QRegion NativeWindowSubsystem::areaCoveredByTopMostWindows()
|
---|
73 | {
|
---|
74 | /* Prepare the top-most region: */
|
---|
75 | QRegion topMostRegion;
|
---|
76 | /* Initialize the list of the top-most rectangles: */
|
---|
77 | topMostRects.clear();
|
---|
78 | /* Populate the list of top-most rectangles: */
|
---|
79 | EnumWindows((WNDENUMPROC)EnumWindowsProc, 0);
|
---|
80 | /* Update the top-most region with top-most rectangles: */
|
---|
81 | for (int iRectIndex = 0; iRectIndex < topMostRects.size(); ++iRectIndex)
|
---|
82 | topMostRegion += topMostRects[iRectIndex];
|
---|
83 | /* Return top-most region: */
|
---|
84 | return topMostRegion;
|
---|
85 | }
|
---|
86 |
|
---|
87 | const void NativeWindowSubsystem::setScreenSaverActive(BOOL fDisableScreenSaver)
|
---|
88 | {
|
---|
89 | BOOL fIsActive;
|
---|
90 | SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &fIsActive, 0);
|
---|
91 | if (fIsActive == !fDisableScreenSaver)
|
---|
92 | return;
|
---|
93 | //printf("before %d\n", fIsActive);
|
---|
94 |
|
---|
95 | SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, !fDisableScreenSaver, NULL, 0);
|
---|
96 |
|
---|
97 | SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &fIsActive, 0);
|
---|
98 | /*if (fIsActive == !fDisableScreenSaver)
|
---|
99 | printf("success %d %d\n", fIsActive, fDisableScreenSaver);
|
---|
100 | */
|
---|
101 | }
|
---|
102 |
|
---|
103 | BOOL NativeWindowSubsystem::ShutdownBlockReasonCreateAPI(HWND hWnd, LPCWSTR pwszReason)
|
---|
104 | {
|
---|
105 | BOOL fResult = FALSE;
|
---|
106 | typedef BOOL(WINAPI *PFNSHUTDOWNBLOCKREASONCREATE)(HWND hWnd, LPCWSTR pwszReason);
|
---|
107 |
|
---|
108 | PFNSHUTDOWNBLOCKREASONCREATE pfn = (PFNSHUTDOWNBLOCKREASONCREATE)GetProcAddress(
|
---|
109 | GetModuleHandle(L"User32.dll"), "ShutdownBlockReasonCreate");
|
---|
110 | _ASSERTE(pfn);
|
---|
111 | if (pfn)
|
---|
112 | fResult = pfn(hWnd, pwszReason);
|
---|
113 | return fResult;
|
---|
114 | }
|
---|
115 |
|
---|
116 | bool NativeWindowSubsystem::WinActivateWindow(WId wId, bool)
|
---|
117 | {
|
---|
118 | bool fResult = true;
|
---|
119 | HWND handle = (HWND)wId;
|
---|
120 |
|
---|
121 | if (IsIconic(handle))
|
---|
122 | fResult &= !!ShowWindow(handle, SW_RESTORE);
|
---|
123 | else if (!IsWindowVisible(handle))
|
---|
124 | fResult &= !!ShowWindow(handle, SW_SHOW);
|
---|
125 |
|
---|
126 | fResult &= !!SetForegroundWindow(handle);
|
---|
127 | return fResult;
|
---|
128 | }
|
---|