Public classpopupnontopmost:popup{ Public StaticDependencyProperty Topmostproperty =Window.TopmostProperty.AddOwner (typeof(popupnontopmost),NewFrameworkpropertymetadata (false, ontopmostchanged)); Public BOOLTopmost {Get{return(BOOL) GetValue (Topmostproperty); } Set{SetValue (topmostproperty, value);} } Private Static voidontopmostchanged (DependencyObject obj, DependencyPropertyChangedEventArgs e) {(obj aspopupnontopmost). UpdateWindow (); } protected Override voidonopened (EventArgs e) {UpdateWindow (); } Private voidUpdateWindow () {varhwnd = ((HwndSource) presentationsource.fromvisual ( This. Child)). Handle; Rect rect; if(GetWindowRect (hwnd, outrect)) {SetWindowPos (hwnd, topmost? -1: -2, Rect. Left, rect. Top, (int) This. Width, (int) This. Height,0 ); } } #regionP/invoke Imports & Definitions[StructLayout (layoutkind.sequential)] Public structRECT { Public intLeft ; Public intTop; Public intRight ; Public intBottom; } [DllImport ("user32.dll" )] [return: MarshalAs (unmanagedtype.bool)]Private Static extern BOOLGetWindowRect (IntPtr hWnd, outRECT LpRect); [DllImport ("User32", EntryPoint ="SetWindowPos" )] Private Static extern intSetWindowPos (IntPtr hWnd,intHwndinsertafter,intXintYintCxintCyintwflags); #endregion}
View Code
Troubleshooting WPF Popup Controls blocking other programs