ShowSystemMenuBehavior.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Interop;
  9. using System.Runtime.InteropServices;
  10. using System.Windows.Media;
  11. using System.Windows.Input;
  12. namespace WpfStyleableWindow.StyleableWindow
  13. {
  14. public static class ShowSystemMenuBehavior
  15. {
  16. #region TargetWindow
  17. public static Window GetTargetWindow(DependencyObject obj)
  18. {
  19. return (Window)obj.GetValue(TargetWindow);
  20. }
  21. public static void SetTargetWindow(DependencyObject obj, Window window)
  22. {
  23. obj.SetValue(TargetWindow, window);
  24. }
  25. public static readonly DependencyProperty TargetWindow = DependencyProperty.RegisterAttached("TargetWindow", typeof(Window), typeof(ShowSystemMenuBehavior));
  26. #endregion
  27. #region LeftButtonShowAt
  28. public static UIElement GetLeftButtonShowAt(DependencyObject obj)
  29. {
  30. return (UIElement)obj.GetValue(LeftButtonShowAt);
  31. }
  32. public static void SetLeftButtonShowAt(DependencyObject obj, UIElement element)
  33. {
  34. obj.SetValue(LeftButtonShowAt, element);
  35. }
  36. public static readonly DependencyProperty LeftButtonShowAt = DependencyProperty.RegisterAttached("LeftButtonShowAt",
  37. typeof(UIElement), typeof(ShowSystemMenuBehavior),
  38. new UIPropertyMetadata(null, LeftButtonShowAtChanged));
  39. #endregion
  40. #region RightButtonShow
  41. public static bool GetRightButtonShow(DependencyObject obj)
  42. {
  43. return (bool)obj.GetValue(RightButtonShow);
  44. }
  45. public static void SetRightButtonShow(DependencyObject obj, bool arg)
  46. {
  47. obj.SetValue(RightButtonShow, arg);
  48. }
  49. public static readonly DependencyProperty RightButtonShow = DependencyProperty.RegisterAttached("RightButtonShow",
  50. typeof(bool), typeof(ShowSystemMenuBehavior),
  51. new UIPropertyMetadata(false, RightButtonShowChanged));
  52. #endregion
  53. #region LeftButtonShowAt
  54. static void LeftButtonShowAtChanged(object sender, DependencyPropertyChangedEventArgs e)
  55. {
  56. var element = sender as UIElement;
  57. if (element != null)
  58. {
  59. element.MouseLeftButtonDown += LeftButtonDownShow;
  60. }
  61. }
  62. static bool leftButtonToggle = true;
  63. static void LeftButtonDownShow(object sender, System.Windows.Input.MouseButtonEventArgs e)
  64. {
  65. if (leftButtonToggle)
  66. {
  67. var element = ((UIElement)sender).GetValue(LeftButtonShowAt);
  68. var showMenuAt = ((Visual)element).PointToScreen(new Point(0, 0));
  69. var targetWindow = ((UIElement)sender).GetValue(TargetWindow) as Window;
  70. SystemMenuManager.ShowMenu(targetWindow, showMenuAt);
  71. leftButtonToggle = !leftButtonToggle;
  72. }
  73. else
  74. {
  75. leftButtonToggle = !leftButtonToggle;
  76. }
  77. }
  78. #endregion
  79. #region RightButtonShow handlers
  80. private static void RightButtonShowChanged(object sender, DependencyPropertyChangedEventArgs e)
  81. {
  82. var element = sender as UIElement;
  83. if (element != null)
  84. {
  85. element.MouseRightButtonDown += RightButtonDownShow;
  86. }
  87. }
  88. static void RightButtonDownShow(object sender, System.Windows.Input.MouseButtonEventArgs e)
  89. {
  90. var element = (UIElement)sender;
  91. var targetWindow = element.GetValue(TargetWindow) as Window;
  92. var showMenuAt = targetWindow.PointToScreen(Mouse.GetPosition((targetWindow)));
  93. SystemMenuManager.ShowMenu(targetWindow, showMenuAt);
  94. }
  95. #endregion
  96. }
  97. }