XAML:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="360" Height="240" Closing="Window_Closing"></Window>
CS 代码:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult result = MessageBox.Show("确定是退出吗?", "询问", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResu if (result == MessageBoxResult.Yes)
{
e.Cancel = false;
Environment.Exit(0);//可以立即中断程序执行并退出
//这个方法用起来的感觉类似在任务管理器里找到进程并终止,即立刻关闭进程,不管该进程目前有没有工作,在做什么工作。
//在WPF中调用该方法即立即退出,不会等待当前工作完成。
}lt.Yes) //关闭窗口
if (result == MessageBoxResult.No) //不关闭窗口
e.Cancel = true;
}
官方例子CS:
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//读取配置文件
try
{
//设置位置、大小
Rect restoreBounds = Properties.Settings.Default.MainRestoreBounds;
this.WindowState = WindowState.Normal;
this.Left = restoreBounds.Left;
this.Top = restoreBounds.Top;
this.Width = restoreBounds.Width;
this.Height = restoreBounds.Height;
//设置窗口状态
this.WindowState = Properties.Settings.Default.MainWindowState;
}
catch { }
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//保存当前位置、大小和状态,到配置文件
Properties.Settings.Default.MainRestoreBounds = this.RestoreBounds;
Properties.Settings.Default.MainWindowState = this.WindowState;
Properties.Settings.Default.Save();
}
}
}