简体   繁体   中英

Remove button from a window

Using c++, I have created a window with a "WNDCLASS" structure. I have then used "CreateWindow" again with the system class "button" and set the parent handle to the handle of the window already created. How can I delete this button from the window? I have tried calling the "DestroyWindow" function but nothing happens. If I can't, is there a way to wipe the window completely and redraw everything again without the button so it does not appear?

#include <Windows.h>
#include <wchar.h>
HWND clientwindow;
RECT wr;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{


    switch (message)
    {
        case WM_COMMAND:
            DestroyWindow( clientwindow );      
            UpdateWindow( hWnd );
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int WINAPI wWinMain( HINSTANCE hInst,HINSTANCE,LPWSTR,INT )
{

    WNDCLASSEX wc = { sizeof( WNDCLASSEX ),CS_CLASSDC,WndProc,0,0,
                  GetModuleHandle( NULL ),NULL,NULL,NULL,NULL,
                  L"jjclass",NULL };


    wc.hCursor = LoadCursor( NULL,IDC_ARROW );
        RegisterClassEx( &wc );


    wr.left = 650;
    wr.right = wr.left + 500;
    wr.top = 150;
    wr.bottom = wr.top + 500;
    AdjustWindowRect( &wr,WS_OVERLAPPEDWINDOW,FALSE );
        HWND hWnd = CreateWindowW( L"jjclass",L"my window",
                              WS_OVERLAPPEDWINDOW,wr.left,wr.top,wr.right-         wr.left,wr.bottom-wr.top,
                          NULL,NULL,wc.hInstance,NULL );

        ShowWindow( hWnd,SW_SHOWDEFAULT );

    clientwindow = CreateWindow( TEXT("BUTTON"),TEXT("BUTTON"),WS_VISIBLE |     WS_CHILD,100,100,100,100,hWnd,(HMENU)1,NULL,NULL);

    UpdateWindow( hWnd );

    MSG msg;
    ZeroMemory( &msg,sizeof( msg ) );
    while( msg.message != WM_QUIT )
    {
        if( PeekMessage( &msg,NULL,0,0,PM_REMOVE ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }




    }
    UnregisterClass( L"jjclass",wc.hInstance );
    return 0;
}

The button actually gets destroyed, you just can't see it. That's because you stopped programming too soon, you didn't implement any painting code. Which you need to do since you set the WNDCLASS.hbrBackGround member to NULL so the default message handler cannot do anything useful when it processes the WM_ERASEBKGND message.

A simple workaround is to use the boilerplate code you'd find in any tutorial or book about Windows programming. Specify a brush for the window background:

WNDCLASSEX wc = { sizeof( WNDCLASSEX ),CS_CLASSDC,WndProc,0,0,
              GetModuleHandle( NULL ),NULL,NULL,
              (HBRUSH)(COLOR_WINDOW+1),               // <=== NOTE
              NULL,
              L"jjclass",NULL };

Lots of other things wrong in your code. Do read Petzold's Programming Windows if you want to write code like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM