ST3
ST3

Reputation: 8946

WinAPI DestroyWindow not working

I have this class:

WNDCLASSEX ActionButton::m_wndClass = CreateWndClass();

ActionButton::ActionButton() :
    m_function(NULL), m_parameters(NULL), m_window()
{}

ActionButton::~ActionButton()
{
    DestroyWindow(m_window);
}

bool ActionButton::DestroyButton()
{
    return DestroyWindow(m_window);
}

bool ActionButton::Create(HWND parent, int x, int y, int heigth, int width)
{
    HWND m_window = CreateWindowEx(0, L"Action button", NULL, WS_CHILD | WS_VISIBLE, 
        x, y, width, heigth, parent, NULL, NULL, NULL);

    if (m_window == NULL)
        return false;

    SetWindowLongPtr(m_window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
    return true;
}

void ActionButton::SetFuncionLeftButtonDown(CallbackFunction f)
{
    m_function = f;
}

void ActionButton::SetParametersLeftButtonDown(void* param)
{
    m_parameters = param;
}

WNDCLASSEX ActionButton::CreateWndClass()
{
    WNDCLASSEX m_wndClass = {0};

    if (m_wndClass.cbSize == 0)
    {
        m_wndClass.cbSize = sizeof(WNDCLASSEX);
        m_wndClass.style = CS_NOCLOSE;
        m_wndClass.lpfnWndProc = WndProc;
        m_wndClass.cbClsExtra = 0;
        m_wndClass.cbWndExtra = 0;
        m_wndClass.hInstance = GetModuleHandle(NULL);
        m_wndClass.hIcon = NULL;
        m_wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        m_wndClass.hbrBackground = HBRUSH(COLOR_BACKGROUND);
        m_wndClass.lpszMenuName = NULL;
        m_wndClass.lpszClassName = L"Action button"; 
        m_wndClass.hIconSm = NULL;
    }

    RegisterClassEx(&m_wndClass);

    return m_wndClass;
}


LRESULT __stdcall ActionButton::WndProc (HWND window, UINT msg, WPARAM wp, LPARAM lp)
{
    ActionButton* classInfo = reinterpret_cast<ActionButton *>(GetWindowLongPtr(window, GWLP_USERDATA));
    switch(msg)
    {
        case WM_LBUTTONDOWN:
        {
            (classInfo->m_function)(classInfo->m_parameters, classInfo);

            classInfo->DestroyButton();
            break;
        }

        case WM_DESTROY:
        {

            break;
        }

        default:
            return DefWindowProc(window, msg, wp, lp);
    }
    return 0;
}

I have found problem, that it do not destroy window, what is more I check with debugger that m_window in destructor and Destroy() methods is NULL.

My code of using this class:

void Function(void* input, ActionButton*)
{
    std::cout << "Works :)\n";
}

//....

ActionButton button;
button.Create(Form.Get(), 150,150, 50,50);
button.SetFuncionLeftButtonDown(Function);

Upvotes: 1

Views: 2046

Answers (1)

Dark Falcon
Dark Falcon

Reputation: 44181

HWND m_window

That declares a local variable which hides the class member variable. Your compiler should be warning you about this. Pay attention to the warnings.

Upvotes: 3

Related Questions