cooky451
cooky451

Reputation: 3510

WinAPI - message loop with own callback

The usual WinAPI message loop looks something like this:

MSG msg;
while (GetMessage(&msg, hwnd, 0, 0))
{
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}

Is it allowed not to call DispatchMessage() but to handle the message on your own? If not, how could I nicely approach this behavior while avoiding global variables and thread problems?

Edit: I basically want to use my own callback function, which hasn't the WndProc signature. But I can't think of a way to call that function out of a WndProc without using static or global variables. [Which would require locking, which I think isn't the best thing you can do with a callback function which probably gets called very frequently.]

Thanks for your help.

Upvotes: 3

Views: 1622

Answers (3)

high5
high5

Reputation: 471

Is it allowed not to call DispatchMessage() but to handle the message on your own? If not, how could I nicely approach this behavior while avoiding global variables and thread problems?

If you are planning to use multiple threads in your GUI then each thread that creates a window will need to manage it's own message queue.

From this page: http://msdn.microsoft.com/en-us/library/ms810439.aspx

Changes to the Message Loop

Applications with multiple threads must include a message loop in each thread that creates a window. The message loop and window procedure for a window must be processed by the thread that created the window. If the message loop does not reside in the same thread that created the window, the DispatchMessage function will not get messages for the window. As a result, the window will appear but won't show activation and won't repaint, be moved, receive mouse messages, or generally work as you expect it to.

Upvotes: 4

Martin James
Martin James

Reputation: 24847

Yes, you can handle the message yourself, if you wish. I usually set the result field to 0, but Windows only make use of this field for a few messages.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490018

You can react to a message there, but you still need/want to call DispatchMessage and actually handle the message in your normal wndproc. I'd be happy to say more about avoiding globals and/or threading problems, but it's hard to comment without more details about what you want to avoid.

Upvotes: 2

Related Questions