forked from joecastro/wpf-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemCommands.cs
More file actions
91 lines (78 loc) · 3.57 KB
/
SystemCommands.cs
File metadata and controls
91 lines (78 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
namespace Microsoft.Windows.Shell
{
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using Standard;
public static class SystemCommands
{
public static RoutedCommand CloseWindowCommand { get; private set; }
public static RoutedCommand MaximizeWindowCommand { get; private set; }
public static RoutedCommand MinimizeWindowCommand { get; private set; }
public static RoutedCommand RestoreWindowCommand { get; private set; }
public static RoutedCommand ShowSystemMenuCommand { get; private set; }
static SystemCommands()
{
CloseWindowCommand = new RoutedCommand("CloseWindow", typeof(SystemCommands));
MaximizeWindowCommand = new RoutedCommand("MaximizeWindow", typeof(SystemCommands));
MinimizeWindowCommand = new RoutedCommand("MinimizeWindow", typeof(SystemCommands));
RestoreWindowCommand = new RoutedCommand("RestoreWindow", typeof(SystemCommands));
ShowSystemMenuCommand = new RoutedCommand("ShowSystemMenu", typeof(SystemCommands));
}
private static void _PostSystemCommand(Window window, SC command)
{
IntPtr hwnd = new WindowInteropHelper(window).Handle;
if (hwnd == IntPtr.Zero || !NativeMethods.IsWindow(hwnd))
{
return;
}
NativeMethods.PostMessage(hwnd, WM.SYSCOMMAND, new IntPtr((int)command), IntPtr.Zero);
}
public static void CloseWindow(Window window)
{
Verify.IsNotNull(window, "window");
_PostSystemCommand(window, SC.CLOSE);
}
public static void MaximizeWindow(Window window)
{
Verify.IsNotNull(window, "window");
_PostSystemCommand(window, SC.MAXIMIZE);
}
public static void MinimizeWindow(Window window)
{
Verify.IsNotNull(window, "window");
_PostSystemCommand(window, SC.MINIMIZE);
}
public static void RestoreWindow(Window window)
{
Verify.IsNotNull(window, "window");
_PostSystemCommand(window, SC.RESTORE);
}
/// <summary>Display the system menu at a specified location.</summary>
/// <param name="screenLocation">The location to display the system menu, in logical screen coordinates.</param>
public static void ShowSystemMenu(Window window, Point screenLocation)
{
Verify.IsNotNull(window, "window");
ShowSystemMenuPhysicalCoordinates(window, DpiHelper.LogicalPixelsToDevice(screenLocation));
}
internal static void ShowSystemMenuPhysicalCoordinates(Window window, Point physicalScreenLocation)
{
const uint TPM_RETURNCMD = 0x0100;
const uint TPM_LEFTBUTTON = 0x0;
Verify.IsNotNull(window, "window");
IntPtr hwnd = new WindowInteropHelper(window).Handle;
if (hwnd == IntPtr.Zero || !NativeMethods.IsWindow(hwnd))
{
return;
}
IntPtr hmenu = NativeMethods.GetSystemMenu(hwnd, false);
uint cmd = NativeMethods.TrackPopupMenuEx(hmenu, TPM_LEFTBUTTON | TPM_RETURNCMD, (int)physicalScreenLocation.X, (int)physicalScreenLocation.Y, hwnd, IntPtr.Zero);
if (0 != cmd)
{
NativeMethods.PostMessage(hwnd, WM.SYSCOMMAND, new IntPtr(cmd), IntPtr.Zero);
}
}
}
}