码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-04-13 16:37:20      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
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
}
View Code

 

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

标签:

原文地址:http://www.cnblogs.com/qidong/p/4422346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!