简体   繁体   English

何时调用 PostQuitMessage

[英]When to call PostQuitMessage

Like many applications, mine creates multiple windows.像许多应用程序一样,我创建了多个 windows。 I do not know and cannot keep track of how many windows have been created, so I don't know when there are none.我不知道也无法跟踪创建了多少 windows,所以我不知道什么时候没有。

My problem is that when all the windows are closed, unless I call PostQuitMessage somehow, the application keeps running with no windows open (obviously).我的问题是,当所有 windows 都关闭时,除非我以某种方式调用PostQuitMessage ,否则应用程序会继续运行而没有 windows 打开(显然)。 I can't call PostQuitMessage in the message handler in response to the WM_DESTROY message because that will close all the windows when the first one is closed, even if there are twenty others still open.我不能在消息处理程序中调用PostQuitMessage来响应WM_DESTROY消息,因为这将在第一个关闭时关闭所有 windows,即使还有二十个其他人仍然打开。

My question is how do I know when to call PostQuitMessage(0) to actually terminate the application?我的问题是我怎么知道何时调用PostQuitMessage(0)来实际终止应用程序?

If, for some reason, you really can't count how many windows the application opens, you can still use EnumThreadWindows() and when there are no more windows, you PostQuitMessage() .如果由于某种原因,您真的无法计算应用程序打开了多少 windows,您仍然可以使用EnumThreadWindows()并且当没有更多 windows 时,您可以使用PostQuitMessage() If you have several threads, make sure you enumerate through those too.如果您有多个线程,请确保您也枚举了这些线程。

From MSDN来自MSDN

BOOL WINAPI EnumThreadWindows(
  __in  DWORD dwThreadId,
  __in  WNDENUMPROC lpfn,
  __in  LPARAM lParam
);

Just keep a static variable with a count of the number of open windows.只需保留一个 static 变量并计算打开的 windows 的数量。 When a windows opens have it increment the counter;当 windows 打开时,让它增加计数器; in the WM_DESTROY handler decrement it.在 WM_DESTROY 处理程序中减少它。 When the count goes to zero, call PostQuitMessage.当计数变为零时,调用 PostQuitMessage。

Of course, the only clean way is keeping track of your windows and post the quitmessage if none is left.当然,唯一干净的方法是跟踪您的 windows 并发布退出消息,如果没有留下。

A possible workaround would be using one of the window enumerating functions.一种可能的解决方法是使用 window 枚举函数之一。 You can count your windows using EnumWindows() which calls a callback where you can count all toplevel windows.您可以使用 EnumWindows() 计算您的 windows,它调用回调,您可以在其中计算所有顶级 windows。 If it reaches zero, call PostQuitMessage().如果它达到零,则调用 PostQuitMessage()。

A static variable for the number of the windows?一个 static 变量为 windows 的数量? I have never seen such a Program.我从未见过这样的程序。 One the one hand child windows can open and close during the lifetime of the application.单手子 windows 可以在应用程序的生命周期内打开和关闭。 One the other hand the main window procedure has to close the application, if the user closes the main window.另一方面,如果用户关闭主 window,则主 window 程序必须关闭应用程序。 For to do this, we have to add PostQuitMessage(0) in the WM_DESTROY branch of the main window procedure.为此,我们必须在 window 主程序的 WM_DESTROY 分支中添加 PostQuitMessage(0)。 After this message, GetMessage() returns 0 and the message loop ends because of this (and the application ends too)在此消息之后,GetMessage() 返回 0,消息循环因此结束(应用程序也结束)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM