#include #include #include #include #include "QWinHost.h" #include #include LRESULT CALLBACK WindowProc(_In_ HWND hwnd, _In_ UINT message, _In_ WPARAM wParam, _In_ LPARAM lParam) { switch (message) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); RECT r{}; GetClientRect(hwnd, &r); HBRUSH b = CreateSolidBrush(RGB(100, 100, 150)); FillRect(hdc, &r, b); DeleteObject(b); SetTextColor(hdc, 0x00000000); LPCWSTR message = L"Native Center View"; DrawText( hdc, message, -1, &r, DT_SINGLELINE | DT_CENTER | DT_VCENTER ); EndPaint(hwnd, &ps); return 0; } case WM_SIZE: { InvalidateRect(hwnd, nullptr, FALSE); break; } case WM_DESTROY: { QString msg = "Warning: Native Center View got destroyed!"; qWarning() << msg; break; } } return DefWindowProc(hwnd, message, wParam, lParam); } int main( int argc, char *argv[] ) { SetDllDirectory(_T("")); // Register our embedded test window class auto hInstance = GetModuleHandle(NULL); WNDCLASS wc; wc.style = 0; wc.lpfnWndProc = (WNDPROC)WindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon((HINSTANCE)NULL, IDI_APPLICATION); wc.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW); wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 255)); wc.lpszMenuName = nullptr; wc.lpszClassName = L"EmbeddedWindow"; if (!RegisterClass(&wc)) return FALSE; //--------------------------------------------------------------------- qputenv("QT_ENABLE_HIGHDPI_SCALING", "0"); //--------------------------------------------------------------------- QApplication app( argc, argv ); QWinHost* winHost = new QWinHost(); // Create native win32 window that gets embedded in our QMainWindow's central area HWND embededWin = CreateWindow(L"EmbeddedWindow", // Predefined class; L"EmbeddedWindow", // caption WS_TABSTOP | WS_VISIBLE | WS_CHILD, // Styles 0, // x position 0, // y position 800, // Button width 600, // Button height (HWND)winHost->winId(), // Parent window NULL, // No menu. hInstance, NULL); // Pointer not needed. winHost->setWindow(embededWin); // Create Top Level MainWindow auto mainWindow = new QMainWindow(); //mainWindow->setWindowTitle(QString("TLW Surface Change on Dock - Qt%1.%2.%3").arg(QT_VERSION_MAJOR).arg(QT_VERSION_MINOR).arg(QT_VERSION_PATCH)); mainWindow->setCentralWidget(winHost); mainWindow->resize(800, 600); mainWindow->show(); // Create dock widget for hosting a web view auto dockWidget = new QDockWidget("WebView", mainWindow); dockWidget->setAllowedAreas(Qt::AllDockWidgetAreas); mainWindow->addDockWidget(Qt::LeftDockWidgetArea, dockWidget); dockWidget->setFloating(true); dockWidget->show(); dockWidget->windowHandle()->winId(); auto webView = new QWebEngineView(); webView->load(QUrl("https://qt-project.org/")); webView->show(); webView->windowHandle()->winId(); dockWidget->setWidget(webView); auto tlwSurfaceType = mainWindow->windowHandle()->surfaceType(); qDebug() << "MainWindow surface type:" << tlwSurfaceType; QObject::connect(dockWidget, &QDockWidget::topLevelChanged, mainWindow, [mainWindow, tlwSurfaceType]() { auto currentSurfaceType = mainWindow->windowHandle()->surfaceType(); if (tlwSurfaceType != currentSurfaceType) { qDebug() << "MainWindow surface type changed to :" << currentSurfaceType; } }); // Run Qt message loop return app.exec(); }