SystemCommands.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. namespace Microsoft.Windows.Shell
  11. {
  12. using System;
  13. using System.Windows;
  14. using System.Windows.Input;
  15. using System.Windows.Interop;
  16. using Standard;
  17. public static class SystemCommands
  18. {
  19. public static RoutedCommand CloseWindowCommand { get; private set; }
  20. public static RoutedCommand MaximizeWindowCommand { get; private set; }
  21. public static RoutedCommand MinimizeWindowCommand { get; private set; }
  22. public static RoutedCommand RestoreWindowCommand { get; private set; }
  23. public static RoutedCommand ShowSystemMenuCommand { get; private set; }
  24. static SystemCommands()
  25. {
  26. CloseWindowCommand = new RoutedCommand("CloseWindow", typeof(SystemCommands));
  27. MaximizeWindowCommand = new RoutedCommand("MaximizeWindow", typeof(SystemCommands));
  28. MinimizeWindowCommand = new RoutedCommand("MinimizeWindow", typeof(SystemCommands));
  29. RestoreWindowCommand = new RoutedCommand("RestoreWindow", typeof(SystemCommands));
  30. ShowSystemMenuCommand = new RoutedCommand("ShowSystemMenu", typeof(SystemCommands));
  31. }
  32. private static void _PostSystemCommand(Window window, SC command)
  33. {
  34. IntPtr hwnd = new WindowInteropHelper(window).Handle;
  35. if (hwnd == IntPtr.Zero || !NativeMethods.IsWindow(hwnd))
  36. {
  37. return;
  38. }
  39. NativeMethods.PostMessage(hwnd, WM.SYSCOMMAND, new IntPtr((int)command), IntPtr.Zero);
  40. }
  41. public static void CloseWindow(Window window)
  42. {
  43. Verify.IsNotNull(window, "window");
  44. _PostSystemCommand(window, SC.CLOSE);
  45. }
  46. public static void MaximizeWindow(Window window)
  47. {
  48. Verify.IsNotNull(window, "window");
  49. _PostSystemCommand(window, SC.MAXIMIZE);
  50. }
  51. public static void MinimizeWindow(Window window)
  52. {
  53. Verify.IsNotNull(window, "window");
  54. _PostSystemCommand(window, SC.MINIMIZE);
  55. }
  56. public static void RestoreWindow(Window window)
  57. {
  58. Verify.IsNotNull(window, "window");
  59. _PostSystemCommand(window, SC.RESTORE);
  60. }
  61. /// <summary>Display the system menu at a specified location.</summary>
  62. /// <param name="screenLocation">The location to display the system menu, in logical screen coordinates.</param>
  63. public static void ShowSystemMenu(Window window, Point screenLocation)
  64. {
  65. Verify.IsNotNull(window, "window");
  66. ShowSystemMenuPhysicalCoordinates(window, DpiHelper.LogicalPixelsToDevice(screenLocation));
  67. }
  68. internal static void ShowSystemMenuPhysicalCoordinates(Window window, Point physicalScreenLocation)
  69. {
  70. const uint TPM_RETURNCMD = 0x0100;
  71. const uint TPM_LEFTBUTTON = 0x0;
  72. Verify.IsNotNull(window, "window");
  73. IntPtr hwnd = new WindowInteropHelper(window).Handle;
  74. if (hwnd == IntPtr.Zero || !NativeMethods.IsWindow(hwnd))
  75. {
  76. return;
  77. }
  78. IntPtr hmenu = NativeMethods.GetSystemMenu(hwnd, false);
  79. uint cmd = NativeMethods.TrackPopupMenuEx(hmenu, TPM_LEFTBUTTON | TPM_RETURNCMD, (int)physicalScreenLocation.X, (int)physicalScreenLocation.Y, hwnd, IntPtr.Zero);
  80. if (0 != cmd)
  81. {
  82. NativeMethods.PostMessage(hwnd, WM.SYSCOMMAND, new IntPtr(cmd), IntPtr.Zero);
  83. }
  84. }
  85. }
  86. }