Skip to content

Commit 727f5f1

Browse files
Ensure that Dialogs always open as Modal.
1 parent 6000f73 commit 727f5f1

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

Barnamenevis.Net.RtlMessageBox.Wpf.Demo/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
mc:Ignorable="d"
7-
Title="WPF RTL MessageBox Demo" Height="350" Width="700">
7+
Title="WPF RTL MessageBox Demo" Height="350" Width="700" WindowStartupLocation="CenterScreen">
88
<Grid Margin="12">
99
<Grid.RowDefinitions>
1010
<RowDefinition Height="Auto"/>

Barnamenevis.Net.RtlMessageBox.Wpf.Demo/MainWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ public MainWindow()
1212
RtlMessageBox.PreferredFontName = "Vazirmatn FD";
1313
RtlMessageBox.PreferredFontPointSize = 13;
1414
RtlMessageBox.ApplyCustomFont = true;
15-
RtlMessageBox.UseCustomTitleBar = true;
15+
RtlMessageBox.UseCustomTitleBar = false;
1616
}
1717

1818
// WPF RtlMessageBox demos (pure WPF)
1919
private void BtnThOk_Click(object sender, RoutedEventArgs e)
2020
{
21-
var r = RtlMessageBox.Show(this,"این یک پیام WPF است.", "پیغام");
21+
var r = RtlMessageBox.Show("این یک پیام WPF است.", "پیغام");
2222
ResultText.Text = $"Result: {r}";
2323
}
2424

Barnamenevis.Net.RtlMessageBox.Wpf/RtlMessageBox.cs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Windows;
1+
using System.Linq;
2+
using System.Windows;
23
using System.Windows.Controls;
34
using System.Windows.Input;
45
using System.Windows.Interop;
@@ -99,23 +100,35 @@ private static void PlaySystemSound(MessageBoxImage icon)
99100
}
100101
}
101102

