“const char *“ 类型的实参与 “LPCWSTR“ 类型的形参不兼容,VScode

不同于VS系列,VScode我没有找到解决办法


问题:

wnd.lpszClassName = "WindowClass";

不能将 "const char *" 类型的值分配到 "LPCWSTR" 类型的实体

"const char *" 类型的实参与 "LPCWSTR" 类型的形参不兼容

解决办法:

头文件增加#include <tchar.h>

在报错的行,把字符串改成_T("字符串")的格式

例如

#include <tchar.h>

wnd.lpszClassName = "WindowClass";//问题的代码
wnd.lpszClassName = _T("WindowClass");//修改后

案例:

1.有问题的代码

#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_DESTROY)
		PostQuitMessage(0);
	else if (message == WM_QUIT)
		DestroyWindow(hwnd);
	return DefWindowProc(hwnd, message, wParam, lParam);
}


BOOL RegisterWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wnd;
	wnd.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wnd.lpfnWndProc = WndProc;
	wnd.style = CS_HREDRAW | CS_VREDRAW;
	wnd.lpszClassName = "WindowClass";
	wnd.hInstance = hInstance;
	wnd.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wnd.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
	wnd.hCursor = LoadCursor(hInstance, IDC_ARROW);
	wnd.cbSize = sizeof(WNDCLASSEX);
	wnd.lpszMenuName = nullptr;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	return RegisterClassEx(&wnd);
}
BOOL DisplayWindow(HWND &hwnd, HINSTANCE hInstance, HMENU hMenu, int nCmdShow)
{
	hwnd = CreateWindow("WindowClass", "TweeChaice", WS_OVERLAPPEDWINDOW, 0, 0, 800, 800,
						nullptr, nullptr, hInstance, nullptr);
	ShowWindow(hwnd, nCmdShow);
	return hwnd ? TRUE : FALSE;
}

HWND hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	if (!RegisterWindow(hInstance))
	{
		MessageBox(nullptr, "Error register!", "Error", MB_ICONERROR | MB_OK);
		return -1;
	}
	if (!DisplayWindow(hwnd, hInstance, nullptr, nCmdShow))
	{
		MessageBox(nullptr, "Error creating window!", "Error", MB_ICONERROR | MB_OK);
		return -1;
	}
	MSG msg;
	while (GetMessage(&msg, 0, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}

2.已解决问题的代码

#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_DESTROY)
		PostQuitMessage(0);
	else if (message == WM_QUIT)
		DestroyWindow(hwnd);
	return DefWindowProc(hwnd, message, wParam, lParam);
}


BOOL RegisterWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wnd;
	wnd.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wnd.lpfnWndProc = WndProc;
	wnd.style = CS_HREDRAW | CS_VREDRAW;
	wnd.lpszClassName = _T("WindowClass");
	wnd.hInstance = hInstance;
	wnd.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wnd.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
	wnd.hCursor = LoadCursor(hInstance, IDC_ARROW);
	wnd.cbSize = sizeof(WNDCLASSEX);
	wnd.lpszMenuName = nullptr;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	return RegisterClassEx(&wnd);
}
BOOL DisplayWindow(HWND &hwnd, HINSTANCE hInstance, HMENU hMenu, int nCmdShow)
{
	hwnd = CreateWindow(_T("WindowClass"), _T("TweeChaice"), WS_OVERLAPPEDWINDOW, 0, 0, 800, 800,
						nullptr, nullptr, hInstance, nullptr);
	ShowWindow(hwnd, nCmdShow);
	return hwnd ? TRUE : FALSE;
}

HWND hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	if (!RegisterWindow(hInstance))
	{
		MessageBox(nullptr, _T("Error register!"), _T("Error"), MB_ICONERROR | MB_OK);
		return -1;
	}
	if (!DisplayWindow(hwnd, hInstance, nullptr, nCmdShow))
	{
		MessageBox(nullptr, _T("Error creating window!"), _T("Error"), MB_ICONERROR | MB_OK);
		return -1;
	}
	MSG msg;
	while (GetMessage(&msg, 0, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}

有更好的办法请告知

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只秋暝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值