0

我是初学者,我正在尝试编写我的第一个游戏。我正在关注前段时间制作的教程。这是我的代码:

#include <windows.h>


//Callback function

LRESULT CALLBACK window_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}



int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    //Create Window Class 

        WNDCLASS window_class = {};
        window_class.style = CS_HREDRAW | CS_VREDRAW;
        window_class.lpszClassName = L"Game Window Class";
        window_class.lpfnWndProc = window_callback;

    //Register Class

        RegisterClass(&window_class);
    

    //Create Window
    CreateWindow(window_class.lpszClassName, "My First Game!", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);




};

当我尝试编译代码时,它给了我这两个错误:

E0167   argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
    
C2664   'HWND CreateWindowExW(DWORD,LPCWSTR,LPCWSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)': cannot convert argument 3 from 'const char [15]' to 'LPCWSTR'  

我究竟做错了什么?

4

1 回答 1

1

当您使用 Windows API 而不明确指定 ANSI 版本(带后缀)或 Unicode 版本(带后缀)时,您应该使用TEXT来表达字符串文字。AW

错误的行:

window_class.lpszClassName = L"Game Window Class";
CreateWindow(window_class.lpszClassName, "My First Game!", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);

更正的行:

window_class.lpszClassName = TEXT("Game Window Class");
CreateWindow(window_class.lpszClassName, TEXT("My First Game!"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);
于 2021-05-16T09:23:56.450 回答