2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > “const char *“ 类型的实参与 “LPCWSTR“ 类型的形参不兼容 VScode

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

时间:2021-08-12 21:40:52

相关推荐

“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;}

以上是作者的解决问题的代码,作者也是一知半解的菜鸡,有更好的办法请告知

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。