gpt4 book ai didi

c# - 如何检测区分外部 wm_close 与 form.close() 内部触发

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:24 24 4
gpt4 key购买 nike

这是一个 C# 应用程序,作为通知图标位于托盘中并执行其操作,直到有人右键单击它并选择关闭(菜单选项)或者它从外部应用程序或操作系统在重新启动期间获取 wm_close .

protected override void WndProc(ref Message m)
{
case Win32.WmClose:
//recvd message to shutdown
Program.Log.InfoFormat("Shutdown received at {0}", DateTime.Now);
CleanUp();
this.Close(); //this is the main form
break;

//other case statements here
}

//somewhere else on menu exit of notify icon
private void toolStripMenuItemExit_Click(object sender, EventArgs e)
{
Program.Log.InfoFormat("Manual EXIT at {0}", DateTime.Now);
CleanUp();
this.Close(); //this is the main form
}

this.close() 会触发另一个 WM_CLOSE,使应用程序陷入困惑。处理这种情况的正确方法是什么?谢谢

最佳答案

处理表单 Closing 事件。每当您想退出时,只需调用 Close();,并使任何其他操作依赖于关闭事件中的关闭,而不是在 WndProctoolStripMenuItemExit_Click 中处理它,所以:

private void OnFormCloseing(object sender, FormClosingEventArgs e)
{
string reason = string.Empty;
switch (e.CloseReason)
{
case CloseReason.UserClosing:
reason = "Manual EXIT";
break;

case CloseReason.WindowsShutDown:
reason = "Shutdown received";
break;
}
Program.Log.InfoFormat(reason + " at {0}", DateTime.Now);
CleanUp();
}

private void toolStripMenuItemExit_Click(object sender, EventArgs e)
{
this.Close(); //this is the main form
}

CloseReason 的更多成员 here .

关于c# - 如何检测区分外部 wm_close 与 form.close() 内部触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6930685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com