English 中文(简体)
Handling multiple windows WIN32 API
原标题:

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

问题回答

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,NULL,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)...

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.

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..





相关问题
How to read exact number of bytes from a stream (tcp) socket?

In winsock, both the sync recv and the async WSARecv complete as soon as there is data available in a stream socket, regardless of the size specified (which is only the upper limit). This means that ...

AcquireCredentialsHandle returns SEC_E_NO_CREDENTIALS

I created a self-signed certificate (created using OpenSSL) and installed it into the Certificate Store using the Certificates MMC snap-in (CertMgr.msc) on Windows Vista Ultimate. I have managed to ...

Calling Win32 EnumThreadWindows() in C#

I m trying to get a call to EnumThreadWindows working, but I always get a Wrong Parameter-Error, although my code is nearly the same as this example on pinvoke.net. I don t know why this doesn t work: ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

Handling multiple windows WIN32 API

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 ...

Creating a thread in DllMain?

It seems that when a thread is created from within DllMain upon DLL_PROCESS_ATTACH it won t begin until all dll s have been loaded. Since I need to make sure the thread runs before I continue, I get a ...

热门标签