using System.Windows; using System.Windows.Input; namespace LampInspectionMachine.Views { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { private WindowState _windowState= WindowState.Normal; public MainWindow() { InitializeComponent(); } Rect rcnormal;//定义一个全局rect记录还原状态下窗口的位置和大小。 /// /// 最大化 /// private void btnMaximize_Click(object sender, RoutedEventArgs e) { if ( _windowState == WindowState.Normal ) { _windowState = WindowState.Maximized; rcnormal = new Rect(this.Left, this.Top, this.Width, this.Height);//保存下当前位置与大小 this.Left = 0;//设置位置 this.Top = 0; Rect rc = SystemParameters.WorkArea;//获取工作区大小 this.Width = rc.Width; this.Height = rc.Height; } else { _windowState = WindowState.Normal; this.Left = rcnormal.Left; this.Top = rcnormal.Top; this.Width = rcnormal.Width; this.Height = rcnormal.Height; } } /// /// 还原 /// private void btnNormal_Click(object sender, RoutedEventArgs e) { this.WindowState = WindowState.Minimized; } private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { if ( this.ActualHeight > SystemParameters.WorkArea.Height || this.ActualWidth > SystemParameters.WorkArea.Width ) { this.WindowState = System.Windows.WindowState.Normal; btnMaximize_Click(null, null); } } private void Grid_MouseDown(object sender, MouseButtonEventArgs e) { if ( e.ClickCount == 2 ) { if ( this.ActualWidth == SystemParameters.WorkArea.Width ) { btnNormal_Click(null, null); } else { btnMaximize_Click(null, null); } } } } }