1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 |
public
class
PopupNonTopmost : Popup { public
static
DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner( typeof (PopupNonTopmost), new
FrameworkPropertyMetadata( false , OnTopmostChanged)); public
bool
Topmost { get
{ return
( bool )GetValue(TopmostProperty); } set
{ SetValue(TopmostProperty, value); } } private
static
void
OnTopmostChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { (obj as
PopupNonTopmost).UpdateWindow(); } protected
override
void
OnOpened(EventArgs e) { UpdateWindow(); } private
void
UpdateWindow() { var
hwnd = ((HwndSource)PresentationSource.FromVisual( this .Child)).Handle; RECT rect; if
(GetWindowRect(hwnd, out
rect)) { SetWindowPos(hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, ( int ) this .Width, ( int ) this .Height, 0); } } #region P/Invoke imports & definitions [StructLayout(LayoutKind.Sequential)] public
struct
RECT { public
int
Left; public
int
Top; public
int
Right; public
int
Bottom; } [DllImport( "user32.dll" )] [ return : MarshalAs(UnmanagedType.Bool)] private
static
extern
bool
GetWindowRect(IntPtr hWnd, out
RECT lpRect); [DllImport( "user32" , EntryPoint = "SetWindowPos" )] private
static
extern
int
SetWindowPos(IntPtr hWnd, int
hwndInsertAfter, int
x, int
y, int
cx, int
cy, int
wFlags); #endregion } |
原文:http://www.cnblogs.com/xsi640/p/3531451.html