MainWindow.xaml.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Windows;
  2. using System.Windows.Input;
  3. namespace LampInspectionMachine.Views
  4. {
  5. /// <summary>
  6. /// MainWindow.xaml 的交互逻辑
  7. /// </summary>
  8. public partial class MainWindow : Window
  9. {
  10. private WindowState _windowState= WindowState.Normal;
  11. public MainWindow()
  12. {
  13. InitializeComponent();
  14. }
  15. Rect rcnormal;//定义一个全局rect记录还原状态下窗口的位置和大小。
  16. /// <summary>
  17. /// 最大化
  18. /// </summary>
  19. private void btnMaximize_Click(object sender, RoutedEventArgs e)
  20. {
  21. if ( _windowState == WindowState.Normal )
  22. {
  23. _windowState = WindowState.Maximized;
  24. rcnormal = new Rect(this.Left, this.Top, this.Width, this.Height);//保存下当前位置与大小
  25. this.Left = 0;//设置位置
  26. this.Top = 0;
  27. Rect rc = SystemParameters.WorkArea;//获取工作区大小
  28. this.Width = rc.Width;
  29. this.Height = rc.Height;
  30. }
  31. else
  32. {
  33. _windowState = WindowState.Normal;
  34. this.Left = rcnormal.Left;
  35. this.Top = rcnormal.Top;
  36. this.Width = rcnormal.Width;
  37. this.Height = rcnormal.Height;
  38. }
  39. }
  40. /// <summary>
  41. /// 还原
  42. /// </summary>
  43. private void btnNormal_Click(object sender, RoutedEventArgs e)
  44. {
  45. this.WindowState = WindowState.Minimized;
  46. }
  47. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  48. {
  49. if ( this.ActualHeight > SystemParameters.WorkArea.Height || this.ActualWidth > SystemParameters.WorkArea.Width )
  50. {
  51. this.WindowState = System.Windows.WindowState.Normal;
  52. btnMaximize_Click(null, null);
  53. }
  54. }
  55. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  56. {
  57. if ( e.ClickCount == 2 )
  58. {
  59. if ( this.ActualWidth == SystemParameters.WorkArea.Width )
  60. {
  61. btnNormal_Click(null, null);
  62. }
  63. else
  64. {
  65. btnMaximize_Click(null, null);
  66. }
  67. }
  68. }
  69. }
  70. }