PopubEx.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Controls.Primitives;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Interop;
  7. using System.Windows;
  8. using System.Windows.Media;
  9. namespace Muchinfo.WPF.Controls
  10. {
  11. public class PopubEx : Popup
  12. {
  13. /// <summary>
  14. /// 是否窗口随动,默认为随动(true)
  15. /// </summary>
  16. public bool IsPositionUpdate
  17. {
  18. get { return (bool)GetValue(IsPositionUpdateProperty); }
  19. set { SetValue(IsPositionUpdateProperty, value); }
  20. }
  21. public static readonly DependencyProperty IsPositionUpdateProperty =
  22. DependencyProperty.Register("IsPositionUpdate", typeof(bool), typeof(PopubEx), new PropertyMetadata(true, new PropertyChangedCallback(IsPositionUpdateChanged)));
  23. private static void IsPositionUpdateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  24. {
  25. (d as PopubEx).pup_Loaded(d as PopubEx, null);
  26. }
  27. /// <summary>
  28. /// 加载窗口随动事件
  29. /// </summary>
  30. public PopubEx()
  31. {
  32. this.Loaded += pup_Loaded;
  33. }
  34. /// <summary>
  35. /// 加载窗口随动事件
  36. /// </summary>
  37. private void pup_Loaded(object sender, RoutedEventArgs e)
  38. {
  39. Popup pup = sender as Popup;
  40. var win = VisualTreeHelper.GetParent(pup);
  41. while (win != null && (win as Window) == null)
  42. {
  43. win = VisualTreeHelper.GetParent(win);
  44. }
  45. if ((win as Window) != null)
  46. {
  47. (win as Window).LocationChanged -= PositionChanged;
  48. if (IsPositionUpdate)
  49. (win as Window).LocationChanged += PositionChanged;
  50. }
  51. }
  52. /// <summary>
  53. /// 刷新位置
  54. /// </summary>
  55. private void PositionChanged(object sender, EventArgs e)
  56. {
  57. var method = typeof(Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
  58. if (this.IsOpen) method.Invoke(this, null);
  59. }
  60. //是否最前默认为非最前(false)
  61. public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner(typeof(Popup), new FrameworkPropertyMetadata(false, OnTopmostChanged));
  62. public bool Topmost
  63. {
  64. get { return (bool)GetValue(TopmostProperty); }
  65. set { SetValue(TopmostProperty, value); }
  66. }
  67. private static void OnTopmostChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  68. {
  69. (obj as PopubEx).UpdateWindow();
  70. }
  71. /// <summary>
  72. /// 重写拉开方法,置于非最前
  73. /// </summary>
  74. /// <param name="e"></param>
  75. protected override void OnOpened(EventArgs e)
  76. {
  77. UpdateWindow();
  78. }
  79. /// <summary>
  80. /// 刷新Popup层级
  81. /// </summary>
  82. private void UpdateWindow()
  83. {
  84. var hwnd = ((HwndSource)PresentationSource.FromVisual(this.Child)).Handle;
  85. RECT rect;
  86. if (NativeMethods.GetWindowRect(hwnd, out rect))
  87. {
  88. NativeMethods.SetWindowPos(hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, (int)this.Width, (int)this.Height, 0);
  89. }
  90. }
  91. [StructLayout(LayoutKind.Sequential)]
  92. public struct RECT
  93. {
  94. public int Left;
  95. public int Top;
  96. public int Right;
  97. public int Bottom;
  98. }
  99. #region P/Invoke imports & definitions
  100. public static class NativeMethods
  101. {
  102. [DllImport("user32.dll")]
  103. [return: MarshalAs(UnmanagedType.Bool)]
  104. internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
  105. [DllImport("user32", EntryPoint = "SetWindowPos")]
  106. internal static extern int SetWindowPos(IntPtr hWnd, int hwndInsertAfter, int x, int y, int cx, int cy, int wFlags);
  107. }
  108. #endregion
  109. }
  110. }