C++ 如何在C++中创建隐藏窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2122506/
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
How to create a hidden window in C++
提问by anand
How to create a hidden window ?
如何创建隐藏窗口?
The purpose of this window is to receive some messages.
这个窗口的目的是接收一些消息。
采纳答案by Nathan Osman
When you create the window, omit the WS_VISIBLE flag and don't call ShowWindow.
创建窗口时,请省略 WS_VISIBLE 标志并且不要调用 ShowWindow。
回答by Slaftos
In a win32/mfc environment what you need to do is create a class and inherit from CWnd like this:
在 win32/mfc 环境中,您需要做的是创建一个类并从 CWnd 继承,如下所示:
class HiddenMsgWindow : public CWnd
{
...
}
in the constructor of that class you would instantiate a window like this:
在该类的构造函数中,您将像这样实例化一个窗口:
HiddenMsgWindow::HiddenMsgWindow()
{
CString wcn = ::AfxRegisterWndClass(NULL);
BOOL created = this->CreateEx(0, wcn, _T("YourExcellentWindowClass"), 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
}
This gets you a hidden window with a message pump almost ready to rock and roll.
这为您提供了一个隐藏的窗口,其中包含一个几乎准备好摇滚的消息泵。
the rest of the story is to provide the linkage between the window messages and the handlers for those messages.
故事的其余部分是提供窗口消息和这些消息的处理程序之间的链接。
This is done by adding a few macros and a message handler to your implementation file (.cpp) like this:
这是通过向您的实现文件 (.cpp) 添加一些宏和消息处理程序来完成的,如下所示:
BEGIN_MESSAGE_MAP(HiddenMsgWindow, CWnd)
ON_MESSAGE(WM_USER + 1, DoNOOP)
END_MESSAGE_MAP()
LRESULT HiddenMsgWindow::DoNOOP(WPARAM wParam, LPARAM lParam)
{
AfxMessageBox(_T("Get Reaaady for a Ruuummmmmmmbllllle!"));
return LRESULT(true);
}
Then you need to fill in the rest of the glue in the header file like this:
然后你需要像这样填写头文件中剩余的胶水:
class HiddenMsgWindow : public CWnd
{
public:
HiddenMsgWindow();
protected:
afx_msg LRESULT DoNOOP(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
}
And just like magic, you have a hidden window all ready to pump your messages.
就像魔术一样,您有一个隐藏的窗口,随时可以发送消息。
In order to use this message window you would instantiate the class retrieve it's handle and send or post messages as desired. Just like this:
为了使用此消息窗口,您将实例化该类以检索它的句柄并根据需要发送或发布消息。像这样:
HiddenMsgWindow *hmw = new HiddenMsgWindow();
SendMessage(hmw->m_hWnd, WM_USER + 1, 0, 0);
回答by ta.speot.is
You can follow the instructions here: https://docs.microsoft.com/en-us/windows/desktop/winmsg/window-features#message-only-windows
您可以按照此处的说明操作:https: //docs.microsoft.com/en-us/windows/desktop/winmsg/window-features#message-only-windows
回答by Georg Fritzsche
You call CreateWindow()
or CreateWindowEx()
as usual but don't specify the WS_VISIBLE
window style. Of course ShowWindow()
should also not be called.
您像往常一样调用CreateWindow()
或CreateWindowEx()
但不指定WS_VISIBLE
窗口样式。当然ShowWindow()
也不应该叫。
回答by Deadlock
If you don't need to show the window, as some suggest create the window, omit the WS_VISIBLE flag and don't call ShowWindow.
如果您不需要显示窗口,就像有人建议创建窗口一样,省略 WS_VISIBLE 标志并且不要调用 ShowWindow。
If not when you call showWindow(), add SW_HIDEparameter.
如果在调用showWindow()时不是,请添加SW_HIDE参数。
ShowWindow(hWnd, SW_HIDE);
回答by user1228651
Ahh, Just came across an issue. If u fail to specify HWND_MESSAGE unexpected behaviours could occur. I have used NULL which is NOT correct. In my case it caused that MS Excel took 10s or more to load an xls file, while it normally takes less then a second when my app was not running!
啊,刚遇到一个问题。如果您未能指定 HWND_MESSAGE,则可能会发生意外行为。我使用了不正确的 NULL。在我的情况下,它导致 MS Excel 需要 10 秒或更长时间来加载 xls 文件,而当我的应用程序未运行时,它通常需要不到一秒钟的时间!
MSDN says this as someone mentioned before.
MSDN 之前有人提到过这一点。