#318 Fix message box not working when ontop is enabled

This commit is contained in:
Frank Skare
2021-09-04 12:39:28 +02:00
parent 0d72731ce7
commit 10e2a2cf3b
6 changed files with 61 additions and 35 deletions

View File

@@ -3,6 +3,7 @@
- All PowerShell dependencies except the scipt host were removed - All PowerShell dependencies except the scipt host were removed
in order to achieve first class Windows 7 compatibility! 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) 5.4.9.7 Beta (2021-08-28)

View File

@@ -44,7 +44,7 @@ public class Msg
string msg = title?.ToString().TrimEx(); string msg = title?.ToString().TrimEx();
MessageBoxEx.DetailsText = details; MessageBoxEx.DetailsText = details;
string windowTitle = System.Windows.Forms.Application.ProductName; 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(); ApartmentState state = Thread.CurrentThread.GetApartmentState();

View File

@@ -48,8 +48,6 @@ namespace MsgBoxEx
#endregion INotifyPropertyChanged #endregion INotifyPropertyChanged
private const string _DEFAULT_CAPTION = "Application Message";
#region fields #region fields
private double screenHeight; private double screenHeight;
@@ -357,7 +355,7 @@ namespace MsgBoxEx
// configure the form based on specified criteria // configure the form based on specified criteria
Message = msg; Message = msg;
MessageTitle = (string.IsNullOrEmpty(title.Trim())) ? _DEFAULT_CAPTION : title; MessageTitle = (string.IsNullOrEmpty(title.Trim())) ? "Application Message" : title;
// url (if specified) // url (if specified)
if (Url != null) if (Url != null)
@@ -434,6 +432,7 @@ namespace MsgBoxEx
SystemSounds.Exclamation.Play(); SystemSounds.Exclamation.Play();
} }
break; break;
default: default:
MessageIcon = null; MessageIcon = null;
break; break;

View File

@@ -1,18 +1,22 @@
 
// https://www.codeproject.com/Articles/5290638/Customizable-WPF-MessageBox
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Text; using System.Drawing.Text;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media; using System.Windows.Media;
namespace MsgBoxEx namespace MsgBoxEx
{ {
public partial class MessageBoxEx : Window, INotifyPropertyChanged public partial class MessageBoxEx : Window, INotifyPropertyChanged
{ {
#region static fields #region fields
private static double screenWidth = SystemParameters.WorkArea.Width - 100; private static double screenWidth = SystemParameters.WorkArea.Width - 100;
@@ -21,9 +25,9 @@ namespace MsgBoxEx
private static List<string> installedFonts = new List<string>(); private static List<string> installedFonts = new List<string>();
public static MessageBoxButtonDefault staticButtonDefault; public static MessageBoxButtonDefault staticButtonDefault;
#endregion static fields #endregion fields
#region static properties #region properties
public static Color DefaultUrlForegroundColor => Colors.Blue; public static Color DefaultUrlForegroundColor => Colors.Blue;
@@ -66,48 +70,54 @@ namespace MsgBoxEx
public static string DelegateToolTip { get; set; } public static string DelegateToolTip { get; set; }
#endregion static properties #endregion properties
#region Show and ShowEx #region methods
public static MessageBoxResult OpenMessageBox( 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) MessageBoxEx window = new MessageBoxEx(msg, title, buttons, image);
//{ SetOwner(window);
// owner = (Application.Current.MainWindow.Visibility == Visibility.Visible) ? Application.Current.MainWindow : null; window.ShowDialog();
//} return window.MessageResult;
MessageBoxEx form = new MessageBoxEx(msg, title, buttons, image) /*{ Owner = owner }*/;
form.ShowDialog();
return form.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) MessageBoxEx window = new MessageBoxEx(msg, title, buttons, image);
//{ SetOwner(window);
// owner = (Application.Current.MainWindow.Visibility == Visibility.Visible) ? Application.Current.MainWindow : null; window.ShowDialog();
//} return window.MessageResultEx;
MessageBoxEx form = new MessageBoxEx(msg, title, buttons, image) /*{ Owner = owner }*/;
form.ShowDialog();
return form.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) public static Color ColorFromString(string colorString)
{ {
Color wpfColor = Colors.Black; Color wpfColor = Colors.Black;
try try {
{
wpfColor = (Color)ColorConverter.ConvertFromString(colorString); wpfColor = (Color)ColorConverter.ConvertFromString(colorString);
} } catch (Exception) { }
catch (Exception) { }
return wpfColor; return wpfColor;
} }
@@ -192,6 +202,6 @@ namespace MsgBoxEx
staticButtonDefault = buttonDefault; staticButtonDefault = buttonDefault;
} }
#endregion static configuration methods #endregion methods
} }
} }

15
src/WPF/MsgBox/Native.cs Normal file
View File

@@ -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);
}
}

View File

@@ -109,6 +109,7 @@
<Compile Include="WPF\MsgBox\MsgBoxExtendedFunctionality.cs" /> <Compile Include="WPF\MsgBox\MsgBoxExtendedFunctionality.cs" />
<Compile Include="WPF\MsgBox\MsgBoxUrl.cs" /> <Compile Include="WPF\MsgBox\MsgBoxUrl.cs" />
<Compile Include="WPF\MsgBox\MsgEnumerators.cs" /> <Compile Include="WPF\MsgBox\MsgEnumerators.cs" />
<Compile Include="WPF\MsgBox\Native.cs" />
<Compile Include="WPF\RelayCommand.cs" /> <Compile Include="WPF\RelayCommand.cs" />
<Compile Include="Misc\CSharpScriptHost.cs" /> <Compile Include="Misc\CSharpScriptHost.cs" />
<Compile Include="Misc\Extension.cs" /> <Compile Include="Misc\Extension.cs" />