SystemCommands.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Input;
  4. using System.Windows.Interop;
  5. using Standard;
  6. namespace Microsoft.Windows.Shell;
  7. public static class SystemCommands
  8. {
  9. public static RoutedCommand CloseWindowCommand { get; private set; } = new RoutedCommand("CloseWindow", typeof(SystemCommands));
  10. public static RoutedCommand MaximizeWindowCommand { get; private set; } = new RoutedCommand("MaximizeWindow", typeof(SystemCommands));
  11. public static RoutedCommand MinimizeWindowCommand { get; private set; } = new RoutedCommand("MinimizeWindow", typeof(SystemCommands));
  12. public static RoutedCommand RestoreWindowCommand { get; private set; } = new RoutedCommand("RestoreWindow", typeof(SystemCommands));
  13. public static RoutedCommand ShowSystemMenuCommand { get; private set; } = new RoutedCommand("ShowSystemMenu", typeof(SystemCommands));
  14. private static void _PostSystemCommand(Window window, SC command)
  15. {
  16. IntPtr handle = new WindowInteropHelper(window).Handle;
  17. if (handle == IntPtr.Zero || !NativeMethods.IsWindow(handle))
  18. {
  19. return;
  20. }
  21. NativeMethods.PostMessage(handle, WM.SYSCOMMAND, new IntPtr((int) command), IntPtr.Zero);
  22. }
  23. public static void CloseWindow(Window window)
  24. {
  25. Verify.IsNotNull<Window>(window, "window");
  26. SystemCommands._PostSystemCommand(window, SC.CLOSE);
  27. }
  28. public static void MaximizeWindow(Window window)
  29. {
  30. Verify.IsNotNull<Window>(window, "window");
  31. SystemCommands._PostSystemCommand(window, SC.MAXIMIZE);
  32. }
  33. public static void MinimizeWindow(Window window)
  34. {
  35. Verify.IsNotNull<Window>(window, "window");
  36. SystemCommands._PostSystemCommand(window, SC.MINIMIZE);
  37. }
  38. public static void RestoreWindow(Window window)
  39. {
  40. Verify.IsNotNull<Window>(window, "window");
  41. SystemCommands._PostSystemCommand(window, SC.RESTORE);
  42. }
  43. public static void ShowSystemMenu(Window window, Point screenLocation)
  44. {
  45. Verify.IsNotNull<Window>(window, "window");
  46. SystemCommands.ShowSystemMenuPhysicalCoordinates(window, DpiHelper.LogicalPixelsToDevice(screenLocation));
  47. }
  48. internal static void ShowSystemMenuPhysicalCoordinates(Window window, Point physicalScreenLocation)
  49. {
  50. Verify.IsNotNull<Window>(window, "window");
  51. IntPtr handle = new WindowInteropHelper(window).Handle;
  52. if (handle == IntPtr.Zero || !NativeMethods.IsWindow(handle))
  53. {
  54. return;
  55. }
  56. IntPtr systemMenu = NativeMethods.GetSystemMenu(handle, false);
  57. uint num = NativeMethods.TrackPopupMenuEx(systemMenu, 256u, (int) physicalScreenLocation.X, (int) physicalScreenLocation.Y, handle, IntPtr.Zero);
  58. if (num != 0u)
  59. {
  60. NativeMethods.PostMessage(handle, WM.SYSCOMMAND, new IntPtr((long) ((ulong) num)), IntPtr.Zero);
  61. }
  62. }
  63. }