如何检测我的应用程序何时最小化?
我有一个程序,其中有一个选项可以最小化任务栏的通知区域。为了使其发挥作用,我需要一种可靠的方法来检测用户何时最小化应用程序。
如何在 C++ 应用程序中使用 Windows API 来做到这一点?
I have a program with an option to enable minimizing to the taskbar's notification area. In order for this to work, I need a reliable way of detecting when the user has minimized the application.
How can I do that using the Windows API in a C++ application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当用户最小化窗口时(使用标题栏上的框,或从系统菜单中选择“最小化”选项),您的应用程序将收到一个
WM_SYSCOMMAND
消息。该消息的wParam
参数将包含值SC_MINIMIZE
,该值指示所请求的系统命令的特定类型。在这种情况下,您不必关心lParam
。因此,您需要设置一个消息映射来侦听
WM_SYSCOMMAND
消息,并将wParam
设置为SC_MINIMIZE
。收到此类消息后,您应该执行代码以将应用程序最小化到任务栏通知区域,并返回 0(表示您已处理该消息)。我不确定你使用的是什么 GUI 框架。对于不同的工具包,示例代码可能看起来非常不同。以下是您可以在直接 Win32 C 应用程序中使用的内容:
When the user minimizes the window (either using the box on the title bar, or by selecting the "Minimize" option from the system menu), your application will receive a
WM_SYSCOMMAND
message. ThewParam
parameter of that message will contain the valueSC_MINIMIZE
, which indicates the particular type of system command that is being requested. In this case, you don't care about thelParam
.So you need to set up a message map that listens for a
WM_SYSCOMMAND
message with thewParam
set toSC_MINIMIZE
. Upon receipt of such a message, you should execute your code to minimize your application to the taskbar notification area, and return 0 (indicating that you've processed the message).I'm not sure what GUI framework you're using. The sample code could potentially look very different for different toolkits. Here's what you might use in a straight Win32 C application:
我认为您正在寻找 WM_SIZE。当您收到此消息时,请检查 wParam 以获取详细信息。这是 MSDN 页面。
WM_SIZE
I think you are looking for WM_SIZE. When you get this, check the wParam to get the specifics. Here is the MSDN page.
WM_SIZE
您可以检查从 GetClientRect 返回的区域大小 - 如果为零,则最小化,对我有用,但可能不适用于所有情况。
You can check the area size returned from GetClientRect - if zero it's minimised, works for me but may not work in all cases.
这就是 IsIconic应该确定,但它对我来说并不一致。 (哦,为了确定这一点的一致方法......)
That's what IsIconic is supposed to determine, but it doesn't work consistently for me. (Oh, for a consistent way to determine this...)
为了完整起见,还有 GetWindowPlacement。窗口状态显示在 WINDOWPLACEMENT 结构,如果窗口最小化,则其值为 2,即 SW_SHOWMINIMIZED。
For completeness, there's also GetWindowPlacement. The window state is revealed in the
showCmd
member of the WINDOWPLACEMENT structure, and if the window is minimized it has a value of 2, or SW_SHOWMINIMIZED.