CustomWnd.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Input;
  4. using System.Windows.Interop;
  5. namespace OpenSEMI.Ctrlib.Window
  6. {
  7. public class CustomWnd : System.Windows.Window
  8. {
  9. private HwndSource curHwndSource = null;
  10. private HwndSourceHook curHwndSourceHook = null;
  11. static CustomWnd()
  12. {
  13. FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWnd), new FrameworkPropertyMetadata(typeof(CustomWnd)));
  14. }
  15. public CustomWnd()
  16. {
  17. base.CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, CloseWindow));
  18. base.CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, MaximizeWindow, CanResizeWindow));
  19. base.CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, MinimizeWindow));
  20. base.CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, RestoreWindow, CanResizeWindow));
  21. }
  22. private void CanResizeWindow(object sender, CanExecuteRoutedEventArgs e)
  23. {
  24. e.CanExecute = (base.ResizeMode == ResizeMode.CanResize || base.ResizeMode == ResizeMode.CanResizeWithGrip);
  25. }
  26. private void RestoreWindow(object sender, ExecutedRoutedEventArgs e)
  27. {
  28. base.WindowState = WindowState.Normal;
  29. }
  30. private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e)
  31. {
  32. base.WindowState = WindowState.Minimized;
  33. }
  34. private void MaximizeWindow(object sender, ExecutedRoutedEventArgs e)
  35. {
  36. base.WindowState = WindowState.Maximized;
  37. }
  38. private void CloseWindow(object sender, ExecutedRoutedEventArgs e)
  39. {
  40. Close();
  41. }
  42. protected override void OnSourceInitialized(EventArgs e)
  43. {
  44. base.OnSourceInitialized(e);
  45. IntPtr handle = new WindowInteropHelper(this).Handle;
  46. curHwndSource = HwndSource.FromHwnd(handle);
  47. if (curHwndSource != null)
  48. {
  49. curHwndSourceHook = this.WndProc;
  50. curHwndSource.AddHook(curHwndSourceHook);
  51. }
  52. }
  53. private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  54. {
  55. IntPtr zero = IntPtr.Zero;
  56. if (msg != 26)
  57. {
  58. handled = false;
  59. }
  60. return zero;
  61. }
  62. }
  63. }