E0513 不能将“const char *”类型的值分配给“LPCWSTR”类型的实体

E0513 a value of type "const char *" cannot be assigned to an entity of type "LPCWSTR"

提问人:Mat 提问时间:2/4/2020 更新时间:2/4/2020 访问量:5450

问:

嗨,我试图在屏幕上画一个窗口,我在 2 周前写了这个,它有效了,但现在我重写了它,我遇到了错误?谁能帮忙? 错误是:“const char *”类型的 E0167 参数与“LPCWSTR”类型的参数不兼容 (和) E0513 无法将“const char *”类型的值分配给“LPCWSTR”类型的实体

#include<Windows.h>
#include<d2d1.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    if (uMsg == WM_DESTROY)
    {
        PostQuitMessage(0);
        return 0;

    }
    DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE prevInstance, LPWSTR cmd, int nCmdShow)
{
    WNDCLASSEX windowclass;
    ZeroMemory(&windowclass, sizeof(WNDCLASSEX));

    windowclass.cbSize = sizeof(WNDCLASSEX);
    windowclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
    windowclass.hInstance = hinstance;
    windowclass.lpfnWndProc = WindowProc;
    windowclass.lpszClassName = "CrystalWindow";
    windowclass.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClassEx(&windowclass);

    HWND windowHandle = CreateWindow("CrystalWindow", "Crystal Engine", WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, NULL, NULL, hinstance, 0);

    if (!windowHandle)
    {
        return -1;
    }

    ShowWindow(windowHandle, nCmdShow);

    MSG message;
    while (GetMessage(&message, NULL, 0, 0))
    {
        DispatchMessage(&message);
    }

    return 0;
}
可视化 C++

评论


答:

8赞 Vlad Feinstein 2/4/2020 #1

in 代表 .WLPCWSTRwide

您传递的是窄字符,而您的程序被编译为 UNICODE。

您可以为所有字符串添加前缀,也可以使用宏。L_T()

例如:

windowclass.lpszClassName = L"CrystalWindow";