views:

121

answers:

3

I see plenty of tutorials and articles showing me how to make a simple windows program, which is great but none of them show me how to make multiple windows.

Right now I have working code that creates and draws a layered window and I can blit stuff using GDI to draw anything I want on it, drag it around, even make it transparent, etc.

But I wanted a second rectangular area that I can draw to, drag around, etc. In other words, a second window. Probably want it to be a child window. Question is, how do I make it?

Also, if anybody knows any good resources (online preferably) like articles or tutorials for window management in the Windows API, please share.

+1  A: 

It sounds like you want a Multiple Document Interface. Here is an example of one:

http://www.codeproject.com/KB/winsdk/Sigma.aspx

Robert Harvey
+1  A: 

You can hit CreateWindow() more than once if you want. The message loop in your WinMain will pass events to all the windows that WinMain creates. You can even create two overlapped windows and set the parent window of the 2nd one to be the handle of the 1st one if you want.

JustJeff
I got it working, finally. It worked when I modified my routine that creates windows by allowing it to register a window class using a different ClassName string. Why would I need to make a separate window class for the second window if the functionality should stay the same?
Steven Lu
@Steven Lu: not sure. The test I ran to double check only registered one window class and was able to CreateWindow twice. Of course, this means they share a WndProc. You don't have static data inside the WndProc, do you?
JustJeff
I was still using the same window proc, but it had to be a different window class, i.e. I had to call `RegisterClassEx` with a different `WNDCLASSEX::lpszClassName`. Is this normal?
Steven Lu
@Steven Lu - no, you should be able to create several instances of the same class, unless there's something about the class that is static so that the different instances are stepping on one another.
JustJeff
+1  A: 

You can create as many windows as you want using CreateWindow/CreateWindowEx, with the relationship between them as you desire (owner/child).

You can make a window "owned" by other with:

SetWindowLongPtr(hwnd, GWLP_HWNDPARENT, (LONG_PTR) hwndParent);

To convert a window to child, use SetParent.

Note that the SetWindowLongPtr call with GWLP_HWNDPARENT does not behave as SetParent (MSDN is wrong on this I think). GWLP_HWNDPARENT does not convert a window to "child", but to "owned".

Hernán