Thread: DestroyWindow haunting me

  1. #1
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120

    DestroyWindow haunting me

    I am trying to create a program that will put a white static box or a black static box on the window according to a value in an array and it will do this multiple times in the program but every time i do it the previous sqaures show up and then the new ones. my code is here:

    Code:
    for(i=0;i<30;i++)
    {
        for(k=0;k<30;k++)
        {
            DestroyWindow(GetDlgItem(hwnd,1000+stickCounter));
            stickCounter++;
        }
    }
    stickCounter=0;
    for(i=0;i<30;i++)
    {
        for(k=0;k<30;k++)
        {
            if(stickTemplate[i][k]==0)
            {
                hWndStaticBlack[i][k] = CreateWindowEx(0,"STATIC","",WS_CHILD | WS_VISIBLE | SS_BLACKRECT,
                    10+(k*5), 100+(i*5),5, 5,hwnd,(HMENU)1000+stickCounter,hInstance,0);
            } else if(stickTemplate[i][k]==255)
            {
                hWndStaticBlack[i][k] = CreateWindowEx(0,"STATIC","",WS_CHILD | WS_VISIBLE | SS_WHITERECT,
                    10+(k*5), 100+(i*5),5, 5,hwnd,(HMENU)1000+stickCounter,hInstance,0);
            }
            stickCounter++;
        }
    }
    when i generate the data in the array and then call this activity, the first time it till create the array of static boxes just fine and in a quick manner

    when i do it a second time, it will remove the existing static boxes and the proceed to paint the new ones up there but before it gets them painted on the screen, the first ones show up and then the new ones like it is painting over them even though they are supposed to be destroyed

    when i do it a third time, the old boxes (from the second time) go away like they should then the first set of boxes flash up there then comes the second set flashing up there and the the third set finally get painted up there

    .
    .
    .

    when i do it an nth time, the previous boxes disappear and then the 1st, 2nd, 3rd, ... nth-1 boxes flash up there and then the nth boxes get painted up there

    i thought that DestroyWindow would get rid of these old static windows and allow the memory allocated for them to be occupied by the new data that is going to be set in it yet i watch the amount of memory that the program is taking and it just keeps growing

    any suggestions or other questions?

    any help would be appreciated. thanks in advanced.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I think you should send a WM_CLOSE message, and let the window take care of the DestroyWindow( ) call itself.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    Do you perhaps have an exapmle of this because as i look on the MSDN site, it says to use the WM_CLOSE message in conjunction with the DestroyWindow function to allow prompting before the close of the window.

    Also, how would i destroy all 900 static boxes in this fashion because the calls would just be qued and would get around to it when the program felt necessary so it could be possible in my program with that method to generate another set of static boxes before all were destroyed thereby destroying part of the newly created boxes once the que is complete.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  4. #4
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    I have replaced the line:
    Code:
    DestroyWindow(GetDlgItem(hwnd,1000+stickCounter));
    with:
    Code:
    SendMessage(GetDlgItem(hwnd,1000+stickCounter),WM_CLOSE,(WPARAM)0,(LPARAM)0);
    and it has the same results as before, i assume because it is using the same method to close the static boxes that i was.

    Is there perhaps another way of using the WM_CLOSE method that I am not aware of or am I using it incorrectly?
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  5. #5
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    I have even tried just putting up a blank static box up after i delete the existing boxes to cover up that area but it doesnt do anything different other than flash one more thing up before it paints it.

    i also have tried to forget the DestroyWindow or WM_CLOSE function and message all together and just make the boxes over the old ones but that is no different then what i started with.

    is there a way to clear the programs memory or something so that it is starting new each time it creates the array of boxes without having to kill the entire application and reopen it?
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  6. #6
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    ok, here some new information, i was using spy++ to look at what all my program was creating and i found out that it is making the static boxes just fine, but it is having trouble getting rid of all of the static boxes

    i created the first set of boxes and it of course works fine, now i create another set of boxes and then it shows up, 675 boxes from the first set still exist

    when i create yet another set, i have 675 boxes from both the first set and the second set plus all the boxes that it just created.

    any clue as to why its only deleting 25% of my boxes and leaving the rest on the window?

    i even tried putting a while loop in there incrementing a variable from 0 to 99 to see if maybe it was just going through it too fast and it didnt work, i just got the same results as before.

    holy geez, as i am typing this thing up, i am trying new things and since it is only deleting 25% of the windows, i thought about maybe repeating the loop 4 times to see if that does anything. it did! now i only have the 900 windows that should be there!!!

    why is this? why should i have to repeat the same loop 4 times to get it to delete all of the windows...i dont know if its just my computer, i can test it on a few others to see but it still confuses me.

    this is now the code that seems to get rid of all those windows:
    Code:
    for(j=0;j<4;j++)
    {
        for(i=0;i<30;i++)
        {
            for(k=0;k<30;k++)
            {
                //DestroyWindow(GetDlgItem(hwnd,1000+stickCounter));
                SendMessage(GetDlgItem(hwnd,1000+stickCounter), WM_CLOSE,(WPARAM)0,(LPARAM)0);
                stickCounter++;
            }
        }
    }
    stickCounter=0;
    for(i=0;i<30;i++)
    {
        for(k=0;k<30;k++)
        {
            char chTemp[255];
            sprintf(chTemp,"%d",stickCounter);
            if(stickTemplate[i][k]==0)
            {
                hWndStaticBlack[i][k] = CreateWindowEx(0,"STATIC",chTemp,WS_CHILD | WS_VISIBLE | SS_BLACKRECT,
                    10+(k*5), 100+(i*5),5, 5,hwnd,(HMENU)1000+stickCounter,hInstance,0);
            } else if(stickTemplate[i][k]==255)
            {
                hWndStaticBlack[i][k] = CreateWindowEx(0,"STATIC",chTemp,WS_CHILD | WS_VISIBLE | SS_WHITERECT,
                    10+(k*5), 100+(i*5),5, 5,hwnd,(HMENU)1000+stickCounter,hInstance,0);
            }
            stickCounter++;
        }
    }
    also just so you know, i tried it with both WM_CLOSE message and the DestroyWindow function and both of them would only delete 25% of the windows.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  7. #7
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278

    Re: DestroyWindow haunting me

    Originally posted by AtomRiot
    (HMENU)1000+stickCounter
    HMENU is a handle, which is a pointer to void. You are typecasting 1000 to a pointer to void, and then adding stickCounter to it. Normally, when you add a number to a pointer, it adds the number multiplied by the size of the type that it points to, so you needn't worry about its size. I honestly have no idea what happens when you do pointer arithmetic with a void pointer. I would use brackets around the integer to ensure you have the proper value before the type cast, to ensure that the WM_CLOSE message you are sending has the same child id as the one you want to be destroyed.

  8. #8
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    ok, i tried using the parentheses around it and i got the same result as before it looped 4 times. i also tried just putting 1000+sticKCounter into a variable and then putting it after the (HMENU) typecasting and i still get the same results.

    even if that was the problem then why would it work 25% of the time and then not work the rest of the time?
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You are also reusing the same ID values.

    The 900 destroy window msgs may not have been processed before you have created another control with the same ID but different hwnd.

    There must be a better way to do this.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Try going back to your message loop in between destroying the old windows and creating the new ones.

    You could call the below function or preferrably schedule a fifty millisecond timer and return to your main message loop. When the timer fires you create the new windows. Timer messages are low priority so all other messages will be dealt with first.
    Code:
    void DoEvents() {
    
    	/* This function pumps available messages.
    	 * It should be noted that windows may be destoyed
    	 * in this loop so an application should call
    	 * IsWindow(hwnd) upon return if it relies on a certain window */
    
    	MSG Msg; 
    
    	while ( PeekMessage(&Msg,NULL,0,0,PM_REMOVE) ) {
    		TranslateMessage(&Msg); 
    		DispatchMessage(&Msg);
    	}
    
    }

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Edit: I should mention that the problem is most likely that the WM_PAINT messages are not being processed. While the WM_DESTROY messages are sent(Windows calls your window procedure directly), as far as I know WM_PAINT are posted to the message queue and are therefore not processed until you pump the message queue.

    I tried to edit my previous post but the stupid board wouldn't work. Does anyone else have serious troubles trying to post, edit and login?

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Also:
    Try WS_CLIPCHILDREN when creating the parent window.
    Try WS_CLIPSIBLINGS and WS_EX_NOPARENTNOTIFY when creating the child windows.

  13. #13
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    AWSOME!!!! thanks anonytmouse, it even looks better when it runs! and it destroys all of the previous windows.

    i just did the WS_CLIPSCHILDREN on the parent and then the WS_CLIPSIBLINGS and WS_EX_NOPARENTNOTIFY on the children and it works great.

    i also tried the DoEvents() method and it also worked, although there was a pause and a blank on the window but it still worked.

    I used spy++ and it is indeed getting rid of the old static boxes before the new ones are painted. thanks that is a huge help!

    Thanks again!!
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dialog box problem keeps haunting me
    By Bleech in forum Windows Programming
    Replies: 2
    Last Post: 08-23-2006, 01:12 AM
  2. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  3. DestroyWindow(); wont destroy the window?
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 08-17-2004, 04:27 PM
  4. Mysterious crash on DestroyWindow()
    By Hunter2 in forum Game Programming
    Replies: 8
    Last Post: 02-03-2003, 08:03 PM