CustomWnd.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Windows.Interop;
  16. namespace OpenSEMI.Ctrlib.Window
  17. {
  18. public class CustomWnd : System.Windows.Window
  19. {
  20. private HwndSource curHwndSource = null;
  21. private HwndSourceHook curHwndSourceHook = null;
  22. static CustomWnd()
  23. {
  24. DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWnd), new FrameworkPropertyMetadata(typeof(CustomWnd)));
  25. }
  26. public CustomWnd()
  27. {
  28. CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, CloseWindow));
  29. CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, MaximizeWindow, CanResizeWindow));
  30. CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, MinimizeWindow));
  31. CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, RestoreWindow, CanResizeWindow));
  32. }
  33. #region Window event
  34. private void CanResizeWindow(object sender, CanExecuteRoutedEventArgs e)
  35. {
  36. e.CanExecute = ResizeMode == ResizeMode.CanResize || ResizeMode == ResizeMode.CanResizeWithGrip;
  37. }
  38. private void RestoreWindow(object sender, ExecutedRoutedEventArgs e)
  39. {
  40. this.WindowState = System.Windows.WindowState.Normal;
  41. }
  42. private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e)
  43. {
  44. this.WindowState = System.Windows.WindowState.Minimized;
  45. }
  46. private void MaximizeWindow(object sender, ExecutedRoutedEventArgs e)
  47. {
  48. this.WindowState = System.Windows.WindowState.Maximized;
  49. }
  50. private void CloseWindow(object sender, ExecutedRoutedEventArgs e)
  51. {
  52. this.Close();
  53. }
  54. #endregion
  55. #region hook
  56. protected override void OnSourceInitialized(EventArgs e)
  57. {
  58. base.OnSourceInitialized(e);
  59. // 获取窗体句柄
  60. IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
  61. curHwndSource = HwndSource.FromHwnd(hwnd);
  62. if (curHwndSource == null)
  63. {
  64. return;
  65. }
  66. curHwndSourceHook = new HwndSourceHook(this.WndProc);
  67. curHwndSource.AddHook(curHwndSourceHook);
  68. }
  69. /// <summary>
  70. /// 系统消息处理
  71. /// </summary>
  72. /// <param name="hwnd"></param>
  73. /// <param name="msg"></param>
  74. /// <param name="wParam"></param>
  75. /// <param name="lParam"></param>
  76. /// <param name="handled"></param>
  77. /// <returns></returns>
  78. private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  79. {
  80. IntPtr result = IntPtr.Zero;
  81. int message = (int)msg;
  82. switch (message)
  83. {
  84. case 0x001A://0x001A,WM_SETTINGCHANGE
  85. break;
  86. default:
  87. handled = false;
  88. break;
  89. }
  90. return result;
  91. }
  92. #endregion
  93. }
  94. }