103+
private static Window? ResolveOwner(Window? owner)
104+
{
105+
if (owner != null) return owner;
106+
var app = Application.Current;
107+
if (app == null) return null;
108+
// Prefer active window then MainWindow
109+
var active = app.Windows.OfType<Window>().FirstOrDefault(w => w.IsActive) ?? app.MainWindow;
110+
return active;
111+
}
112+
102113
private static MessageBoxResult ShowCore(Window? owner, string text, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options)
103114
{
104115
// Play system sound before showing dialog
105116
PlaySystemSound(icon);
106117

118+
var resolvedOwner = ResolveOwner(owner);
119+
107120
var window = new Window
108121
{
109122
Title = caption ?? string.Empty,
110123
ShowInTaskbar = false,
111-
WindowStartupLocation = owner != null ? WindowStartupLocation.CenterOwner : WindowStartupLocation.CenterScreen,
124+
WindowStartupLocation = resolvedOwner != null ? WindowStartupLocation.CenterOwner : WindowStartupLocation.CenterScreen,
112125
SizeToContent = SizeToContent.WidthAndHeight,
113126
ResizeMode = ResizeMode.NoResize,
114127
WindowStyle = UseCustomTitleBar ? WindowStyle.None : WindowStyle.SingleBorderWindow,
115128
FlowDirection = FlowDirection.RightToLeft,
116129
MinWidth = 300,
117130
MaxWidth = 600,
118-
Topmost = owner?.Topmost ?? false
131+
Topmost = resolvedOwner?.Topmost ?? true
119132
};
120133

121134
if (ApplyCustomFont && !string.IsNullOrWhiteSpace(PreferredFontName))
@@ -124,9 +137,9 @@ private static MessageBoxResult ShowCore(Window? owner, string text, string capt
124137
window.FontSize = PreferredFontPointSize;
125138
}
126139

127-
if (owner != null)
140+
if (resolvedOwner != null)
128141
{
129-
window.Owner = owner;
142+
window.Owner = resolvedOwner;
130143
}
131144

132145
// OUTER chrome (adds a border for WindowStyle=None)
@@ -331,7 +344,7 @@ void AddButton(string captionText, MessageBoxResult res, bool isDefault = false,
331344
var target = okOrYesButton ?? defaultButton;
332345
if (target != null)
333346
{
334-
window.Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
347+
window.Dispatcher.BeginInvoke(DispatcherPriority.Input, new System.Action(() =>
335348
{
336349
FocusManager.SetFocusedElement(window, target);
337350
Keyboard.Focus(target);
@@ -379,9 +392,9 @@ private static void ForceShowFocusCues(Window window)
379392
const int UISF_HIDEFOCUS = 0x1;
380393
const int UISF_HIDEACCEL = 0x2;
381394
var hwnd = new WindowInteropHelper(window).Handle;
382-
if (hwnd == IntPtr.Zero) return;
383-
IntPtr wParam = (IntPtr)(UIS_CLEAR | ((UISF_HIDEFOCUS | UISF_HIDEACCEL) << 16));
384-
SendMessage(hwnd, WM_CHANGEUISTATE, wParam, IntPtr.Zero);
395+
if (hwnd == System.IntPtr.Zero) return;
396+
System.IntPtr wParam = (System.IntPtr)(UIS_CLEAR | ((UISF_HIDEFOCUS | UISF_HIDEACCEL) << 16));
397+
SendMessage(hwnd, WM_CHANGEUISTATE, wParam, System.IntPtr.Zero);
385398
}
386399

387400
private static MessageBoxResult InferDefaultResult(MessageBoxButton button, MessageBoxResult defaultResult)
@@ -411,16 +424,16 @@ private static MessageBoxResult InferDefaultResult(MessageBoxButton button, Mess
411424
private const int IDI_SHIELD = 32518;
412425

413426
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
414-
private static extern IntPtr LoadImage(IntPtr hinst, IntPtr lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);
427+
private static extern System.IntPtr LoadImage(System.IntPtr hinst, System.IntPtr lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);
415428

416429
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
417-
private static extern IntPtr CopyIcon(IntPtr hIcon);
430+
private static extern System.IntPtr CopyIcon(System.IntPtr hIcon);
418431

419432
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
420-
private static extern bool DestroyIcon(IntPtr hIcon);
433+
private static extern bool DestroyIcon(System.IntPtr hIcon);
421434

422435
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
423-
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
436+
private static extern System.IntPtr SendMessage(System.IntPtr hWnd, int msg, System.IntPtr wParam, System.IntPtr lParam);
424437

425438
// Shell stock icons (modern, Fluent on recent Windows)
426439
private enum SHSTOCKICONID : uint
@@ -435,7 +448,7 @@ private enum SHSTOCKICONID : uint
435448
private struct SHSTOCKICONINFO
436449
{
437450
public uint cbSize;
438-
public IntPtr hIcon;
451+
public System.IntPtr hIcon;
439452
public int iSysImageIndex;
440453
public int iIcon;
441454
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 260)]
@@ -467,7 +480,7 @@ private struct SHSTOCKICONINFO
467480
var sii = new SHSTOCKICONINFO { cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf<SHSTOCKICONINFO>() };
468481
// large current shell size icon to get crisp visuals on modern Windows
469482
int hr = SHGetStockIconInfo(siid.Value, SHGSI_ICON | SHGSI_LARGEICON | SHGSI_SHELLICONSIZE, ref sii);
470-
if (hr == 0 && sii.hIcon != IntPtr.Zero)
483+
if (hr == 0 && sii.hIcon != System.IntPtr.Zero)
471484
{
472485
try
473486
{
@@ -493,10 +506,10 @@ private struct SHSTOCKICONINFO
493506
};
494507
if (resId == 0) return null;
495508

496-
IntPtr hIconShared = LoadImage(IntPtr.Zero, (IntPtr)resId, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
497-
if (hIconShared == IntPtr.Zero) return null;
498-
IntPtr hIcon = CopyIcon(hIconShared);
499-
if (hIcon == IntPtr.Zero) return null;
509+
System.IntPtr hIconShared = LoadImage(System.IntPtr.Zero, (System.IntPtr)resId, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
510+
if (hIconShared == System.IntPtr.Zero) return null;
511+
System.IntPtr hIcon = CopyIcon(hIconShared);
512+
if (hIcon == System.IntPtr.Zero) return null;
500513

501514
try
502515
{

0 commit comments

Comments
 (0)