Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BehaviorProperties.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 
4 using System;
5 using System.Reflection;
6 using System.Runtime.InteropServices;
7 using System.Windows;
8 using System.Windows.Controls;
9 using System.Windows.Interop;
10 
11 using SiliconStudio.Presentation.Extensions;
12 
13 namespace SiliconStudio.Presentation.Behaviors
14 {
15  /// <summary>
16  /// This static class contains attached dependency properties that can be used as behavior to add or change features of controls.
17  /// </summary>
18  public static class BehaviorProperties
19  {
20  /// <summary>
21  /// When attached to a <see cref="ScrollViewer"/> or a control that contains a <see cref="ScrollViewer"/>, this property allows to control whether the scroll viewer should handle scrolling with the mouse wheel.
22  /// </summary>
23  public static DependencyProperty HandlesMouseWheelScrollingProperty = DependencyProperty.RegisterAttached("HandlesMouseWheelScrolling", typeof(bool), typeof(BehaviorProperties), new PropertyMetadata(true, HandlesMouseWheelScrollingChanged));
24 
25  /// <summary>
26  /// When attached to a <see cref="Window"/> that have the <see cref="Window.WindowStyle"/> value set to <see cref="WindowStyle.None"/>, prevent the window to expand over the taskbar when maximized.
27  /// </summary>
28  public static DependencyProperty KeepTaskbarWhenMaximizedProperty = DependencyProperty.RegisterAttached("KeepTaskbarWhenMaximized", typeof(bool), typeof(BehaviorProperties), new PropertyMetadata(false, KeepTaskbarWhenMaximizedChanged));
29 
30  /// <summary>
31  /// Gets the current value of the <see cref="HandlesMouseWheelScrollingProperty"/> dependency property attached to the given <see cref="DependencyObject"/>.
32  /// </summary>
33  /// <param name="target">The target <see cref="DependencyObject"/>.</param>
34  /// <returns>The value of the <see cref="HandlesMouseWheelScrollingProperty"/> dependency property.</returns>
36  {
37  return (bool)target.GetValue(HandlesMouseWheelScrollingProperty);
38  }
39 
40  /// <summary>
41  /// Sets the value of the <see cref="HandlesMouseWheelScrollingProperty"/> dependency property attached to the given <see cref="DependencyObject"/>.
42  /// </summary>
43  /// <param name="target">The target <see cref="DependencyObject"/>.</param>
44  /// <param name="value">The value to set.</param>
45  public static void SetHandlesMouseWheelScrolling(DependencyObject target, bool value)
46  {
47  target.SetValue(HandlesMouseWheelScrollingProperty, value);
48  }
49 
50  /// <summary>
51  /// Gets the current value of the <see cref="KeepTaskbarWhenMaximizedProperty"/> dependency property attached to the given <see cref="DependencyObject"/>.
52  /// </summary>
53  /// <param name="target">The target <see cref="DependencyObject"/>.</param>
54  /// <returns>The value of the <see cref="KeepTaskbarWhenMaximizedProperty"/> dependency property.</returns>
55  public static bool GetKeepTaskbarWhenMaximized(DependencyObject target)
56  {
57  return (bool)target.GetValue(KeepTaskbarWhenMaximizedProperty);
58  }
59 
60  /// <summary>
61  /// Sets the value of the <see cref="KeepTaskbarWhenMaximizedProperty"/> dependency property attached to the given <see cref="DependencyObject"/>.
62  /// </summary>
63  /// <param name="target">The target <see cref="DependencyObject"/>.</param>
64  /// <param name="value">The value to set.</param>
65  public static void SetKeepTaskbarWhenMaximized(DependencyObject target, bool value)
66  {
67  target.SetValue(KeepTaskbarWhenMaximizedProperty, value);
68  }
69 
70  private static void HandlesMouseWheelScrollingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
71  {
72  var scrollViewer = d as ScrollViewer ?? d.FindVisualChildOfType<ScrollViewer>();
73 
74  if (scrollViewer != null)
75  {
76  // Yet another internal property that should be public.
77  typeof(ScrollViewer).GetProperty("HandlesMouseWheelScrolling", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(scrollViewer, e.NewValue);
78  }
79  else
80  {
81  // The framework element is not loaded yet and thus the ScrollViewer is not reachable.
82  var frameworkElement = d as FrameworkElement;
83  if (frameworkElement != null && !frameworkElement.IsLoaded)
84  {
85  // Let's delay the behavior till the scroll viewer is loaded.
86  frameworkElement.Loaded += (sender, args) =>
87  {
88  var dependencyObject = (DependencyObject)sender;
89  var loadedScrollViewer = dependencyObject.FindVisualChildOfType<ScrollViewer>();
90  if (loadedScrollViewer != null)
91  typeof(ScrollViewer).GetProperty("HandlesMouseWheelScrolling", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(loadedScrollViewer, e.NewValue);
92  };
93  }
94  }
95  }
96 
97  private static void KeepTaskbarWhenMaximizedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
98  {
99  var window = d as Window;
100  if (window != null)
101  {
102  if (window.IsLoaded)
103  {
104  var hwnd = new WindowInteropHelper(window).Handle;
105  var source = HwndSource.FromHwnd(hwnd);
106  if (source != null)
107  {
108  source.AddHook(WindowProc);
109  }
110  }
111  else
112  {
113  window.SourceInitialized += (sender, arg) =>
114  {
115  var hwnd = new WindowInteropHelper(window).Handle;
116  var source = HwndSource.FromHwnd(hwnd);
117  if (source != null)
118  {
119  source.AddHook(WindowProc);
120  }
121  };
122  }
123  }
124  }
125 
126  private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
127  {
128  switch (msg)
129  {
130  /* WM_GETMINMAXINFO */
131  case 0x0024:
132  var mmi = (NativeHelper.MINMAXINFO)Marshal.PtrToStructure(lparam, typeof(NativeHelper.MINMAXINFO));
133  var monitor = NativeHelper.MonitorFromWindow(hwnd, NativeHelper.MONITOR_DEFAULTTONEAREST);
134  if (monitor != IntPtr.Zero)
135  {
136  var monitorInfo = new NativeHelper.MONITORINFO();
137  NativeHelper.GetMonitorInfo(monitor, monitorInfo);
138  NativeHelper.RECT rcWorkArea = monitorInfo.rcWork;
139  NativeHelper.RECT rcMonitorArea = monitorInfo.rcMonitor;
140  mmi.ptMaxPosition.X = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
141  mmi.ptMaxPosition.Y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
142  mmi.ptMaxSize.X = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
143  mmi.ptMaxSize.Y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top);
144  }
145 
146  Marshal.StructureToPtr(mmi, lparam, true);
147  handled = true;
148  break;
149  }
150  return IntPtr.Zero;
151  }
152  }
153 }
static void SetHandlesMouseWheelScrolling(DependencyObject target, bool value)
Sets the value of the HandlesMouseWheelScrollingProperty dependency property attached to the given De...
static void SetKeepTaskbarWhenMaximized(DependencyObject target, bool value)
Sets the value of the KeepTaskbarWhenMaximizedProperty dependency property attached to the given Depe...
This static class contains attached dependency properties that can be used as behavior to add or chan...
static bool GetHandlesMouseWheelScrolling(DependencyObject target)
Gets the current value of the HandlesMouseWheelScrollingProperty dependency property attached to the ...
static bool GetKeepTaskbarWhenMaximized(DependencyObject target)
Gets the current value of the KeepTaskbarWhenMaximizedProperty dependency property attached to the gi...