Skip to content

Commit e67bc88

Browse files
authored
Merge pull request #597 from uk-taniyama/pr-wpf-fix-z-order
fix WPF Popup Z-Index.
2 parents 5fe7ac5 + 26e49a2 commit e67bc88

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Rg.Plugins.Popup/Platforms/Wpf/Renderers/PopupPageRenderer.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.Runtime.InteropServices;
23
using System.Windows;
34
using System.Windows.Controls.Primitives;
5+
using System.Windows.Interop;
6+
using System.Windows.Media;
47
using Rg.Plugins.Popup.Pages;
58
using Rg.Plugins.Popup.WPF.Renderers;
69
using Xamarin.Forms;
@@ -26,25 +29,41 @@ internal void Prepare(WinPopup container)
2629

2730
if (Application.Current.MainWindow != null)
2831
Application.Current.MainWindow.SizeChanged += OnSizeChanged;
32+
if (Application.Current.MainWindow != null)
33+
Application.Current.MainWindow.Activated += OnActivated;
2934

3035
UpdateElementSize();
3136
CurrentElement.CloseWhenBackgroundIsClicked = true;
3237
Container.AllowsTransparency = true;
3338
Container.MouseDown += Container_MouseDown;
39+
Container.Opened += Container_Opened;
40+
}
41+
42+
private void OnActivated(object sender, EventArgs e)
43+
{
44+
UpdateZOrder();
3445
}
3546

3647
private void Container_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
3748
{
3849
Container.IsOpen = false;
3950
}
4051

52+
private void Container_Opened(object sender, EventArgs e)
53+
{
54+
UpdateZOrder();
55+
}
56+
4157
internal void Destroy()
4258
{
4359
Container.MouseDown -= Container_MouseDown;
60+
Container.Opened -= Container_Opened;
4461
Container = null;
4562

4663
if (Application.Current.MainWindow != null)
4764
Application.Current.MainWindow.SizeChanged -= OnSizeChanged;
65+
if (Application.Current.MainWindow != null)
66+
Application.Current.MainWindow.Activated -= OnActivated;
4867
}
4968

5069
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
@@ -79,5 +98,28 @@ private void UpdateElementSize()
7998
Container.VerticalOffset = rectangle.Y;
8099
Container.HorizontalOffset = rectangle.X;
81100
}
101+
102+
private void UpdateZOrder()
103+
{
104+
_ = SetWindowPos(GetHwnd(Container.Child), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
105+
}
106+
107+
private static IntPtr GetHwnd(Visual visual)
108+
{
109+
HwndSource hwndSource = ((HwndSource)PresentationSource.FromVisual(visual));
110+
if(hwndSource==null)
111+
{
112+
return IntPtr.Zero;
113+
}
114+
return hwndSource.Handle;
115+
}
116+
117+
private const int HWND_NOTOPMOST = -2;
118+
private const int SWP_NOMOVE = 2;
119+
private const int SWP_NOSIZE = 1;
120+
121+
[DllImport("user32", EntryPoint = "SetWindowPos")]
122+
private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
123+
82124
}
83125
}

0 commit comments

Comments
 (0)