Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SystemCommands.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Input;
6 using System.Windows.Interop;
7 
8 using SiliconStudio.Presentation.Extensions;
9 
10 namespace SiliconStudio.Presentation.Commands
11 {
12  /// <summary>
13  /// A static class containing system commands to control a window.
14  /// </summary>
15  public static class SystemCommands
16  {
17  static SystemCommands()
18  {
19  MinimizeWindowCommand = new SystemCommand(CanMinimize, Minimize);
20  MaximizeWindowCommand = new SystemCommand(CanMaximize, Maximize);
21  RestoreWindowCommand = new SystemCommand(CanRestore, Restore);
22  CloseWindowCommand = new SystemCommand(CanClose, Close);
23  ShowSystemMenuCommand = new SystemCommand(CanShowSystemMenu, ShowSystemMenu);
24  }
25 
26  /// <summary>
27  /// Gets a command that minimizes the window passed as parameter.
28  /// </summary>
29  public static ICommand MinimizeWindowCommand { get; private set; }
30 
31  /// <summary>
32  /// Gets a command that maximizes the window passed as parameter.
33  /// </summary>
34  public static ICommand MaximizeWindowCommand { get; private set; }
35 
36  /// <summary>
37  /// Gets a command that restores the window passed as parameter.
38  /// </summary>
39  public static ICommand RestoreWindowCommand { get; private set; }
40 
41  /// <summary>
42  /// Gets a command that closes the window passed as parameter.
43  /// </summary>
44  public static ICommand CloseWindowCommand { get; private set; }
45 
46  /// <summary>
47  /// Gets a command that show the system menu of the window passed as parameter.
48  /// </summary>
49  public static ICommand ShowSystemMenuCommand { get; private set; }
50 
51  private static bool CanMinimize(Window window)
52  {
53  return HasFlag(window, NativeHelper.WS_MINIMIZEBOX);
54  }
55 
56  private static void Minimize(Window window)
57  {
58  System.Windows.SystemCommands.MinimizeWindow(window);
59  }
60 
61  private static bool CanMaximize(Window window)
62  {
63  return HasFlag(window, NativeHelper.WS_MAXIMIZEBOX);
64  }
65 
66  private static void Maximize(Window window)
67  {
68  System.Windows.SystemCommands.MaximizeWindow(window);
69  }
70 
71  private static bool CanRestore(Window window)
72  {
73  return HasFlag(window, NativeHelper.WS_MAXIMIZEBOX);
74  }
75 
76  private static void Restore(Window window)
77  {
78  System.Windows.SystemCommands.RestoreWindow(window);
79  }
80 
81  private static bool CanClose(Window window)
82  {
83  return true;
84  }
85 
86  private static void Close(Window window)
87  {
88  System.Windows.SystemCommands.CloseWindow(window);
89  }
90 
91  private static bool CanShowSystemMenu(Window window)
92  {
93  return window != null && HasFlag(window, NativeHelper.WS_SYSMENU);
94  }
95 
96  private static void ShowSystemMenu(Window window)
97  {
98  // Note: as we fetch for the content presenter, this command is a bit dependent of the window control template.
99  // But both our template and the default (Aero) seems to use one so this is probably ok.
100  var presenter = window.FindVisualChildrenOfType<ContentPresenter>().FirstOrDefault(x => Equals(x.FindVisualParentOfType<Control>(), window));
101  if (presenter == null)
102  throw new InvalidOperationException("The given window does not contain a ContentPresenter.");
103 
104  System.Windows.SystemCommands.ShowSystemMenu(window, presenter.PointToScreen(new Point(0, 0)));
105  }
106 
107  private static bool HasFlag(Window window, int flag)
108  {
109  var hwnd = new WindowInteropHelper(window).Handle;
110  bool hasFlag = (NativeHelper.GetWindowLong(hwnd, NativeHelper.GWL_STYLE) & flag) != 0;
111  return hasFlag;
112  }
113  }
114 }
A static class containing system commands to control a window.
System.Windows.Point Point
Definition: ColorPicker.cs:15