zoukankan      html  css  js  c++  java
  • WPF Popup 置顶问题

    本文来自白锦文的博客,原文地址:http://blog.csdn.net/Baijinwen/archive/2011/01/22/6159043.aspx

    (ps:经验证可用)

    问题:

    使用wpf的popup,当在popup中弹出MessageBox或者打开对话框的时候,popup总是置顶,并遮住MessageBox或对话框.

    解决:

    写如下用户控件

    需导入的空间: using System.Windows.Controls.Primitives;

        using System.Runtime.InteropServices;

        using System.Windows.Interop;

     1 public class CCPopup : Popup
     2     {
     3         public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner(typeof(CCPopup), new FrameworkPropertyMetadata(false, OnTopmostChanged));
     4         public bool Topmost
     5         {
     6             get { return (bool)GetValue(TopmostProperty); }
     7             set { SetValue(TopmostProperty, value); }
     8         }
     9         private static void OnTopmostChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    10         {
    11             (obj as CCPopup).UpdateWindow();
    12         }
    13         protected override void OnOpened(EventArgs e)
    14         {
    15             UpdateWindow();
    16         }
    17         private void UpdateWindow()
    18         {
    19             var hwnd = ((HwndSource)PresentationSource.FromVisual(this.Child)).Handle;
    20             RECT rect;
    21             if (GetWindowRect(hwnd, out rect))
    22             {
    23                 SetWindowPos(hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, (int)this.Width, (int)this.Height, 0);
    24             }
    25         }
    26         #region P/Invoke imports & definitions
    27         [StructLayout(LayoutKind.Sequential)]
    28         public struct RECT
    29         {
    30             public int Left;
    31             public int Top;
    32             public int Right;
    33             public int Bottom;
    34         }
    35         [DllImport("user32.dll")]
    36         [return: MarshalAs(UnmanagedType.Bool)]
    37         private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    38         [DllImport("user32", EntryPoint = "SetWindowPos")]
    39         private static extern int SetWindowPos(IntPtr hWnd, int hwndInsertAfter, int x, int y, int cx, int cy, int wFlags);
    40         #endregion
    41     }
  • 相关阅读:
    windows xp查看缩略图时有缩略图没有文件名
    数据库的相关操作
    使用timer控件创建一个简单的报警程序
    xp_sendmail的正确配置与使用
    SQL Server 索引结构及其使用(三)
    启动与关闭服务器
    不间断连续图片滚动效果的制作方法
    使用C#调用外部Ping命令获取网络连接情况
    SQL Server 索引结构及其使用(一)
    winform 与asp.net 下拉列表的区别
  • 原文地址:https://www.cnblogs.com/MarcLiu/p/3688134.html
Copyright © 2011-2022 走看看