#include #include #include #include #include class Paintable final : public QQuickPaintedItem { public: explicit Paintable(QQuickItem* parent = nullptr) : QQuickPaintedItem(parent) { setOpaquePainting(true); } void paint(QPainter* painter) override { QColor highlight{255, 0, 0, 50}; painter->fillRect(QRect{QPoint{10, 0}, QSize{100, 100}}, highlight); painter->fillRect(QRect{QPoint{20, 20}, QSize{100, 100}}, highlight); painter->fillRect(QRect{QPoint{100, 100}, QSize{100, 100}}, highlight); } }; QScreen* gscreen = nullptr; class MyQWindow : public QQuickWindow { protected: void mousePressEvent(QMouseEvent*) override { float f = devicePixelRatio(); float f1 = gscreen->devicePixelRatio(); char s[100]; sprintf(s, "Ratio=%g,%g\n", f, f1); OutputDebugStringA(s); } }; MyQWindow* qquickWindow = nullptr; HWND hWnd1 = NULL; LRESULT WndProc1(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { #if 0 if (msg == WM_DPICHANGED) { int dpi = HIWORD(wParam); int dpi1 = LOWORD(wParam); } if (msg == 0x2E2) // WM_DPICHANGED_BEFOREPARENT) { PostMessage(hWnd, WM_USER + 10, 0, 0); int dpi = HIWORD(wParam); int dpi1 = LOWORD(wParam); } if (msg == 0x2E3) // WM_DPICHANGED_AFTERPARENT) { int dpi = HIWORD(wParam); int dpi1 = LOWORD(wParam); } if (0) // msg == 0x2E3 ) //WM_USER+10 ) { qquickWindow->destroy(); qquickWindow = new MyQWindow(); QWindow* qMain = QWindow::fromWinId((WId)hWnd1); qquickWindow->setParent(qMain); auto* paintable = new Paintable(qquickWindow->contentItem()); paintable->setSize(QSize{1000, 1000}); paintable->setVisible(true); qquickWindow->show(); } #endif return DefWindowProc(hWnd, msg, wParam, lParam); } int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); AllocConsole(); freopen("CONOUT$", "wt", stdout); printf("Qt test:\n"); HINSTANCE hInst = GetModuleHandle(NULL); WNDCLASS wc = {}; wc.hInstance = hInst; wc.lpfnWndProc = DefWindowProc; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszClassName = L"MyClass"; RegisterClass(&wc); WNDCLASS wc1 = {}; wc1.hInstance = hInst; wc1.lpfnWndProc = WndProc1; wc1.hCursor = LoadCursor(NULL, IDC_ARROW); wc1.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc1.lpszClassName = L"MyClass1"; RegisterClass(&wc1); HMODULE hUser32 = LoadLibrary(L"User32.dll"); using SetThreadDpiAwarenessContextFuncType = DPI_AWARENESS_CONTEXT(WINAPI*)(DPI_AWARENESS_CONTEXT); using SetThreadDpiHostingBehaviourFuncType = DPI_HOSTING_BEHAVIOR(WINAPI*)(DPI_HOSTING_BEHAVIOR); auto setHostingBehavior = (SetThreadDpiHostingBehaviourFuncType)GetProcAddress( hUser32, "SetThreadDpiHostingBehavior"); auto setDpiContext = (SetThreadDpiAwarenessContextFuncType)GetProcAddress( hUser32, "SetThreadDpiAwarenessContext"); setDpiContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); setHostingBehavior(DPI_HOSTING_BEHAVIOR_MIXED); HWND hWnd = CreateWindow(L"MyClass", L"WINAPI Window", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, hInst, NULL); ShowWindow(hWnd, SW_NORMAL); hWnd1 = CreateWindow(L"MyClass1", L"WINAPI Window", WS_CHILD | WS_VISIBLE, 100, 100, 500, 500, hWnd, NULL, hInst, NULL); qquickWindow = new MyQWindow(); QWindow* qMain = QWindow::fromWinId((WId)hWnd1); qquickWindow->setParent(qMain); QScreen* screen = gscreen = qquickWindow->screen(); QObject::connect(screen, &QScreen::logicalDotsPerInchChanged, [screen, qwin=qquickWindow]() { printf("logicalDotsPerInchChanged: %.3f (QWindow: %.3f)\n", screen->devicePixelRatio(), qwin->devicePixelRatio()); }); QObject::connect(qquickWindow, &QWindow::screenChanged, [qwin=qquickWindow](QScreen* screen) { printf("screenChanged: %.3f (QWindow: %.3f)\n", screen ? screen->devicePixelRatio() : -1.0, qwin->devicePixelRatio()); gscreen = screen; if (screen) { QObject::connect(screen, &QScreen::logicalDotsPerInchChanged, [screen, qwin]() { printf("logicalDotsPerInchChanged: %.3f (QWindow: %.3f)\n", screen->devicePixelRatio(), qwin->devicePixelRatio()); }); } }); auto* paintable = new Paintable(qquickWindow->contentItem()); paintable->setSize(QSize{1000, 1000}); paintable->setVisible(true); qquickWindow->show(); return app.exec(); }