user1219742
user1219742

Reputation:

How to create, handle and destory multiple windows at once?

I've heard that to create multiple windows in my C++ application, all I need to do is just make as many HWNDs as I want, and for each one, use CreateWindowEx(), along with the appropriate windowclasses, etc.
I've managed that already, so that's not the problem.

Now, normally, I'd make this kind of a messageloop for my application:

MSG msg;
while(1)
{
    while(PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    if(msg.message == WM_QUIT)
        break;
}

(That should be correct, yes?)

But now that I have 3 windows, my messageloop looks like this:

MSG msg;
while(1)
{
    while(PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) || PeekMessage(&msg, hConWnd, 0, 0, PM_REMOVE) || PeekMessage(&msg, hStatWnd, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    if(msg.message == WM_QUIT)
        break;
}

So it checks the messages for all the windows, right?

Now my question is, is this the correct way of getting messages for multiple windows?
Also, I've heard that using different WndProcs for different windows isn't good, is this true, and if so, why? (And how to use just one, if I want a bit different behavior for each window?)

And finally, how do I exit my application so that if one of the windows is closed (any window), all will be closed, appropriately?

Upvotes: 1

Views: 817

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69724

PeekMessage(&msg, NULL, ... will do what you need. msg will receive handle of window where the message will need to be dispatched, so single API call is good for all existing windows out there.

hWnd [in, optional]

A handle to the window whose messages are to be retrieved. The window must belong to the current thread.

If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.

That is, you are OK to stay with your original message pumping loop, just not limit it to specific window.

The Qs:

Now my question is, is this the correct way of getting messages for multiple windows?

A message loop, yours or someone's dispatches messages. You handle them on your window's WndProc without thinking too much who exactly delivers the messages and calls your WndProc. It works equally well for single window, and for multiple.

Also, I've heard that using different WndProcs for different windows isn't good, is this true, and if so, why? (And how to use just one, if I want a bit different behavior for each window?)

Untrue.

And finally, how do I exit my application so that if one of the windows is closed (any window), all will be closed, appropriately?

When WM_QUIT is posted in your example, the message loop breaks out. Then you are supposed to destroy windows and exit.

Upvotes: 4

Related Questions