C++ WNDCLASSEX不接受WNDPROC参数

C++ WNDCLASSEX不接受WNDPROC参数,c++,winapi,C++,Winapi,在这里尝试学习一些WinAPI并将WNDPROC传递给我的wcex.lpfnWndProc时遇到了麻烦,在删除过程中一切正常,但在调用MyWinClass(WNDPROC、LPCWSTR、HINSTANCE)时出错 错误在该代码段底部的WinMain函数中 我正在学习的源代码是1998年的,但它是最直观的(对我个人来说),它一直在用扩展版本WNDCLASSEX、CreateWindowEx等替换旧版本,如WNDCLASS #include <Windows.h> #include &

在这里尝试学习一些WinAPI并将WNDPROC传递给我的wcex.lpfnWndProc时遇到了麻烦,在删除过程中一切正常,但在调用MyWinClass(WNDPROC、LPCWSTR、HINSTANCE)时出错

错误在该代码段底部的WinMain函数中

我正在学习的源代码是1998年的,但它是最直观的(对我个人来说),它一直在用扩展版本WNDCLASSEX、CreateWindowEx等替换旧版本,如WNDCLASS

#include <Windows.h>
#include <string>
using namespace std;

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

class MyWinClass
{
public:
    MyWinClass(WNDPROC winProc, LPCWSTR className, HINSTANCE hInst);
    void Register()
    {
        ::RegisterClassEx(&wcex);
    }
private:
    WNDCLASSEX wcex;
};

MyWinClass::MyWinClass(WNDPROC winProc, LPCWSTR className, HINSTANCE hInst)
{
    wcex.style = 0;
    wcex.lpfnWndProc = winProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInst;
    wcex.hIcon = 0;
    wcex.hCursor = LoadCursor(0, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = 0;
    wcex.lpszClassName = className;
}

class CreateMyWindow
{
public:
    CreateMyWindow() : _hwnd(0){}
    CreateMyWindow(char const * caption, char const * className, HINSTANCE hInstance);
    void ShowWindow(int cmdShow)
    {
        ::ShowWindow(_hwnd, cmdShow);
        ::UpdateWindow(_hwnd);
    }
protected:
    HWND _hwnd;
};

CreateMyWindow::CreateMyWindow(char const * caption, char const * className, HINSTANCE hInstance)
{
    _hwnd = ::CreateWindowEx(
        (DWORD)className,
        (LPCWSTR)caption,
        WS_OVERLAPPED,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        0,
        0,
        NULL,
        hInstance,
        0);
}


//MyWindow Procedure that is called by windows

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    char className[] = "Winnie";

    MyWinClass myWinClass(WindowProcedure, className, hInst);
    /*Error: no Instance of constructor "MYWinClass::MYWinClass" mat  the argument list argument types are:(LRESULT _stdcall(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) char[7], HINSTANCE)

//MyWinClass myWinClass(WindowProcedure...) us underlined with above error, and I do not know why it is seeing WNDPROC as an HWND.*/
    myWinClass.Register();

    CreateMyWindow myWndClass("MyWindowClass", className, hInst);
    myWndClass.ShowWindow(cmdShow);

    MSG msg;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}
#包括
#包括
使用名称空间std;
LRESULT回调窗口过程(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM);
Mywinglass类
{
公众:
MyWinClass(WNDPROC winProc、LPCWSTR类名称、HINSTANCE hInst);
无效登记册()
{
::RegisterClassEx(&wcex);
}
私人:
WNDCLASSEX wcex;
};
MyWinClass::MyWinClass(WNDPROC winProc、LPCWSTR类名、HINSTANCE hInst)
{
wcex.style=0;
wcex.lpfnWndProc=winProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInst;
wcex.hIcon=0;
wcex.hCursor=LoadCursor(0,IDC_箭头);
wcex.hbrBackground=(HBRUSH)(彩色窗口+1);
wcex.lpszMenuName=0;
wcex.lpszClassName=className;
}
类CreateMyWindow
{
公众:
CreateMyWindow():hwnd(0){}
CreateMyWindow(char const*标题,char const*类名,HINSTANCE HINSTANCE);
void ShowWindow(int-cmdShow)
{
::ShowWindow(_-hwnd,cmdShow);
::UpdateWindow(\u hwnd);
}
受保护的:
HWND HWND;
};
CreateMyWindow::CreateMyWindow(char const*标题、char const*类名、HINSTANCE HINSTANCE)
{
_hwnd=::CreateWindowEx(
(德沃德)类名,
(LPCWSTR)标题,
W_重叠,
CW_使用默认值,
CW_使用默认值,
CW_使用默认值,
CW_使用默认值,
0,
0,
无效的
hInstance,
0);
}
//由windows调用的MyWindow过程
LRESULT回调窗口过程(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
开关(信息)
{
案例WM_销毁:
PostQuitMessage(0);
返回0;
}
返回DefWindowProc(hwnd、message、wParam、lParam);
}
int-WINAPI-WinMain(HINSTANCE-hInst、HINSTANCE-hPrevInst、char*cmdParam、int-cmdShow)
{
char className[]=“Winnie”;
mywinglass mywinglass(WindowProcedure,className,hInst);
/*错误:参数列表参数类型中没有构造函数“MYWinClass::MYWinClass”的实例:(LRESULT\u stdcall(HWND-HWND,UINT-message,WPARAM-WPARAM,LPARAM-LPARAM)char[7],HINSTANCE)
//MyWinClass MyWinClass(WindowProcedure…)us在上面的错误下划线,我不知道为什么它将WNDPROC视为HWND*/
myWinClass.Register();
创建MyWindow myWndClass(“MyWindowClass”,类名,hInst);
myWndClass.ShowWindow(cmdShow);
味精;
while(GetMessage(&msg,NULL,0,0))
{
翻译信息(&msg);
发送消息(&msg);
}
返回msg.wParam;
}

