destroywindow() problem

This is a discussion on destroywindow() problem within the Windows Programming forums, part of the Platform Specific Boards category; I've tried making a programme in Windows but the problem is that DestroyWindow() isn't working on my buttons when i ...

  1. #1
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161

    destroywindow() problem

    I've tried making a programme in Windows but the problem is that DestroyWindow() isn't working on my buttons when i put it in wm_command:. There is a main menu and when you click on the options like continue i wanted the main menu buttons to be deleted but they don't get deleted.

    Code:
    #include <windows.h>
    
    
    
    
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    
    HINSTANCE hInstGlobal;
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    {
        hInstGlobal = hInstance;
        WNDCLASSEX  WndCls;
        static char szAppName[] = "Kazalio Studios";
        MSG         Msg;
    
        WndCls.cbSize        = sizeof(WndCls);
        WndCls.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
        WndCls.lpfnWndProc   = WindProcedure;
        WndCls.cbClsExtra    = 0;
        WndCls.cbWndExtra    = 0;
        WndCls.hInstance     = hInstance;
        WndCls.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        WndCls.lpszMenuName  = NULL;
        WndCls.lpszClassName = szAppName;
        WndCls.hIconSm       = LoadIcon(hInstance, IDI_APPLICATION);
        RegisterClassEx(&WndCls);
    
        CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                       szAppName, "Dog Simulator 2",
                       WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                       CW_USEDEFAULT, CW_USEDEFAULT, 600, 400,
                       NULL, NULL, hInstance, NULL);
    
        while( GetMessage(&Msg, NULL, 0, 0) )
        {
            TranslateMessage(&Msg);
            DispatchMessage( &Msg);
        }
    
        return static_cast<int>(Msg.wParam);
    }
    
    
    void dogname(HWND hWnd, WPARAM wParam, LPARAM lParam);
    void credits(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    void startscrn(HWND hWnd, WPARAM wParam, LPARAM lParam);
    void dogmenu(HWND hWnd, WPARAM wParam, LPARAM lParam);
    
    HWND hName, hNamecont;
    
    
    
    
    
    
    
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,
                                   WPARAM wParam, LPARAM lParam)
    {
        HDC         hDC;
        PAINTSTRUCT Ps;
        HFONT	    font;
        static BITMAPFILEHEADER *pbmfh;
        static BITMAPINFO *pbmi;
        static BYTE *pBits;
        static int cxDib, cyDib;
    
    
        switch(Msg)
        {
                   
        case WM_CREATE:
             
             
             
             
    
             
             
             HWND hContinue, hExit, hCredits;
             hContinue = CreateWindow ( "Button", "Continue",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        10, 320, 140, 20,
                                        hWnd, (HMENU) 1,
                                        hInstGlobal, NULL );
             hExit = CreateWindow     ( "Button", "Exit",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        437, 320, 140, 20,
                                        hWnd, (HMENU) 2,
                                        hInstGlobal, NULL);
             hCredits = CreateWindow  ( "Button", "Credits",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        225, 320, 140, 20,
                                        hWnd, (HMENU) 3,
                                        hInstGlobal, NULL);
                                        
             
             return 0;
        case WM_COMMAND:
             if (HIWORD(wParam) == BN_CLICKED)
             {
                if(LOWORD(wParam) == 1)
                {
                    DestroyWindow(hCredits);              
                dogname(hWnd, wParam, lParam);
                                 
                }
                 
                if(LOWORD(wParam) == 2)
                {
                   SendMessage (GetParent((HWND)lParam),
                   WM_DESTROY, 0, 0);
                   }
                if(LOWORD(wParam) == 3)
                {
                                  
                                  DestroyWindow( hExit );
                credits(hWnd, Msg, wParam, lParam);
                }
                
                if (LOWORD(wParam) == 5)
                {
                startscrn(hWnd, wParam, lParam);
                }
                
                if (LOWORD(wParam) == 4)
                {
                startscrn(hWnd, wParam, lParam);
                }
                   }
                return 0;
                                
        case WM_PAINT:
           DWORD dwFileSize, dwHighSize, dwBytesRead;
             HANDLE hFile;
             hFile = CreateFile ("E:\\and.bmp",GENERIC_READ,
                                 FILE_SHARE_READ, NULL,
                                 OPEN_EXISTING,
                                 FILE_FLAG_SEQUENTIAL_SCAN,
                                 NULL);
                                 
             dwFileSize = GetFileSize (hFile, &dwHighSize);
             pbmfh = (BITMAPFILEHEADER *) malloc (dwFileSize);
             ReadFile (hFile, pbmfh, dwFileSize, &dwBytesRead, NULL);
             pbmi = (BITMAPINFO *) (pbmfh + 1);
             pBits = (BYTE *) pbmfh + pbmfh->bfOffBits;
             cxDib = pbmi->bmiHeader.biWidth;
             cyDib = abs (pbmi->bmiHeader.biHeight);
             HDC hdc;
             hdc = GetDC (hWnd);
             
             SetDIBitsToDevice ( hdc,
                               200,
                               195,
                               cxDib,
                               cyDib,
                               
                               0,
                               0,
                               0,
                               cyDib,
                               pBits,
                               pbmi,
                               DIB_RGB_COLORS);
             ReleaseDC (hWnd, hdc);
             free (pbmfh);  
             
             
    	hDC = BeginPaint(hWnd, &Ps);
    		
             font = CreateFont(90,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Adolescence");
    
            SelectObject(hDC, font);
            TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
            DeleteObject(font);
            
            font = CreateFont(18,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Arial");
    
            SelectObject(hDC, font);
            TextOut(hDC, 13, 115, "Welcome to Dog Simulator 2 the sequel to Dog Simulator 1 from Kazalio Studios.", 78);
            TextOut(hDC, 13, 135, "There are a few differences between dog Simulator 1 and Dog Simulator 2, the ", 77);
            TextOut(hDC, 13, 155, "main one being that Dog Simulator 2 is now in a window and has fonts and ",72);
            TextOut(hDC, 13, 175, "graphics to make it look better. There are also more things to do with your", 75);
            TextOut(hDC, 13, 195, "dog.", 4);
            DeleteObject(font);
    
    	EndPaint(hWnd, &Ps);
    	break;
        case WM_DESTROY:
    	PostQuitMessage(WM_QUIT);
    	break;
        default:
    	return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        return 0;
    }
    
    void startscrn (HWND hWnd, WPARAM wParam, LPARAM lParam)
    {
         
         
         HDC hDC;
         PAINTSTRUCT Ps;
         HFONT font;
         hDC = GetDC (GetParent((HWND) lParam));
    		Rectangle (hDC, 0, 0, 600, 460);
             font = CreateFont(90,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Adolescence");
    
            SelectObject(hDC, font);
            TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
            DeleteObject(font);
            
            font = CreateFont(18,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Arial");
    
            SelectObject(hDC, font);
            TextOut(hDC, 13, 115, "Welcome to Dog Simulator 2 the sequel to Dog Simulator 1 from Kazalio Studios.", 78);
            TextOut(hDC, 13, 135, "There are a few differences between dog Simulator 1 and Dog Simulator 2, the ", 77);
            TextOut(hDC, 13, 155, "main one being that Dog Simulator 2 is now in a window and has fonts and ",72);
            TextOut(hDC, 13, 175, "graphics to make it look better. There are also more things to do with your", 75);
            TextOut(hDC, 13, 195, "dog.", 4);
            DeleteObject(font);
            
            char *dogname;
            dogname = 0;
            
            HWND hContinue, hExit, hCredits, hBack;
             hContinue = CreateWindow ( "Button", "Continue",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        10, 320, 140, 20,
                                        hWnd, (HMENU) 1,
                                        hInstGlobal, NULL );
             hExit = CreateWindow     ( "Button", "Exit",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        437, 320, 140, 20,
                                        hWnd, (HMENU) 2,
                                        hInstGlobal, NULL);
             hCredits = CreateWindow  ( "Button", "Credits",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        225, 320, 140, 20,
                                        hWnd, (HMENU) 3,
                                        hInstGlobal, NULL);
    
    	EndPaint(hWnd, &Ps);
    }
    
    void credits(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
        
        
         HDC hDC;
         PAINTSTRUCT Ps;
         HFONT font;
         hDC = GetDC (GetParent((HWND) lParam));
         Rectangle (hDC, 0, 0, 600, 460);
    		
             font = CreateFont(90,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Adolescence");
    
            SelectObject(hDC, font);
            TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
            
         
         font = CreateFont(18, 0, 0, 0,
                           FW_NORMAL, FALSE, FALSE, FALSE,
                           ANSI_CHARSET, OUT_DEFAULT_PRECIS,
                           CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                           DEFAULT_PITCH | FF_ROMAN,
                           "Arial");
         
         SelectObject(hDC, font);
         TextOut(hDC, 13, 115, "Dog Simulator 1 and 2 was created by Kazalio Studios. The creators were:", 72);
         TextOut(hDC, 13, 135, "Programming: Alex Pajak", 23);
         TextOut(hDC, 13, 155, "Ideas/Design: Kasper Wilkosz", 28);
         
         
         
         
         HWND hScrnmain;
         hScrnmain = CreateWindow ( "Button", "Back",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        13, 200, 140, 20,
                                        hWnd, (HMENU) 4,
                                        hInstGlobal, NULL );
         DeleteObject(font);
         DeleteObject(hWnd);
         EndPaint(hWnd, &Ps);
         ReleaseDC (GetParent((HWND) lParam), hDC);
    }
    
    void dogname(HWND hWnd, WPARAM wParam, LPARAM lParam)
    {
         
         static BITMAPFILEHEADER *pbmfh;
        static BITMAPINFO *pbmi;
        static BYTE *pBits;
        static int cxDib, cyDib;
         DWORD dwFileSize, dwHighSize, dwBytesRead;
             HANDLE hFile;
             hFile = CreateFile ("E:\\blank.bmp",GENERIC_READ,
                                 FILE_SHARE_READ, NULL,
                                 OPEN_EXISTING,
                                 FILE_FLAG_SEQUENTIAL_SCAN,
                                 NULL);
                                 
             dwFileSize = GetFileSize (hFile, &dwHighSize);
             pbmfh = (BITMAPFILEHEADER *) malloc (dwFileSize);
             ReadFile (hFile, pbmfh, dwFileSize, &dwBytesRead, NULL);
             pbmi = (BITMAPINFO *) (pbmfh + 1);
             pBits = (BYTE *) pbmfh + pbmfh->bfOffBits;
             cxDib = pbmi->bmiHeader.biWidth;
             cyDib = abs (pbmi->bmiHeader.biHeight);
             HDC hdc;
             hdc = GetDC (GetParent((HWND) lParam));;
             
             SetDIBitsToDevice ( hdc,
                               200,
                               195,
                               cxDib,
                               cyDib,
                               
                               0,
                               0,
                               0,
                               cyDib,
                               pBits,
                               pbmi,
                               DIB_RGB_COLORS);
             ReleaseDC (GetParent((HWND) lParam), hdc);
             free (pbmfh);  
         HDC         hDC;
        PAINTSTRUCT Ps;
        HFONT	    font;
         hDC = GetDC (GetParent((HWND) lParam));
         Rectangle (hDC, 0, 0, 600, 460);
         
         
         font = CreateFont(90,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Adolescence");
    
            SelectObject(hDC, font);
            TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
            DeleteObject(font);
    		
             font = CreateFont(18,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Arial");
    
            SelectObject(hDC, font);
            TextOut(hDC, 13, 115, "First you must choose a name for your dog.", 42);
            TextOut(hDC, 13, 135, "Name:", 5);
            
            HWND hName, hNamecont;
            
            hName = CreateWindow ( "EDIT", "",
                                        WS_CHILD | WS_VISIBLE |
                                        WS_BORDER | ES_AUTOHSCROLL,
                                        80, 135, 140, 20,
                                        hWnd, (HMENU) 1,
                                        hInstGlobal, NULL );
                                        
            hNamecont = CreateWindow ( "Button", "Continue",
                                        WS_CHILD | WS_VISIBLE |
                                        BS_PUSHBUTTON,
                                        13, 160, 140, 20,
                                        hWnd, (HMENU) 5,
                                        hInstGlobal, NULL );
                                        
            
            DeleteObject(font);
    
    	EndPaint(hWnd, &Ps);
    }
    
    void dogmenu(HWND hWnd, WPARAM wParam, LPARAM lParam)
    {
          HDC         hDC;
        PAINTSTRUCT Ps;
        HFONT	    font;
         hDC = GetDC (GetParent((HWND) lParam));
         Rectangle (hDC, 0, 0, 600, 460);
         
         
         font = CreateFont(90,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Adolescence");
    
            SelectObject(hDC, font);
            TextOut(hDC, 22, 0, "Dog Simulator 2", 15);
            DeleteObject(font);
    		
             font = CreateFont(18,0,0, 0,
                               FW_NORMAL, FALSE, FALSE, FALSE,
                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
    		         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
    		         DEFAULT_PITCH | FF_ROMAN,
    			"Arial");
    
            SelectObject(hDC, font);
            TextOut(hDC, 13, 115,"Hello", 5);
            TextOut(hDC, 13, 135, "Name:", 5);
            
             DeleteObject(font);
    
    	EndPaint(hWnd, &Ps);
    }
    The main menu is the starting screen and what i have being trying to accomplish is that the buttons can't be clicked once the user clicks on continue or credits, because i just painted a rectangle over the top of them but they can still click them if they know where they are, and i can't put buttons over the top.
    I started out with nothing and I still have most of it left.

  2. #2
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Did you try to use ShowWindow(hCredits, SW_HIDE); ? If that doesn't work, try following the ShowWindow() call by an UpdateWindow(hCredits); call. DestroyWindow() should be working, but I can't explain as to why it isn't. If that doesn't work then just post back and I'll actually try compiling your code to see if I can figure out whats wrong (would do that now, but I'm about a 4 hour drive from my computer that has a compiler on it, hehe).

    Hope that helps,
    cyreon

  3. #3
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    it doesn't work still. In WM_COMMAND: i put in lparam == 1 that it should disable the credits button and it doesn't work. If you get it to work please post back code.
    I started out with nothing and I still have most of it left.

  4. #4
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    I will look into the problem tonight. I will be taking the bus back to where my laptop is today (very soon actually), that will be the first thing I do after I get home and settle in. Should have a reply by the end of the night for you.

  5. #5
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    ok thanks cyreon
    I started out with nothing and I still have most of it left.

  6. #6
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Okay I never noticed this before. You can use DestroyWindow() except you need to do one of two things (the choice is up to you based on how you want to code it). The first choice is placing static in front of the hEdit, hCredits, etc. declarations just above WM_CREATE message handling in your windows procedure function. The second choice is to declare these HWNDs globally instead of locally inside the function. Your problem is this: you are declaring the variables (fresh ones) each time the windows procedure function is called, and then destroying them when the function ends. Thus when you receive the WM_COMMAND message your variables are now empty.

    Hope this helps,
    cyreon

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    You've asked this before.

    1. Make the buttons HWND a STATIC variable. As soon as the WM_CREATE ends you have lost the handles to the buttons.

    Or use this when you need the buttons handle,
    Code:
    HWND hButton = GetDlgItem( hDialog, BUTTONS_ID_NUMBER); // the HMENU param in CreateWindow()
    2. You create the same button multiple times, don't! It is a new button each time

    3.Your app leaks GDI memory badly. First symptom will be things not drawing properly.
    read this thread;
    respond to button

    4. Read the manual on BeginPaint() EndPaint(). You can't use them except as a pair AND inside WM_PAINT.

    5. Just drawing over a button will NOT stop the user clicking it (unless you create another control and move it higher in the Z-Order than the button).
    Windows will always draw the button above/thru the rectangle.

    To do what you want, replace this line

    DestroyWindow(hCredits);

    with the one I posted in this thread. The ID number is the same as the LOWORD(wParam) ie 1

    deleting buttons

    6. Look at LoadImage() and CreatePatternBrush()
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21