在 WPF 中处理系统关闭
如何在 WPF 中重写 WndProc? 当我的窗口关闭时,我尝试检查我正在使用的文件是否被修改,如果是,我必须提示用户“你想保存更改吗?” 消息,然后关闭正在使用的文件和窗口。但是,当我的窗口仍然打开时,我无法处理用户重新启动/关闭/注销的情况。我无法覆盖 WndProc,因为我正在使用 WPF 进行开发。我也尝试过使用 < a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6b97a6de-0480-4339-8ed0-cb7cdb27bd83" rel="noreferrer">此示例 MSDN 代码 .这就是我所做的 私有无效loadForm(对象发送者,RoatedEventArgs e) {
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_QUERYENDSESION.)
{
OnWindowClose(this, new CancelEventArgs())
handled = true;
shutdown = true;
}
return IntPtr.Zero;
}
private void OnWindowClose(object sender, CancelEvetArgs e)
{
if (modified)
{
//show message box
//if result is yes/no
e.cancel = false;
//if cancel
e.cancel = true;
}
}
在 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();
}
}
How can I override WndProc in WPF?
When my window close, I try to check if the file i'm using was modified, if so, I have to promt the user for "Do you want to save changes?" message, then close the file being used and the window.However, I cannot handle the case when user restarts/shutdown/logoff when my window is still open.I cannot override WndProc since I am developing using WPF.I have also tried using this sample MSDN code.This is what I did
private void loadedForm(object sender, RoutedEventArgs e)
{
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_QUERYENDSESION.)
{
OnWindowClose(this, new CancelEventArgs())
handled = true;
shutdown = true;
}
return IntPtr.Zero;
}
private void OnWindowClose(object sender, CancelEvetArgs e)
{
if (modified)
{
//show message box
//if result is yes/no
e.cancel = false;
//if cancel
e.cancel = true;
}
}
On the XAML file, I also used Closing = "OnWindowClose"
however nothing happens when I click yes/no, my application does not close. and if I try to close it again using the close button, I receive an error? why is this so? is it because of the Hook??
What is the equivalent of this in 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用 Application.SessionEnding 事件? 这似乎是为了做你想做的事情而设计的,你不需要直接处理 Windows 消息。
如果想要取消关闭,可以将 SessionEndingCancelEventArgs 上的 Cancel 设置为 true。
Why not use the Application.SessionEnding event? That seems to be designed to do what you wish and you won't need to handle the windows message directly.
You can set Cancel to true on the SessionEndingCancelEventArgs if want to cancel the shutdown.
您可以使用
Application.SessionEnding
事件,但如果您需要了解有关如何测试它的更多信息 - 请参阅以下答案:https://stackoverflow.com/a/65392006/2338477
You can use
Application.SessionEnding
event, but if you need to know more on how to test it - see following answer:https://stackoverflow.com/a/65392006/2338477