123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Interop;
- using System.Runtime.InteropServices;
- using System.Windows.Media;
- using System.Windows.Input;
- namespace WpfStyleableWindow.StyleableWindow
- {
- public static class ShowSystemMenuBehavior
- {
- #region TargetWindow
- public static Window GetTargetWindow(DependencyObject obj)
- {
- return (Window)obj.GetValue(TargetWindow);
- }
- public static void SetTargetWindow(DependencyObject obj, Window window)
- {
- obj.SetValue(TargetWindow, window);
- }
- public static readonly DependencyProperty TargetWindow = DependencyProperty.RegisterAttached("TargetWindow", typeof(Window), typeof(ShowSystemMenuBehavior));
- #endregion
- #region LeftButtonShowAt
- public static UIElement GetLeftButtonShowAt(DependencyObject obj)
- {
- return (UIElement)obj.GetValue(LeftButtonShowAt);
- }
- public static void SetLeftButtonShowAt(DependencyObject obj, UIElement element)
- {
- obj.SetValue(LeftButtonShowAt, element);
- }
- public static readonly DependencyProperty LeftButtonShowAt = DependencyProperty.RegisterAttached("LeftButtonShowAt",
- typeof(UIElement), typeof(ShowSystemMenuBehavior),
- new UIPropertyMetadata(null, LeftButtonShowAtChanged));
- #endregion
- #region RightButtonShow
- public static bool GetRightButtonShow(DependencyObject obj)
- {
- return (bool)obj.GetValue(RightButtonShow);
- }
- public static void SetRightButtonShow(DependencyObject obj, bool arg)
- {
- obj.SetValue(RightButtonShow, arg);
- }
- public static readonly DependencyProperty RightButtonShow = DependencyProperty.RegisterAttached("RightButtonShow",
- typeof(bool), typeof(ShowSystemMenuBehavior),
- new UIPropertyMetadata(false, RightButtonShowChanged));
- #endregion
- #region LeftButtonShowAt
-
- static void LeftButtonShowAtChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- var element = sender as UIElement;
- if (element != null)
- {
- element.MouseLeftButtonDown += LeftButtonDownShow;
- }
- }
- static bool leftButtonToggle = true;
- static void LeftButtonDownShow(object sender, System.Windows.Input.MouseButtonEventArgs e)
- {
- if (leftButtonToggle)
- {
- var element = ((UIElement)sender).GetValue(LeftButtonShowAt);
- var showMenuAt = ((Visual)element).PointToScreen(new Point(0, 0));
- var targetWindow = ((UIElement)sender).GetValue(TargetWindow) as Window;
- SystemMenuManager.ShowMenu(targetWindow, showMenuAt);
- leftButtonToggle = !leftButtonToggle;
- }
- else
- {
- leftButtonToggle = !leftButtonToggle;
- }
- }
- #endregion
- #region RightButtonShow handlers
- private static void RightButtonShowChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- var element = sender as UIElement;
- if (element != null)
- {
- element.MouseRightButtonDown += RightButtonDownShow;
- }
- }
- static void RightButtonDownShow(object sender, System.Windows.Input.MouseButtonEventArgs e)
- {
- var element = (UIElement)sender;
- var targetWindow = element.GetValue(TargetWindow) as Window;
- var showMenuAt = targetWindow.PointToScreen(Mouse.GetPosition((targetWindow)));
- SystemMenuManager.ShowMenu(targetWindow, showMenuAt);
- }
- #endregion
- }
- }
|