From 10e2a2cf3b7abebd9afaac6646c8a67c97bfeeb1 Mon Sep 17 00:00:00 2001 From: Frank Skare Date: Sat, 4 Sep 2021 12:39:28 +0200 Subject: [PATCH] #318 Fix message box not working when ontop is enabled --- docs/Changelog.md | 3 +- src/Misc/Msg.cs | 2 +- src/WPF/MsgBox/MessageBoxEx.xaml.cs | 5 +- src/WPF/MsgBox/MessageBoxExStatic.cs | 70 ++++++++++++++++------------ src/WPF/MsgBox/Native.cs | 15 ++++++ src/mpv.net.csproj | 1 + 6 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 src/WPF/MsgBox/Native.cs diff --git a/docs/Changelog.md b/docs/Changelog.md index a1d3076..7f0b964 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -3,7 +3,8 @@ - All PowerShell dependencies except the scipt host were removed in order to achieve first class Windows 7 compatibility! - +- Fix message box not working when ontop is enabled. + 5.4.9.7 Beta (2021-08-28) diff --git a/src/Misc/Msg.cs b/src/Misc/Msg.cs index e08e168..d2c0cd4 100644 --- a/src/Misc/Msg.cs +++ b/src/Misc/Msg.cs @@ -44,7 +44,7 @@ public class Msg string msg = title?.ToString().TrimEx(); MessageBoxEx.DetailsText = details; string windowTitle = System.Windows.Forms.Application.ProductName; - return MessageBoxEx.OpenMessageBox(null, msg, windowTitle, buttons, img); + return MessageBoxEx.OpenMessageBox(msg, windowTitle, buttons, img); } ApartmentState state = Thread.CurrentThread.GetApartmentState(); diff --git a/src/WPF/MsgBox/MessageBoxEx.xaml.cs b/src/WPF/MsgBox/MessageBoxEx.xaml.cs index 26f500a..60bb754 100644 --- a/src/WPF/MsgBox/MessageBoxEx.xaml.cs +++ b/src/WPF/MsgBox/MessageBoxEx.xaml.cs @@ -48,8 +48,6 @@ namespace MsgBoxEx #endregion INotifyPropertyChanged - private const string _DEFAULT_CAPTION = "Application Message"; - #region fields private double screenHeight; @@ -357,7 +355,7 @@ namespace MsgBoxEx // configure the form based on specified criteria Message = msg; - MessageTitle = (string.IsNullOrEmpty(title.Trim())) ? _DEFAULT_CAPTION : title; + MessageTitle = (string.IsNullOrEmpty(title.Trim())) ? "Application Message" : title; // url (if specified) if (Url != null) @@ -434,6 +432,7 @@ namespace MsgBoxEx SystemSounds.Exclamation.Play(); } break; + default: MessageIcon = null; break; diff --git a/src/WPF/MsgBox/MessageBoxExStatic.cs b/src/WPF/MsgBox/MessageBoxExStatic.cs index fe31124..dacf22f 100644 --- a/src/WPF/MsgBox/MessageBoxExStatic.cs +++ b/src/WPF/MsgBox/MessageBoxExStatic.cs @@ -1,18 +1,22 @@  +// https://www.codeproject.com/Articles/5290638/Customizable-WPF-MessageBox + using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Drawing.Text; using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Interop; using System.Windows.Media; namespace MsgBoxEx { public partial class MessageBoxEx : Window, INotifyPropertyChanged { - #region static fields + #region fields private static double screenWidth = SystemParameters.WorkArea.Width - 100; @@ -21,9 +25,9 @@ namespace MsgBoxEx private static List installedFonts = new List(); public static MessageBoxButtonDefault staticButtonDefault; - #endregion static fields + #endregion fields - #region static properties + #region properties public static Color DefaultUrlForegroundColor => Colors.Blue; @@ -66,48 +70,54 @@ namespace MsgBoxEx public static string DelegateToolTip { get; set; } - #endregion static properties + #endregion properties - #region Show and ShowEx + #region methods public static MessageBoxResult OpenMessageBox( - Window owner, string msg, string title, MessageBoxButton buttons, MessageBoxImage image) + string msg, string title, MessageBoxButton buttons, MessageBoxImage image) { - //if (owner == null) - //{ - // owner = (Application.Current.MainWindow.Visibility == Visibility.Visible) ? Application.Current.MainWindow : null; - //} - - MessageBoxEx form = new MessageBoxEx(msg, title, buttons, image) /*{ Owner = owner }*/; - - form.ShowDialog(); - return form.MessageResult; + MessageBoxEx window = new MessageBoxEx(msg, title, buttons, image); + SetOwner(window); + window.ShowDialog(); + return window.MessageResult; } - public static MessageBoxResultEx OpenMessageBox(Window owner, string msg, string title, MessageBoxButtonEx buttons, MessageBoxImage image) + public static MessageBoxResultEx OpenMessageBox(string msg, string title, MessageBoxButtonEx buttons, MessageBoxImage image) { - //if (owner == null) - //{ - // owner = (Application.Current.MainWindow.Visibility == Visibility.Visible) ? Application.Current.MainWindow : null; - //} - MessageBoxEx form = new MessageBoxEx(msg, title, buttons, image) /*{ Owner = owner }*/; - form.ShowDialog(); - return form.MessageResultEx; + MessageBoxEx window = new MessageBoxEx(msg, title, buttons, image); + SetOwner(window); + window.ShowDialog(); + return window.MessageResultEx; } - #endregion Show and ShowEx + public static void SetOwner(Window window) + { + IntPtr ownerHandle = GetOwnerHandle(); - #region static configuration methods + if (ownerHandle != IntPtr.Zero) + new WindowInteropHelper(window).Owner = ownerHandle; + } + + public static IntPtr GetOwnerHandle() + { + IntPtr foregroundWindow = Native.GetForegroundWindow(); + Native.GetWindowThreadProcessId(foregroundWindow, out var procID); + + using (var proc = Process.GetCurrentProcess()) + if (proc.Id == procID) + return foregroundWindow; + + return IntPtr.Zero; + } public static Color ColorFromString(string colorString) { Color wpfColor = Colors.Black; - try - { + try { wpfColor = (Color)ColorConverter.ConvertFromString(colorString); - } - catch (Exception) { } + } catch (Exception) { } return wpfColor; } @@ -192,6 +202,6 @@ namespace MsgBoxEx staticButtonDefault = buttonDefault; } - #endregion static configuration methods + #endregion methods } } diff --git a/src/WPF/MsgBox/Native.cs b/src/WPF/MsgBox/Native.cs new file mode 100644 index 0000000..2d45a23 --- /dev/null +++ b/src/WPF/MsgBox/Native.cs @@ -0,0 +1,15 @@ + +using System; +using System.Runtime.InteropServices; + +namespace MsgBoxEx +{ + class Native + { + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); + } +} diff --git a/src/mpv.net.csproj b/src/mpv.net.csproj index a80ca3c..640c2eb 100644 --- a/src/mpv.net.csproj +++ b/src/mpv.net.csproj @@ -109,6 +109,7 @@ +