gpt4 book ai didi

c++ - WinAPI DestroyWindow 不工作

转载 作者:可可西里 更新时间:2023-11-01 11:31:54 31 4
gpt4 key购买 nike

我有这门课:

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;
}

我发现了问题,它不会破坏窗口,而且我用调试器检查析构函数和 Destroy() 方法中的 m_windowNULL

我使用这个类的代码:

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

//....

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

最佳答案

HWND m_window

声明了一个隐藏类成员变量的局部变量。您的编译器应该警告您这一点。注意警告。

关于c++ - WinAPI DestroyWindow 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18513266/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com