我有这个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; }
我发现问题,它不会销毁窗口,我更多的检查与debugging器m_window
在析构函数和Destroy()
方法是NULL
。
我使用这个类的代码:
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
这声明了隐藏类成员变量的局部变量。 你的编译器应该警告你这个。 注意警告。