Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qwincapturablewindows.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "private/qcapturablewindow_p.h"
6
7#include <qt_windows.h>
8
10
11static bool isTopLevelWindow(HWND hwnd)
12{
13 return hwnd && ::GetAncestor(hwnd, GA_ROOT) == hwnd;
14}
15
16static bool canCaptureWindow(HWND hwnd)
17{
18 Q_ASSERT(hwnd);
19
20 if (!::IsWindowVisible(hwnd))
21 return false;
22
23 RECT rect{};
24 if (!::GetWindowRect(hwnd, &rect))
25 return false;
26
27 if (rect.left >= rect.right || rect.top >= rect.bottom)
28 return false;
29
30 return true;
31}
32
33static QString windowTitle(HWND hwnd) {
34 // QTBUG-114890
35 // TODO: investigate the case when hwnd is inner and belows to another thread.
36 // It might causes deadlocks in specific cases.
37 auto titleLength = ::GetWindowTextLengthW(hwnd);
38 std::wstring buffer(titleLength + 1, L'\0');
39 titleLength = ::GetWindowTextW(hwnd, buffer.data(), titleLength + 1);
40 buffer.resize(titleLength);
41
43}
44
45QList<QCapturableWindow> QWinCapturableWindows::windows() const
46{
47 QList<QCapturableWindow> result;
48
49 auto windowHandler = [](HWND hwnd, LPARAM lParam) {
50 if (!canCaptureWindow(hwnd))
51 return TRUE; // Ignore window and continue enumerating
52
53 auto& windows = *reinterpret_cast<QList<QCapturableWindow>*>(lParam);
54
55 auto windowData = std::make_unique<QCapturableWindowPrivate>();
56 windowData->id = reinterpret_cast<QCapturableWindowPrivate::Id>(hwnd);
57 windowData->description = windowTitle(hwnd);
58 windows.push_back(windowData.release()->create());
59
60 return TRUE;
61 };
62
63 ::EnumWindows(windowHandler, reinterpret_cast<LPARAM>(&result));
64
65 return result;
66}
67
69{
70 const auto hwnd = reinterpret_cast<HWND>(window.id);
71 return isTopLevelWindow(hwnd) && canCaptureWindow(hwnd);
72}
73
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromStdWString(const std::wstring &s)
Returns a copy of the str string.
Definition qstring.h:1458
bool isWindowValid(const QCapturableWindowPrivate &window) const override
QList< QCapturableWindow > windows() const override
rect
[4]
Combined button and popup list for selecting options.
GLenum GLuint buffer
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static bool canCaptureWindow(HWND hwnd)
static QT_BEGIN_NAMESPACE bool isTopLevelWindow(HWND hwnd)
static QString windowTitle(HWND hwnd)
aWidget window() -> setWindowTitle("New Window Title")
[2]