How to create a hidden window in C++

北城以北 提交于 2020-01-11 17:12:33

问题


How to create a hidden window ?

The purpose of this window is to receive some messages.


回答1:


When you create the window, omit the WS_VISIBLE flag and don't call ShowWindow.




回答2:


In a win32/mfc environment what you need to do is create a class and inherit from CWnd like this:

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:

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);



回答3:


You can follow the instructions here: https://docs.microsoft.com/en-us/windows/desktop/winmsg/window-features#message-only-windows




回答4:


You call CreateWindow() or CreateWindowEx() as usual but don't specify the WS_VISIBLE window style. Of course ShowWindow() should also not be called.




回答5:


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.

If not when you call showWindow(), add SW_HIDE parameter.

ShowWindow(hWnd, SW_HIDE);



回答6:


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!

MSDN says this as someone mentioned before.



来源:https://stackoverflow.com/questions/2122506/how-to-create-a-hidden-window-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!