views:

330

answers:

2

HI I'm trying to create an application in the Win32 environment containing more than one window. How do i do that? all the Win32 Tutorials on web i found only showed how to manage one window. How do i create the second window, and how do i handle messages for both of them together? (i understood i'm supposed to have only one message loop)

Thankslot!

Dan

A: 

So you are going to create a child window right? You have to decide what sort of window you want to create - there are two types of them modal (also called Dialog boxes) and modeless.

Just look for CreateDialog, DialogBox and CreateWindow functions

You have to provide a wndproc for you new modal window while modeless windows use the same wndproc as main window.

Sorry if I have mistaken something, it's been very long ago I programmed raw WinAPI..

Captain Comic
Hi, Thanks for the quick reply!I added as you said a window as a child window - But i see that my WndProc only gets called once with the HWND of the parent window. My ideal would have been for WndProc to be called twice, and to have a switch in it to find out which HWND i'm dealing with. IS this possible?Do i have to create another window with its own Wndproc?Thanksalot
dan
+1  A: 

Your message loop fetches, and dispatches, messages for all windows created on the current thread.

A simple message loop like this will suffice.

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

Otherwise, to create windows on the thread, you simply call CreateWindow(...) as often as you want/need to build your UI.

Warning: A common pattern seen in many simple programs is to handle WM_DESTROY in a WindowProc to call PostQuitMessage(). PostQuitMessage() posts a message to the thread, telling the message loop to exit. This will terminate the app, destroying all the windows on the thread. If you have a main application window that, if closed, should exit the app, only that window should call PostQuitMessage. If you have two (or more) windows that can be closed in any sequence, then you need to maintain a count of open windows and call PostQuitMessage only when the last window is closed by the user.

Really, any sample program that demonstrates a dialog box is ALSO demonstrating how to create many windows on a thread, as every control on a dialog box IS also a window with a windowproc.

Many new Windows developers fall into a trap of trying to filter messages for a specific window:

  while(GetMessage(&msg,hwnd,0,0,0)...

This is wrong as it prevents any child windows getting their messages. Lots of utilitie libraries will create hidden windows on a thread and use them to receive messages from other processes / threads - filtering only messages for the app window like that will cause the message queue to grow, and other things to fail in strange an unusual ways. Pass NULL for the hwnd until you understand exactly why you might want to filter messages for a specific window for a while.

Chris Becke