How to close multiple windows individually (Beginner)

Hi all,

I've just started to learn Win32 API stuf via the "Tricks of the Windows Game Programming Gurus" book. And I'm slightly stuck.

This is just a little task set in the book (nothing major) but I don't want to continue until I have this sussed!

Basically I can get 2 different windows (with 2 DIFFERENT instances of WNDCLASSEX), but trying to get them to close individually without killing the app. A simple counter worked first of all (increment for each window created, decrement for each closed) but I'm concerned about this being a suitable choice!

I started with the idea of setting up a handle array, placing the handle into the array when created, removed it when closed, kill app if array empty...

But...

Well here's my code and output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    switch(msg)
    {
        case WM_CREATE:
        {
            //do initialistation stuff here
            // then return process
            int index = 0;
            for(index; hWindowHandleArray[index]; index++)
            {
                cout << "In Loop" << endl;
                cout << "Index is at position " << index
                     << " with value of " << hWindowHandleArray[index] << endl;
            }
            if(index = 50)
            {
                cout << "End of array reached!" << endl;
                return(0);
            }

            cout << "Window has been created with handle of " << hwnd << endl;
            hWindowHandleArray[index] = hwnd;

            return(0);
        }
        break;
        case WM_PAINT:
        {
            //simply validate the window
            hdc = BeginPaint(hwnd,&ps);
            //do all your painting here
            EndPaint(hwnd,&ps);
            return(0);
        }
        break;
        case WM_DESTROY:
        {
            // kill the app, this send a WM_QUIT message

            if(hWindowHandleArray[0] == NULL)
            {
                PostQuitMessage(0);
            }
            return(0);
        }
        break;
        case WM_CLOSE:
        {
            for(int index = 0; hWindowHandleArray[index]; index++)
            {
                if(hWindowHandleArray[index] = hwnd)
                {
                    cout << "Handle " << hwnd << " found at position " << index << endl;
                    //if it's there remove and move line up
                    cout << "Window with hwnd of " << hwnd << " has been closed!" << endl;
                    hWindowHandleArray[index] = 0;
                    for(int index2 = index; index2 < 50; index2++)
                    {
                        hWindowHandleArray[index2] = hWindowHandleArray[index2+1];
                    }
                    break;
                }
            }
        }
        break;
        default: break;
    }


And here's the output that's got me stumped (explanation will follow).


End of array reached!
End of array reached!



I've already initialised the entire array to zero via a loop in WinMain, so WHY is it not putting the handle into the array? Why is index going onto 50?!?!?!?!?!?!?!?!?!?!

Gah! If anyone can see what I'm trying to do and shaking their head in the way one would shake their head at a small chil confused by a game of peekaboo, then PLEASE helpe me lol.

Just to restate, I'm trying to setup a system that doesn't kill the app when only one of the windows is closed, only when all windows have been closed (by that 2, 3 or 30).
if(index = 50) //error
Oh my word, what a total pleb I am!!!!

There must be a special word for this kind of stupid lol.

Anyway, have changed line 14 to the following:

if(index == 50)
Or even
if(hWindowHandleArray[50])

It works fine now.

Can I just ask though - would I have been better off just sticking to the basic counter methodology? Or was I right to try and make the code a little more rigid? I'm asking as I want to know if my thinking is wrong...

Many thanks guestgulkan!
and line 50
if(hWindowHandleArray[index] == hwnd)
Topic archived. No new replies allowed.