问题不是您的窗口程序。它与
className
一起使用。您的构造函数被定义为采用
LPCWSTR
,也称为
wchar\t const*
。但是
className
char
的数组,而不是
wchar\t
的数组。您可以将此错误声明为:

wchar_t className[] = L"Winnie";

但是,您的代码还有很多其他问题,我现在不想讨论这些问题。

问题不是您的窗口过程。它与
className
一起使用。您的构造函数被定义为采用
LPCWSTR
,也称为
wchar\t const*
。但是
className
char
的数组,而不是
wchar\t
的数组。您可以将此错误声明为:

wchar_t className[] = L"Winnie";

但是,您的代码还有很多其他问题,我现在不想讨论这些问题。

您以错误的方式混合了ANSI和Unicode数据,并且您的一些类型转换是错误的。请尝试类似以下内容:

#include <Windows.h>

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

class MyWinClass
{
public:
    MyWinClass(WNDPROC winProc, LPCTSTR className, HINSTANCE hInst);

    void Register()
    {
        ::RegisterClassEx(&wcex);
    }

private:
    WNDCLASSEX wcex;
};

MyWinClass::MyWinClass(WNDPROC winProc, LPCTSTR className, HINSTANCE hInst)
{
    wcex.style = 0;
    wcex.lpfnWndProc = winProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInst;
    wcex.hIcon = 0;
    wcex.hCursor = LoadCursor(0, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = 0;
    wcex.lpszClassName = className;
}

class CreateMyWindow
{
public:
    CreateMyWindow() : _hwnd(0){}
    CreateMyWindow(LPCTSTR caption, LPCTSTR className, HINSTANCE hInstance);

    void ShowWindow(int cmdShow)
    {
        ::ShowWindow(_hwnd, cmdShow);
        ::UpdateWindow(_hwnd);
    }

protected:
    HWND _hwnd;
};

CreateMyWindow::CreateMyWindow(LPCTSTR caption, LPCTSTR className, HINSTANCE hInstance)
{
    _hwnd = ::CreateWindowEx(
        0,
        className,
        caption,
        WS_OVERLAPPED,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);
}


//MyWindow Procedure that is called by windows

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    TCHAR className[] = TEXT("Winnie");

    MyWinClass myWinClass(WindowProcedure, className, hInst);
    myWinClass.Register();

    CreateMyWindow myWndClass(TEXT("MyWindowClass"), className, hInst);
    myWndClass.ShowWindow(cmdShow);

    MSG msg;

    while (::GetMessage(&msg, NULL, 0, 0))
    {
        ::TranslateMessage(&msg);
        ::DispatchMessage(&msg);
    }

    return msg.wParam;
}
#包括
LRESULT回调窗口过程(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM);
Mywinglass类
{
公众:
MyWinClass(WNDPROC winProc、LPCTSTR类名称、HINSTANCE hInst);
无效登记册()
{
::RegisterClassEx(&wcex);
}
私人:
WNDCLASSEX wcex;
};
MyWinClass::MyWinClass(WNDPROC winProc、LPCTSTR类名称、HINSTANCE hInst)
{
wcex.style=0;
wcex.lpfnWndProc=winProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInst;
wcex.hIcon=0;
wcex.hCursor=LoadCursor(0,IDC_箭头);
wcex.hbrBackground=(HBRUSH)(彩色窗口+1);
wcex.lpszMenuName=0;
wcex.lpszClassName=className;
}
类CreateMyWindow
{
公众:
CreateMyWindow():hwnd(0){}
CreateMyWindow(LPCTSTR标题、LPCTSTR类名、HINSTANCE HINSTANCE);
void ShowWindow(int-cmdShow)
{
::ShowWindow(_-hwnd,cmdShow);
::UpdateWindow(\u hwnd);
}
受保护的:
HWND HWND;
};
CreateMyWindow::CreateMyWindow(LPCTSTR标题、LPCTSTR类名、HINSTANCE HINSTANCE)
{
_hwnd=::CreateWindowEx(
0,
类名,
说明文字
W_重叠,
CW_使用默认值,
CW_使用默认值,
CW_使用默认值,
CW_使用默认值,
无效的
无效的
hInstance,
无效);
}
//由windows调用的MyWindow过程
LRESULT回调窗口过程(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
开关(信息)
{
案例WM_销毁:
PostQuitMessage(0);
返回0;
}
返回DefWindowProc(hwnd、message、wParam、lParam);
}
int-WINAPI-WinMain(HINSTANCE-hInst、HINSTANCE-hPrevInst、char*cmdParam、int-cmdShow)
{
TCHAR className[]=文本(“Winnie”);
mywinglass mywinglass(WindowProcedure,className,hInst);
myWinClass.Register();
CreateMyWindow myWndClass(文本(“MyWindowClass”),类名,hInst);
myWndClass.ShowWindow(cmdShow);