引自:http://technet.microsoft.com/zh-cn/magazine/bb613596(VS.90).aspx

   http://www.th7.cn/Program/WPF/201307/142902.shtml

  http://www.th7.cn/Program/WPF/201307/141820.shtml

1、Popup弹出的位置

  相关属性:Placement,PlacementTarget。具体看http://technet.microsoft.com/zh-cn/magazine/bb613596(VS.90).aspx

2、popup始终置顶的问题

  只要设置Popup的StayOpen为true,则popup始终显示在桌面的最顶层,解决方法参考:http://www.th7.cn/Program/WPF/201307/141820.shtml,自定义CustomPopup

public class CustomPopup : Popup
    {
        public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner(typeof(CustomPopup), 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 CustomPopup).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

相关文章: