views:

1033

answers:

5

How to create a hidden window ?

The purpose of this window is to receive some messages.

+3  A: 

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

Georg Fritzsche
Man, you had the exact same answer as me :)
George Edison
You both left out that you need to be a child of HWND_MESSAGE, http://msdn.microsoft.com/en-us/library/ms632599(VS.85).aspx#message_only
taspeotis
You don't *need* to (*message-only windows* doesn't imply that other window types can't do messaging), but i guess it saves overhead when you need a messaging-only window - the OP didn't specify that.
Georg Fritzsche
"The purpose of this window is to receive some messages."
taspeotis
Yep, and not "The purpose of this window is to only receive messages."
Georg Fritzsche
+2  A: 

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

George Edison
MSDN states you should also be a child window of HWND_MESSAGE, see http://msdn.microsoft.com/en-us/library/ms632599(VS.85).aspx#message_only
taspeotis
But then you don't get broadcast messages.
George Edison
+1  A: 

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);
Slaftos
I edited it for you. Take a look at the source to se how you can format various things. :] Code should be indented by four spaces, and you can do 'inline code" by surrounding the code with back-ticks: \`code\` becomes `code`.
GMan
+1 internets for HWND_MESSAGE
taspeotis
A: 

You can follow the instructions here: http://msdn.microsoft.com/en-us/library/ms632599(VS.85).aspx#message_only

taspeotis
A: 

i written in the same way, but in a dll & messages are not getting posted to the hidden window. my requirement is to post the messsages to the hidden window from the hidden window itself.

i just call start function of this hidden window then it will create & process the user defined message (this is my requirement), but not working.

Help need very urgently (ASAP)

Nani_26