From 412dc8fccf7145de324e35c4a3dd2c5621e69d55 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Wed, 27 Mar 2024 16:11:43 +0800 Subject: [PATCH] fix: Save the last display name crashed --- DSView/pv/mainframe.cpp | 35 ++++++------------------------ DSView/pv/winnativewidget.cpp | 40 +++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/DSView/pv/mainframe.cpp b/DSView/pv/mainframe.cpp index fb326949..29ebd95a 100644 --- a/DSView/pv/mainframe.cpp +++ b/DSView/pv/mainframe.cpp @@ -230,11 +230,7 @@ bool MainFrame::ParentIsMaxsized() void MainFrame::MoveBegin() { -#ifdef _WIN32 - if (_parentNativeWidget != NULL){ - _move_start_screen = _parentNativeWidget->GetPointScreen(); - } -#endif + } void MainFrame::MoveEnd() @@ -250,17 +246,6 @@ void MainFrame::MoveEnd() #ifndef _WIN32 saveNormalRegion(); #endif - -#ifdef _WIN32 - if (_parentNativeWidget != NULL){ - auto scr = _parentNativeWidget->GetPointScreen(); - if (scr != _move_start_screen){ - _parentNativeWidget->UpdateChildDpi(); - } - - _parentNativeWidget->ResizeChild(); - } -#endif } void MainFrame::OnParentNativeEvent(ParentNativeEvent msg) @@ -270,15 +255,18 @@ void MainFrame::OnParentNativeEvent(ParentNativeEvent msg) void MainFrame::OnParentNaitveWindowEvent(int msg) { + #ifdef _WIN32 if (_parentNativeWidget != NULL && msg == PARENT_EVENT_DISPLAY_CHANGED){ + + qApp->processEvents(); //wait the screen dpi ready. QTimer::singleShot(100, this, [this](){ auto scr = _parentNativeWidget->GetPointScreen(); if (scr == NULL){ - dsv_info("ERROR: failed to get point screen, will to get the primary."); - scr = QGuiApplication::primaryScreen(); + dsv_info("ERROR: MainFrame::OnParentNaitveWindowEvent, failed to get pointing screen."); + return; } PopupDlgList::TryCloseAllByScreenChanged(scr); @@ -646,18 +634,7 @@ void MainFrame::writeSettings() { AppConfig &app = AppConfig::Instance(); app.frameOptions.isMax = IsMaxsized(); - -#ifdef _WIN32 - if (_parentNativeWidget != NULL){ - auto scr = _parentNativeWidget->GetPointScreen(); - app.frameOptions.displayName = scr->name(); - } - else{ - app.frameOptions.displayName = windowHandle()->screen()->name(); - } -#else app.frameOptions.displayName = windowHandle()->screen()->name(); -#endif if (IsNormalsized() && isVisible()){ saveNormalRegion(); diff --git a/DSView/pv/winnativewidget.cpp b/DSView/pv/winnativewidget.cpp index 718b1503..4be649c6 100644 --- a/DSView/pv/winnativewidget.cpp +++ b/DSView/pv/winnativewidget.cpp @@ -216,7 +216,18 @@ LRESULT CALLBACK WinNativeWidget::WndProc(HWND hWnd, UINT message, WPARAM wParam case WM_MOVE: { if (IsIconic(hWnd) == FALSE) { - self->_hCurrentMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST); + HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST); + + if (hMonitor == NULL){ + dsv_info("ERROR: WM_MOVE: get an invalid monitor."); + } + + if (hMonitor != self->_hCurrentMonitor && self->_hCurrentMonitor) + { + dsv_info("WM_MOVE:display be changed."); + } + + self->_hCurrentMonitor = hMonitor; self->ResizeChild(); } break; @@ -224,12 +235,15 @@ LRESULT CALLBACK WinNativeWidget::WndProc(HWND hWnd, UINT message, WPARAM wParam case WM_DPICHANGED: case WM_DISPLAYCHANGE: { - if (self->_event_callback != NULL){ - self->_event_callback->OnParentNativeEvent(PARENT_EVENT_DISPLAY_CHANGED); + if (message == WM_DPICHANGED){ + dsv_info("dpi changed."); } else{ - self->UpdateChildDpi(); - self->ResizeChild(); + dsv_info("display changed."); + } + + if (self->_event_callback != NULL && self->_childWindow != NULL){ + self->_event_callback->OnParentNativeEvent(PARENT_EVENT_DISPLAY_CHANGED); } break; } @@ -514,17 +528,23 @@ QScreen* WinNativeWidget::screenFromWindow(HWND hwnd) if (hwnd == NULL) return NULL; - HMONITOR monitor = _hCurrentMonitor; + HMONITOR hMonitor = _hCurrentMonitor; - if (monitor == NULL){ - monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); + if (hMonitor == NULL){ + hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); } MONITORINFO monitor_info; memset(&monitor_info, 0, sizeof(MONITORINFO)); monitor_info.cbSize = sizeof(MONITORINFO); - GetMonitorInfoW(monitor, &monitor_info); + GetMonitorInfoW(hMonitor, &monitor_info); + + int workAreaWidth = monitor_info.rcWork.right - monitor_info.rcWork.left; + if (workAreaWidth == 0){ + dsv_info("ERROR:WinNativeWidget::screenFromWindow, the monitor info is invalid."); + return NULL; + } QPoint top_left; top_left.setX(monitor_info.rcMonitor.left); @@ -537,6 +557,8 @@ QScreen* WinNativeWidget::screenFromWindow(HWND hwnd) return screen; } } + + dsv_info("ERROR:WinNativeWidget::screenFromWindow, can't match a monitor."); return NULL; }