2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2014-11-07 14:57:25 +00:00
|
|
|
|
|
|
|
#include "nativewindowdump.h"
|
|
|
|
#include "qwindowdump.h"
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
2020-07-06 14:37:47 +00:00
|
|
|
#include <QtCore/QList>
|
2019-11-08 09:48:43 +00:00
|
|
|
#include <QtCore/QRect>
|
2020-07-06 14:37:47 +00:00
|
|
|
#include <QtCore/QSharedPointer>
|
|
|
|
#include <QtCore/QTextStream>
|
2014-11-07 14:57:25 +00:00
|
|
|
|
|
|
|
#include <QtCore/qt_windows.h>
|
|
|
|
|
2015-02-03 14:14:30 +00:00
|
|
|
#ifndef WS_EX_NOREDIRECTIONBITMAP
|
|
|
|
# define WS_EX_NOREDIRECTIONBITMAP 0x00200000L
|
|
|
|
#endif
|
|
|
|
|
2019-11-12 13:01:48 +00:00
|
|
|
using namespace Qt;
|
|
|
|
|
2014-11-07 14:57:25 +00:00
|
|
|
namespace QtDiag {
|
|
|
|
|
|
|
|
struct DumpContext {
|
2016-01-07 14:30:54 +00:00
|
|
|
DumpContext() : indentation(0), parent(0) {}
|
2014-11-07 14:57:25 +00:00
|
|
|
|
|
|
|
int indentation;
|
2016-01-07 14:30:54 +00:00
|
|
|
HWND parent;
|
2014-11-07 14:57:25 +00:00
|
|
|
QSharedPointer<QTextStream> stream;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define debugWinStyle(str, style, styleConstant) \
|
|
|
|
if (style & styleConstant) \
|
|
|
|
str << ' ' << #styleConstant;
|
|
|
|
|
2019-11-08 09:48:43 +00:00
|
|
|
static QTextStream &operator<<(QTextStream &str, const QPoint &p)
|
|
|
|
{
|
|
|
|
str << p.x() << ", " << p.y();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QTextStream &operator<<(QTextStream &str, const QSize &s)
|
|
|
|
{
|
|
|
|
str << s.width() << 'x' << s.height();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QTextStream &operator<<(QTextStream &str, const QRect &rect)
|
|
|
|
{
|
2020-01-28 10:46:16 +00:00
|
|
|
str << rect.size() << Qt::forcesign << rect.x() << rect.y() << Qt::noforcesign;
|
2019-11-08 09:48:43 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline QSize qsizeFromRECT(const RECT &rect)
|
|
|
|
{
|
|
|
|
return QSize(rect.right -rect.left, rect.bottom - rect.top);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline QRect qrectFromRECT(const RECT &rect)
|
|
|
|
{
|
|
|
|
return QRect(QPoint(rect.left, rect.top), qsizeFromRECT(rect));
|
|
|
|
}
|
|
|
|
|
|
|
|
static QRect getFrameGeometry(HWND hwnd)
|
|
|
|
{
|
|
|
|
RECT rect;
|
|
|
|
return GetWindowRect(hwnd, &rect) ? qrectFromRECT(rect) : QRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QPoint getClientAreaScreenPos(HWND hwnd)
|
|
|
|
{
|
|
|
|
POINT clientPos{0, 0};
|
|
|
|
return ClientToScreen(hwnd, &clientPos) ? QPoint(clientPos.x, clientPos.y) : QPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QRect getClientAreaGeometry(HWND hwnd)
|
|
|
|
{
|
|
|
|
RECT clientRect;
|
|
|
|
return GetClientRect(hwnd, &clientRect)
|
|
|
|
? QRect(getClientAreaScreenPos(hwnd), qsizeFromRECT(clientRect)) : QRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isTopLevel(HWND hwnd)
|
|
|
|
{
|
|
|
|
auto parent = GetParent(hwnd);
|
|
|
|
return !parent || parent == GetDesktopWindow();
|
|
|
|
}
|
|
|
|
|
2014-11-07 14:57:25 +00:00
|
|
|
static void formatNativeWindow(HWND hwnd, QTextStream &str)
|
|
|
|
{
|
2020-01-28 10:46:16 +00:00
|
|
|
str << Qt::hex << Qt::showbase << quintptr(hwnd) << Qt::noshowbase << Qt::dec;
|
2019-11-08 09:48:43 +00:00
|
|
|
|
|
|
|
const bool topLevel = isTopLevel(hwnd);
|
|
|
|
if (topLevel)
|
|
|
|
str << " [top]";
|
|
|
|
const auto frameGeometry = getFrameGeometry(hwnd);
|
|
|
|
const auto clientGeometry = getClientAreaGeometry(hwnd);
|
|
|
|
str << ' ' << frameGeometry;
|
|
|
|
if (!topLevel)
|
|
|
|
str << " local: " << (clientGeometry.topLeft() - getClientAreaScreenPos(GetParent(hwnd)));
|
|
|
|
if (clientGeometry != frameGeometry) {
|
|
|
|
str << " client: " << clientGeometry << " frame: "
|
|
|
|
<< (clientGeometry.topLeft() - frameGeometry.topLeft());
|
2014-11-07 14:57:25 +00:00
|
|
|
}
|
2019-11-08 09:48:43 +00:00
|
|
|
|
2014-11-07 14:57:25 +00:00
|
|
|
if (IsWindowVisible(hwnd))
|
|
|
|
str << " [visible]";
|
|
|
|
|
2016-01-07 14:30:54 +00:00
|
|
|
wchar_t buf[512];
|
|
|
|
if (GetWindowText(hwnd, buf, sizeof(buf)/sizeof(buf[0])) && buf[0])
|
|
|
|
str << " title=\"" << QString::fromWCharArray(buf) << "\"/";
|
|
|
|
else
|
|
|
|
str << ' ';
|
|
|
|
if (GetClassName(hwnd, buf, sizeof(buf)/sizeof(buf[0])))
|
|
|
|
str << '"' << QString::fromWCharArray(buf) << '"';
|
|
|
|
|
2020-01-28 10:46:16 +00:00
|
|
|
str << Qt::hex << Qt::showbase;
|
2014-11-07 14:57:25 +00:00
|
|
|
if (const LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE)) {
|
|
|
|
str << " style=" << style;
|
|
|
|
debugWinStyle(str, style, WS_OVERLAPPED)
|
|
|
|
debugWinStyle(str, style, WS_POPUP)
|
|
|
|
debugWinStyle(str, style, WS_MINIMIZE)
|
|
|
|
debugWinStyle(str, style, WS_CHILD)
|
|
|
|
debugWinStyle(str, style, WS_VISIBLE)
|
|
|
|
debugWinStyle(str, style, WS_DISABLED)
|
|
|
|
debugWinStyle(str, style, WS_CLIPSIBLINGS)
|
|
|
|
debugWinStyle(str, style, WS_CLIPCHILDREN)
|
|
|
|
debugWinStyle(str, style, WS_MAXIMIZE)
|
|
|
|
debugWinStyle(str, style, WS_CAPTION)
|
|
|
|
debugWinStyle(str, style, WS_BORDER)
|
|
|
|
debugWinStyle(str, style, WS_DLGFRAME)
|
|
|
|
debugWinStyle(str, style, WS_VSCROLL)
|
|
|
|
debugWinStyle(str, style, WS_HSCROLL)
|
|
|
|
debugWinStyle(str, style, WS_SYSMENU)
|
|
|
|
debugWinStyle(str, style, WS_THICKFRAME)
|
|
|
|
debugWinStyle(str, style, WS_GROUP)
|
|
|
|
debugWinStyle(str, style, WS_TABSTOP)
|
|
|
|
debugWinStyle(str, style, WS_MINIMIZEBOX)
|
|
|
|
debugWinStyle(str, style, WS_MAXIMIZEBOX)
|
|
|
|
}
|
|
|
|
if (const LONG_PTR exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE)) {
|
|
|
|
str << " exStyle=" << exStyle;
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_DLGMODALFRAME)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_NOPARENTNOTIFY)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_TOPMOST)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_ACCEPTFILES)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_TRANSPARENT)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_MDICHILD)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_TOOLWINDOW)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_WINDOWEDGE)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_CLIENTEDGE)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_CONTEXTHELP)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_RIGHT)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_LEFT)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_RTLREADING)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_LTRREADING)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_LEFTSCROLLBAR)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_RIGHTSCROLLBAR)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_CONTROLPARENT)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_STATICEDGE)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_APPWINDOW)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_LAYERED)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_NOINHERITLAYOUT)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_NOREDIRECTIONBITMAP)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_LAYOUTRTL)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_COMPOSITED)
|
|
|
|
debugWinStyle(str, exStyle, WS_EX_NOACTIVATE)
|
|
|
|
}
|
2016-01-07 14:30:54 +00:00
|
|
|
|
|
|
|
if (const ULONG_PTR classStyle = GetClassLongPtr(hwnd, GCL_STYLE)) {
|
|
|
|
str << " classStyle=" << classStyle;
|
|
|
|
debugWinStyle(str, classStyle, CS_BYTEALIGNCLIENT)
|
|
|
|
debugWinStyle(str, classStyle, CS_BYTEALIGNWINDOW)
|
|
|
|
debugWinStyle(str, classStyle, CS_CLASSDC)
|
|
|
|
debugWinStyle(str, classStyle, CS_DBLCLKS)
|
|
|
|
debugWinStyle(str, classStyle, CS_DROPSHADOW)
|
|
|
|
debugWinStyle(str, classStyle, CS_GLOBALCLASS)
|
|
|
|
debugWinStyle(str, classStyle, CS_HREDRAW)
|
|
|
|
debugWinStyle(str, classStyle, CS_NOCLOSE)
|
|
|
|
debugWinStyle(str, classStyle, CS_OWNDC)
|
|
|
|
debugWinStyle(str, classStyle, CS_PARENTDC)
|
|
|
|
debugWinStyle(str, classStyle, CS_SAVEBITS)
|
|
|
|
debugWinStyle(str, classStyle, CS_VREDRAW)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const ULONG_PTR wndProc = GetClassLongPtr(hwnd, GCLP_WNDPROC))
|
|
|
|
str << " wndProc=" << wndProc;
|
|
|
|
|
2020-01-28 10:46:16 +00:00
|
|
|
str << Qt::noshowbase << Qt::dec;
|
2014-11-07 14:57:25 +00:00
|
|
|
|
2016-01-07 14:30:54 +00:00
|
|
|
if (GetWindowModuleFileName(hwnd, buf, sizeof(buf)/sizeof(buf[0])))
|
|
|
|
str << " module=\"" << QString::fromWCharArray(buf) << '"';
|
|
|
|
|
2014-11-07 14:57:25 +00:00
|
|
|
str << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dumpNativeWindowRecursion(HWND hwnd, DumpContext *dc);
|
|
|
|
|
|
|
|
BOOL CALLBACK dumpWindowEnumChildProc(HWND hwnd, LPARAM lParam)
|
|
|
|
{
|
2016-01-07 14:30:54 +00:00
|
|
|
DumpContext *dumpContext = reinterpret_cast<DumpContext *>(lParam);
|
|
|
|
// EnumChildWindows enumerates grand children as well, skip these to
|
|
|
|
// get the hierarchical formatting right.
|
|
|
|
if (GetAncestor(hwnd, GA_PARENT) == dumpContext->parent)
|
|
|
|
dumpNativeWindowRecursion(hwnd, dumpContext);
|
2014-11-07 14:57:25 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dumpNativeWindowRecursion(HWND hwnd, DumpContext *dc)
|
|
|
|
{
|
|
|
|
indentStream(*dc->stream, dc->indentation);
|
|
|
|
formatNativeWindow(hwnd, *dc->stream);
|
|
|
|
DumpContext nextLevel = *dc;
|
|
|
|
nextLevel.indentation += 2;
|
2016-01-07 14:30:54 +00:00
|
|
|
nextLevel.parent = hwnd;
|
2014-11-07 14:57:25 +00:00
|
|
|
EnumChildWindows(hwnd, dumpWindowEnumChildProc, reinterpret_cast<LPARAM>(&nextLevel));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* recurse by Z order, same result */
|
|
|
|
static void dumpNativeWindowRecursionByZ(HWND hwnd, DumpContext *dc)
|
|
|
|
{
|
|
|
|
indentStream(*dc->stream, dc->indentation);
|
|
|
|
formatNativeWindow(hwnd, *dc->stream);
|
|
|
|
if (const HWND topChild = GetTopWindow(hwnd)) {
|
|
|
|
DumpContext nextLevel = *dc;
|
|
|
|
nextLevel.indentation += 2;
|
|
|
|
for (HWND child = topChild; child ; child = GetNextWindow(child, GW_HWNDNEXT))
|
|
|
|
dumpNativeWindowRecursionByZ(child, &nextLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 14:37:47 +00:00
|
|
|
typedef QList<WId> WIdVector;
|
2014-11-07 14:57:25 +00:00
|
|
|
|
|
|
|
static void dumpNativeWindows(const WIdVector& wins)
|
|
|
|
{
|
|
|
|
DumpContext dc;
|
|
|
|
QString s;
|
|
|
|
dc.stream = QSharedPointer<QTextStream>(new QTextStream(&s));
|
2020-01-28 10:46:16 +00:00
|
|
|
for (WId win : wins)
|
2014-11-07 14:57:25 +00:00
|
|
|
dumpNativeWindowRecursion(reinterpret_cast<HWND>(win), &dc);
|
|
|
|
qDebug().noquote() << s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpNativeWindows(WId rootIn)
|
|
|
|
{
|
|
|
|
const WId root = rootIn ? rootIn : reinterpret_cast<WId>(GetDesktopWindow());
|
|
|
|
dumpNativeWindows(WIdVector(1, root));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CALLBACK findQtTopLevelEnumChildProc(HWND hwnd, LPARAM lParam)
|
|
|
|
{
|
|
|
|
WIdVector *v = reinterpret_cast<WIdVector *>(lParam);
|
|
|
|
wchar_t buf[512];
|
|
|
|
if (GetClassName(hwnd, buf, sizeof(buf)/sizeof(buf[0]))) {
|
|
|
|
if (wcsstr(buf, L"Qt"))
|
|
|
|
v->append(reinterpret_cast<WId>(hwnd));
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WIdVector findQtTopLevels()
|
|
|
|
{
|
|
|
|
WIdVector result;
|
|
|
|
EnumChildWindows(GetDesktopWindow(),
|
|
|
|
findQtTopLevelEnumChildProc,
|
|
|
|
reinterpret_cast<LPARAM>(&result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpNativeQtTopLevels()
|
|
|
|
{
|
|
|
|
dumpNativeWindows(findQtTopLevels());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace QtDiag
|