using System; using System.Windows; using System.Windows.Input; using System.Windows.Interop; namespace OpenSEMI.Ctrlib.Window { public class CustomWnd : System.Windows.Window { private HwndSource curHwndSource = null; private HwndSourceHook curHwndSourceHook = null; static CustomWnd() { FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWnd), new FrameworkPropertyMetadata(typeof(CustomWnd))); } public CustomWnd() { base.CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, CloseWindow)); base.CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, MaximizeWindow, CanResizeWindow)); base.CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, MinimizeWindow)); base.CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, RestoreWindow, CanResizeWindow)); } private void CanResizeWindow(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = (base.ResizeMode == ResizeMode.CanResize || base.ResizeMode == ResizeMode.CanResizeWithGrip); } private void RestoreWindow(object sender, ExecutedRoutedEventArgs e) { base.WindowState = WindowState.Normal; } private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e) { base.WindowState = WindowState.Minimized; } private void MaximizeWindow(object sender, ExecutedRoutedEventArgs e) { base.WindowState = WindowState.Maximized; } private void CloseWindow(object sender, ExecutedRoutedEventArgs e) { Close(); } protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); IntPtr handle = new WindowInteropHelper(this).Handle; curHwndSource = HwndSource.FromHwnd(handle); if (curHwndSource != null) { curHwndSourceHook = this.WndProc; curHwndSource.AddHook(curHwndSourceHook); } } private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { IntPtr zero = IntPtr.Zero; if (msg != 26) { handled = false; } return zero; } } }