xaml代码:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="你好" Closing="Window_Closing_1" Height="233.792" Width="329.201"> <Grid Margin="0,0,4.2,5.6"> <Grid.ColumnDefinitions> <ColumnDefinition Width="0*"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Button Content="位置" HorizontalAlignment="Left" Margin="30,30,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1" Grid.ColumnSpan="2"/> <Button Content="打开" HorizontalAlignment="Left" Margin="30,109,0,0" VerticalAlignment="Center" Width="75" Click="Button_Click_2" Grid.ColumnSpan="2"/> <Button Content="隐藏" HorizontalAlignment="Left" Margin="226,110,0,0" VerticalAlignment="Center" Width="75" Click="Button_Click_3" Grid.ColumnSpan="2"/> </Grid> </Window>
xaml.cs代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //设置窗口状态 this.WindowState = Properties.Settings.Default.MainWindowState; //设置位置 大小 Rect rect = Properties.Settings.Default.MainWindowRect; 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,获取窗口在最小化或最大化之前的大小和位置,有4个枚举值,Top、Left、Width、Height。 } private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e) { //保存当前位置、大小和状态,到配置文件 Properties.Settings.Default.MainWindowState = this.WindowState; Properties.Settings.Default.MainWindowRect = this.RestoreBounds; Properties.Settings.Default.Save(); if (n != null) { n.Close(); } } NewWindow n; private void Button_Click_2(object sender, RoutedEventArgs e) { if(n==null) { n = new NewWindow(); n.Show(); } else { n.Show(); } } private void Button_Click_3(object sender, RoutedEventArgs e) { if (n!=null) { n.Hide(); } } } }
时间: 2024-10-16 23:59:40