如何在 Windows 中编写可以最小化到系统托盘的控制台应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/268260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:33:56  来源:igfitidea点击:

How do I write a console application in Windows that would minimize to the system tray?

c++cwindowsvisual-c++

提问by sep

I have a written a Visual C++ console application (i.e. subsystem:console) that prints useful diagnositic messages to the console.

我编写了一个 Visual C++ 控制台应用程序(即子系统:控制台),可将有用的诊断消息打印到控制台。

However, I would like to keep the application minimized most of the time, and instead of minimizing to the taskbar, appear as a nice icon on the system tray. I would also like to restore the console when the system tray icon is clicked.

但是,我希望大部分时间都将应用程序最小化,而不是最小化到任务栏,而是在系统托盘上显示为一个漂亮的图标。我还想在单击系统托盘图标时恢复控制台。

How should I change my program to do this?

我应该如何更改我的程序来做到这一点?

回答by Tamas Czinege

This is going to be an ugly hack.

这将是一个丑陋的黑客。

First, you have to retrieve the hWnd/ hInstanceof you console application. Right now, I can only come up with one way:

首先,您必须检索控制台应用程序的hWnd/ hInstance。目前,我只能想出一种方法:

  • Create a Guid with CoCreateGuid()
  • Convert it to a string
  • Set the title of the console window to this guid with SetConsoleTitle()
  • Find the hWndof the your window with the Guid as the tile with FindWindow()
  • And you can do it from the usual way from this point. See http://www.gidforums.com/t-9218.html for more info.
  • Don't forget the rename your console window to the original title once you're done.
  • 创建一个指南 CoCreateGuid()
  • 将其转换为字符串
  • 将控制台窗口的标题设置为此 guid SetConsoleTitle()
  • hWnd使用 Guid 作为磁贴找到您的窗口FindWindow()
  • 从这一点开始,您可以按照通常的方式进行操作。有关更多信息,请参阅 http://www.gidforums.com/t-9218.html。
  • 完成后不要忘记将控制台窗口重命名为原始标题。

As you can see, even though this is possible to do, it's a horrible and painful solution. Please don't do it. Please do not minimize console applications to the system tray. It is not something you are supposed to be able to do in the Windows API.

如您所见,尽管可以这样做,但这是一个可怕而痛苦的解决方案。请不要这样做。请不要将控制台应用程序最小化到系统托盘。这不是您应该能够在 Windows API 中执行的操作。

回答by Colin Pickard

You might want to write a separate gui to function as a log reader. You will then find it much easier to make this minimize to the tray. It would also let you do some other stuff you might find useful, such as changing which level of logging messages are visible on the fly.

您可能想要编写一个单独的 gui 来充当日志阅读器。然后您会发现将其最小化到托盘要容易得多。它还可以让您做一些您可能会觉得有用的其他事情,例如更改即时可见的日志消息级别。

回答by sep

To learn the console's hWnd you have two choices:

要了解控制台的 hWnd,您有两种选择:

  • On Windows 2000 or later you can use the GetConsoleWindow() function. Don't forget to define _WIN32_WINNT as 0x0500 or greater before including windows.h to have access to this function.
  • If you want to run your program on earlier Windows versions as well then you must use something like the GUID trick described above.
  • 在 Windows 2000 或更高版本上,您可以使用 GetConsoleWindow() 函数。在包含 windows.h 以访问此函数之前,不要忘记将 _WIN32_WINNT 定义为 0x0500 或更大。
  • 如果您还想在较早的 Windows 版本上运行您的程序,那么您必须使用类似上述 GUID 技巧的方法。

回答by MSalters

Probably your best bet is to create a "Message-only window"(a message queue without a visible window) to receive the Notification Area messages.

最好的办法可能是创建一个“仅消息窗口”(一个没有可见窗口的消息队列)来接收通知区域消息。

回答by MSalters

The answer with a GUID is completely ridiculous (no sense at all) The Console hWnd is of course given by GetConsoleWindow() (!)

带有 GUID 的答案是完全荒谬的(完全没有意义)控制台 hWnd 当然是由 GetConsoleWindow() (!)