From 5d3a6e62a1e7853e94dc5f9deefef6823f16314f Mon Sep 17 00:00:00 2001 From: Nguyen Duy Date: Fri, 7 Jun 2024 07:37:23 +0700 Subject: [PATCH] app: fix offline display arrival --- app/MainWindow.xaml.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/MainWindow.xaml.cs b/app/MainWindow.xaml.cs index f9cac72..2709f2c 100644 --- a/app/MainWindow.xaml.cs +++ b/app/MainWindow.xaml.cs @@ -37,6 +37,10 @@ protected override void OnSourceInitialized(EventArgs e) Handle = new WindowInteropHelper(this).EnsureHandle(); Helper.EnableDropShadow(Handle); + + var source = HwndSource.FromHwnd(Handle); + source.AddHook(new HwndSourceHook(WndProc)); + } protected override void OnClosing(CancelEventArgs e) @@ -98,14 +102,15 @@ private void Window_Unloaded(object sender, RoutedEventArgs e) SystemEvents.DisplaySettingsChanged -= DisplayChanged; } - private void DisplayChanged(object sender, EventArgs e) + private void DisplayChanged(object sender, EventArgs e) { Dispatcher.Invoke(() => { var displays = ParsecVDD.GetDisplays(out bool noMonitors); xDisplays.Children.Clear(); - xNoDisplay.Visibility = displays.Count <= 0 ? Visibility.Visible : Visibility.Hidden; + xNoDisplay.Visibility = displays.Count > 0 + ? Visibility.Hidden : Visibility.Visible; foreach (var display in displays) { @@ -206,5 +211,15 @@ private void Window_KeyDown(object sender, KeyEventArgs e) } } } + + private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + { + if (msg == 0x0219 && unchecked((int)wParam) == 0x7) + { + DisplayChanged(this, EventArgs.Empty); + } + + return IntPtr.Zero; + } } } \ No newline at end of file