与 CreatWindow 功能作斗争
Struggling with the CreatWindow function
我是初学者,正在尝试编写我的第一款游戏。我正在关注前一段时间制作的教程。这是我的代码:
#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);
};
当我尝试编译代码时,它返回了以下 2 个错误:
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'
我做错了什么?
当你使用 Windows API 而不明确指定 ANSI 版本(带 A
后缀)或 Unicode 版本(带 W
后缀).
错误行:
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);
我是初学者,正在尝试编写我的第一款游戏。我正在关注前一段时间制作的教程。这是我的代码:
#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);
};
当我尝试编译代码时,它返回了以下 2 个错误:
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'
我做错了什么?
当你使用 Windows API 而不明确指定 ANSI 版本(带 A
后缀)或 Unicode 版本(带 W
后缀).
错误行:
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);