How you do this

Started by
3 comments, last by password 18 years, 5 months ago
I've made a basic windows application with the win api. I'm reading the book "Tricks of the window programming gurus" and came to the chapter where they explain how to open multiple windows. It's fairly easy, you just need to call CreateWindow twice with the same window class. The exercise in the chap was to open two windows and be able to close on of the windows without both being closed at the same time (that's what happens now). The hint said that I should create two window classes, I understand that so far. I don't really understand what they mean with "don’t send a WM_QUIT message until both windows have been closed" is the WM_QUIT message the PostQuitMessage(0); line? Actually I solved the problem by just deleting that line. What purpouse does the PostQuitMessage(0) really have then? in this case it was just a nuisance and everything worked without that function. One last thing, what's the difference between WM_DESTROY and WM_QUIT? they didn't even use the WM_QUIT in the switch statement so I don't really know why they brought that up and especially when i'm not even using it except when checking the message queen for emptiness :) Anyways, clear up my mind, hate to be confused, can't believe something easy like this made me post here. By the way, I really recommend this book for all beginners. It's a good introduction to the win api and covering some interesting stuff, so far this is the best win api book i've read.
Advertisement
In that book, when WM_QUIT is called, the program exits. If it is not called, then the program keeps running, just without a window.

What I suggest you do is use a counter variable to keep track of the number of windows open. When you create a window (call WM_CREATE), you increment it. When you destroy a window (call WM_DESTROY), you deincrement it. When the counter = 0, the program exits. That's how I would do it, if I had to open up multiple windows.
Yeah, I noticed now that all the windows were in the process list. I got it to work now by doing what you said, thanks. Posting how I did for those who wants to know:

In WM_DESTROY:

if (counter==0) {
PostQuitMessage(0);
} else {
--counter;
}


In WM_CREATE:

counter++;

EDIT:
this is really strange, did I do something wrong? everytime I run the program it works like usually I can close on of them like normal. But sometimes it doesn't work like when I maximaze one of the windows and put the other above the first one and close one of them. What can be wrong?
Here's what I would do, don't know if it will fix your problem, but it will definitely fix a problem...
counter--;if (counter =< 0){    PostQuitMessage(0);}  //...


You want to have the deincrementation first, otherwise PostQuitMessage() won't get called until the next message pass, which won't happen because there won't be any more windows. So, your program will just keep running without a window, like before.
Yes it solved some of it but not all, I did it wrong after all. But i'm sure it worked this time, thanks for your help. Hope I can go on peacefully now with the next chapter to the harder stuff, without getting bitten by all those easy but hard to find dead endings :)

This topic is closed to new replies.

Advertisement