简体   繁体   中英

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

I am having issues with my code. I am making a class to wrap around some WinAPI to create GUIs, however I am having issues when attempting to register the class.

My code:

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 is already defined in the class header as of WNDCLASSEX and the register function simply runs RegisterClassEx(&_wndClassEx).

The following are how I call these classes: (However only one is called at a time)

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

The second one works perfectly fine, however when I call the first (shorter parameters, which puts it through to the second) class registration fails. I have completely written the _wndClassEx as well as gone through every single one and modified it to no success. I have gone through with a debugger and everything seems fine. So I have absolutely no idea what to do.

By the way, abstractWindow::msgRouter is static.

Thanks.

The problem is with this constructor:

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

You can't call a constructor from another constructor like that. What ends up happening is that a temporary object is created and then immediatly discarded. The way to achieve that (this only works if you have a compiler which implements this feature from the new C++11 Standard) is if you say

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

Another way would be to do what @aztaroth said: create a separate method and call it from both constructors (that works even with an older compiler).

编写一个初始化窗口的方法(基本上剪切并粘贴第二个构造函数),然后使用两个构造函数中的适当值调用它。

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