Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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 <QtGui/qwindow.h>
8
9#include <qt_windows.h>
10
12
13static bool isTopLevelWindow(HWND hwnd)
14{
15 return hwnd && ::GetAncestor(hwnd, GA_ROOT) == hwnd;
16}
17
18static bool canCaptureWindow(HWND hwnd)
19{
20 Q_ASSERT(hwnd);
21
22 if (!::IsWindowVisible(hwnd))
23 return false;
24
25 RECT rect{};
26 if (!::GetWindowRect(hwnd, &rect))
27 return false;
28
29 if (rect.left >= rect.right || rect.top >= rect.bottom)
30 return false;
31
32 return true;
33}
34
35static QString windowTitle(HWND hwnd) {
36 // QTBUG-114890
37 // TODO: investigate the case when hwnd is inner and belows to another thread.
38 // It might causes deadlocks in specific cases.
39 auto titleLength = ::GetWindowTextLengthW(hwnd);
40 std::wstring buffer(titleLength + 1, L'\0');
41 titleLength = ::GetWindowTextW(hwnd, buffer.data(), titleLength + 1);
42 buffer.resize(titleLength);
43
44 return QString::fromStdWString(buffer);
45}
46
47QList<QCapturableWindow> QWinCapturableWindows::windows() const
48{
49 QList<QCapturableWindow> result;
50
51 auto windowHandler = [](HWND hwnd, LPARAM lParam) {
52 if (!canCaptureWindow(hwnd))
53 return TRUE; // Ignore window and continue enumerating
54
55 auto& windows = *reinterpret_cast<QList<QCapturableWindow>*>(lParam);
56
57 windows.push_back(QCapturableWindowPrivate::create(
58 reinterpret_cast<QCapturableWindowPrivate::Id>(hwnd),
59 windowTitle(hwnd)));
60
61 return TRUE;
62 };
63
64 ::EnumWindows(windowHandler, reinterpret_cast<LPARAM>(&result));
65
66 return result;
67}
68
69bool QWinCapturableWindows::isWindowValid(const QCapturableWindowPrivate &window) const
70{
71 const auto hwnd = reinterpret_cast<HWND>(window.id);
72 return isTopLevelWindow(hwnd) && canCaptureWindow(hwnd);
73}
74
75q23::expected<QCapturableWindow, QString> QWinCapturableWindows::fromQWindow(QWindow *window) const
76{
77 const auto hwnd = reinterpret_cast<HWND>(window->winId());
78 return QCapturableWindowPrivate::create(
79 reinterpret_cast<QCapturableWindowPrivate::Id>(hwnd),
80 windowTitle(hwnd));
81}
82QT_END_NAMESPACE
static bool canCaptureWindow(HWND hwnd)
static QT_BEGIN_NAMESPACE bool isTopLevelWindow(HWND hwnd)
static QString windowTitle(HWND hwnd)