解决wpf popup控件遮挡其他程序的问题

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
}


知识共享许可协议
《解决wpf popup控件遮挡其他程序的问题》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     230.91ms