简体   繁体   中英

Why can I not set "Game Window Class" as my class name?

I'm wondering why I cannot set the name. This is the error:

图片

It doesn't even let me assign a char.

#include <windows.h>

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 = "Game Window Class";
    window_class.lpfnWndProc = window_callback;//callback
    
    //Register class

    //Create window
}

WNDCLASS::lpszClassName is an LPCTSTR pointer, ie a const TCHAR* pointer. TCHAR maps to wchar_t when UNICODE is defined, otherwise it maps to char instead.

You are compiling your project with UNICODE defined, because lpszClassName is expecting a wide const wchar_t* pointer to a Unicode string, but you are giving it a (decayed) narrow const char* pointer to an ANSI string literal instead, hence the error.

You can either:

  • undefine UNICODE in your project settings.

  • prefix the string literal with L to make it a Unicode string:

    window_class.lpszClassName = L"Game Window Class";

  • wrap the string literal in the TEXT() macro :

    window_class.lpszClassName = TEXT("Game Window Class");

window_class.lpszClassName = "Game Window Class";

has breaks in the text. Try instead Game_Window_Class or just GameWindowClass

LPCWSTR is a Type.

An LPCWSTR is a 32-bit pointer to a constant string of 16-bit Unicode characters, which MAY be null-terminated

ref: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/76f10dd8-699d-45e6-a53c-5aefc586da20

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