public MainWindow() { InitializeComponent(); this.WindowState = Properties.Settings.Default.WindowState;//初始化窗体状态 Properties:配置文件的命名空间 Rect rect = Properties.Settings.Default.WindowRect; //初始化窗体位置 this.Top = rect.Top; this.Left = rect.Left; this.Width = rect.Width; this.Height = rect.Height; } private void Button_Click_1(object sender, RoutedEventArgs e)//显示坐标及大小 { MessageBox.Show(this.RestoreBounds.ToString()); //RestoreBounds窗体坐标及大小 } private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)//窗体关闭前触发 { Properties.Settings.Default.WindowState = this.WindowState;//初始化窗体状态 Properties.Settings.Default.WindowRect = this.RestoreBounds; Properties.Settings.Default.Save(); //e.Cancel = true;//不能点击关闭按钮 //隐藏之后f便不为空了,所以点击主窗体的时候小窗体已经弹出来了,但是只是隐藏了还没有关闭,所有要关闭它 if (f != null) { f.Close(); } } Window1 f; private void Button_Click_2(object sender, RoutedEventArgs e) { //加if条件是为了保证唯一实例化 if (f == null) { f = new Window1(); f.Show(); } else { f.Show(); } } private void Button_Click_3(object sender, RoutedEventArgs e) { //加if条件是为了保证唯一实例化,杜绝资源的浪费 if (f != null) { f.Hide(); } else { MessageBox.Show("请先打开窗口再执行隐藏"); } }
时间: 2024-10-18 13:40:28