开发者

Does GetMessage need a GUI

开发者 https://www.devze.com 2023-01-31 23:59 出处:网络
Many years ago I learned that in order to use GetMessage you need some form of GUI. Without it windows don\'t create a message-queue.

Many years ago I learned that in order to use GetMessage you need some form of GUI. Without it windows don't create a message-queue.

Was I taught wrongly?

EDIT: MSDN says: Because the system directs messages to individual windows in an application, a thread must create at least one wi开发者_JAVA技巧ndow before starting its message loop.

It seems I was taught well, or is there more?


You confuse some things here.

First of all you can use GetMessage without creating live windows in your thread.

OTOH the OS doesn't create the message queue for the newly created thread by default. Hence - calls like PostThreadMessage to the newly created thread may fail.

To enforce the creation of the message queue you need to call any function that is supposed to operate on this thread's queue from this thread.

In simple words, just call PeekMessage in that thread.

More info is here: http://msdn.microsoft.com/en-us/library/ms644946%28v=vs.85%29.aspx

(see remarks)


The calls to SendMessage and PostMessage require a HWND for a parameter. If you don't have a window, you won't have a HWND, and you can't receive messages whether you have a message queue or not.

Edit: I take it back. GetMessage allows you to pass a NULL parameter for the HWND, and PostThreadMessage will post a message to the queue without an HWND. PostMessage also allows a NULL for the HWND parameter.

I learn something new every day!

0

精彩评论

暂无评论...
验证码 换一张
取 消