Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF中的处理系统关闭_Wpf_Shutdown_Wndproc_Savechanges - Fatal编程技术网

WPF中的处理系统关闭

WPF中的处理系统关闭,wpf,shutdown,wndproc,savechanges,Wpf,Shutdown,Wndproc,Savechanges,如何在WPF中重写WndProc? 当我的窗口关闭时,我尝试检查我正在使用的文件是否被修改,如果是,我必须提示用户“是否要保存更改?”消息,然后关闭正在使用的文件和窗口。但是,当我的窗口仍然打开时,我无法处理用户重新启动/关闭/注销的情况。我无法覆盖WndProc,因为我正在使用WPF进行开发。我还尝试使用。这就是我所做的 私有void loadedForm(对象发送方,RoutedEventArgs e) { 在XAML文件中,我也使用了Closing=“OnWindowClose”,但是当我

如何在WPF中重写WndProc? 当我的窗口关闭时,我尝试检查我正在使用的文件是否被修改,如果是,我必须提示用户“是否要保存更改?”消息,然后关闭正在使用的文件和窗口。但是,当我的窗口仍然打开时,我无法处理用户重新启动/关闭/注销的情况。我无法覆盖WndProc,因为我正在使用WPF进行开发。我还尝试使用。这就是我所做的 私有void loadedForm(对象发送方,RoutedEventArgs e) {

在XAML文件中,我也使用了
Closing=“OnWindowClose”
,但是当我单击“是/否”时,没有发生任何事情,我的应用程序不会关闭。如果我尝试使用“关闭”按钮再次关闭它,我会收到一个错误?为什么会这样?是因为挂钩吗?? 这在WPF中的等效值是什么

private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg==WM_QUERYENDSESSION)
    {
        systemShutdown = true;
    }

    // If this is WM_QUERYENDSESSION, the closing event should be
    // raised in the base WndProc.
    base.WndProc(m);

} //WndProc 

private void Form1_Closing(
    System.Object sender, 
    System.ComponentModel.CancelEventArgs e)
{
    if (systemShutdown)
        // Reset the variable because the user might cancel the 
        // shutdown.
    {
        systemShutdown = false;
        if (DialogResult.Yes==MessageBox.Show("My application", 
            "Do you want to save your work before logging off?", 
            MessageBoxButtons.YesNo))
        {
            SaveFile();
            e.Cancel = false;
        }
        else{
            e.Cancel = true;
        }
        CloseFile();
    }
}
为什么不使用该事件?它似乎是为实现您的愿望而设计的,您不需要直接处理windows消息


如果要取消关机,您可以在SessionEndingCancelEventArgs上将Cancel设置为true。

您可以使用
应用程序。SessionEnding
事件,但如果您需要了解有关如何测试它的更多信息,请参阅以下答案:


我相信WPF,您必须使用
SystemEvents.SessionEnding
private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg==WM_QUERYENDSESSION)
    {
        systemShutdown = true;
    }

    // If this is WM_QUERYENDSESSION, the closing event should be
    // raised in the base WndProc.
    base.WndProc(m);

} //WndProc 

private void Form1_Closing(
    System.Object sender, 
    System.ComponentModel.CancelEventArgs e)
{
    if (systemShutdown)
        // Reset the variable because the user might cancel the 
        // shutdown.
    {
        systemShutdown = false;
        if (DialogResult.Yes==MessageBox.Show("My application", 
            "Do you want to save your work before logging off?", 
            MessageBoxButtons.YesNo))
        {
            SaveFile();
            e.Cancel = false;
        }
        else{
            e.Cancel = true;
        }
        CloseFile();
    }
}