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

博客介绍了在C++编程中遇到的类型不兼容问题,即尝试将`const char*`赋值给`LPCWSTR`时出现错误。通过引入`tchar.h`头文件,并使用`_T()`宏转换字符串,成功解决了这个问题。修改后的代码示例展示了如何正确地为窗口类名和窗口标题分配宽字符字符串。
摘要由CSDN通过智能技术生成

不同于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;
}

有更好的办法请告知

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一只秋暝

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

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

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

打赏作者

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

抵扣说明:

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

余额充值