gpt4 book ai didi

c++ - WinAPI C++ - RegisterClassEx 'The parameter was incorrect'

转载 作者:行者123 更新时间:2023-11-30 03:01:51 26 4
gpt4 key购买 nike

我的代码有问题。我正在创建一个类来围绕一些 WinAPI 来创建 GUI,但是我在尝试注册该类时遇到了问题。

我的代码:

inline Window::Window(const TCHAR *windowName, const int x, const int y, const int width, const int height, const TCHAR *className) : abstractWindow() {
Window(0, className, windowName, WS_OVERLAPPEDWINDOW, x, y, width, height, NULL, NULL, NULL, NULL);
}

Window::Window(const DWORD dwExStyle, const TCHAR *lpClassName, const TCHAR *lpWindowName, const DWORD dwStyle, const int x, const int y, const int nWidth, const int nHeight, const HWND hWndParent, const HMENU hMenu, const HINSTANCE hInstance, const LPVOID lpParam) : abstractWindow() {
_proc = (WNDPROC*) &abstractWindow::msgRouter;

_styleEx = dwExStyle;
_className = (!lpClassName) ? TEXT("MyGuiClass") : lpClassName;
_windowName = lpWindowName;
_style = dwStyle;
_x = x;
_y = y;
_width = nWidth;
_height = nHeight;
_hwndParent = hWndParent;
_hInstance = (!hInstance) ? ::GetModuleHandle(NULL) : hInstance;
_hMenu = hMenu;
_lpParam = lpParam;

_wndClassEx.cbSize = sizeof(_wndClassEx);
_wndClassEx.style = CS_HREDRAW | CS_VREDRAW;
_wndClassEx.lpfnWndProc = abstractWindow::msgRouter;
_wndClassEx.cbClsExtra = 0;
_wndClassEx.cbWndExtra = 0;
_wndClassEx.hInstance = _hInstance;
_wndClassEx.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
_wndClassEx.hCursor = ::LoadCursor(NULL, IDC_ARROW);
_wndClassEx.hbrBackground = (HBRUSH) COLOR_WINDOW;
_wndClassEx.lpszMenuName = NULL;
_wndClassEx.lpszClassName = _className;
_wndClassEx.hIconSm = ::LoadIcon(NULL, IDI_APPLICATION);
}

_wndClassEx 已在 WNDCLASSEX 的类 header 中定义,注册函数仅运行 RegisterClassEx(&_wndClassEx)。

以下是我如何调用这些类:(但是一次只调用一个)

Window gui (TEXT("Title"), 10, 10, 500, 250);
Window gui (0, NULL, TEXT("Title"), WS_OVERLAPPEDWINDOW, 10, 10, 500, 200, NULL, NULL, hInstance, NULL);

第二个工作得很好,但是当我调用第一个(较短的参数,将其传递给第二个)时,类注册失败。我已经完整地编写了 _wndClassEx 并且遍历了每一个并对其进行了修改但没有成功。我已经使用调试器进行了调试,似乎一切正常。所以我完全不知道该怎么做。

顺便说一下,abstractWindow::msgRouter 是静态的。

谢谢。

最佳答案

问题出在这个构造函数上:

inline Window::Window(const TCHAR *windowName, const int x, const int y, const int width, const int height, const TCHAR *className) : abstractWindow() {
Window(0, className, windowName, WS_OVERLAPPEDWINDOW, x, y, width, height, NULL, NULL, NULL, NULL);
}

你不能像那样从另一个构造函数调用构造函数。最终发生的事情是创建了一个临时对象,然后立即将其丢弃。实现这一点的方法(这只有在你有一个从新的 C++11 标准实现这个特性的编译器时才有效)是如果你说

inline Window::Window(const TCHAR *windowName, const int x, const int y, const int width, const int height, const TCHAR *className) : Window(0, className, windowName, WS_OVERLAPPEDWINDOW, x, y, width, height, NULL, NULL, NULL, NULL) {};

另一种方法是按照@aztaroth 所说的去做:创建一个单独的方法并从两个构造函数中调用它(即使使用较旧的编译器也可以)。

关于c++ - WinAPI C++ - RegisterClassEx 'The parameter was incorrect',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10684498/

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