我用 C++ 编程的时间不长,但需要为我的学校制作一个 Win32 应用程序。老师在信息方面帮助了我很多,但经过几天的尝试,我仍然卡住了。
错误:
error C2440: '=' : cannot convert from 'const char [11]' to 'LPCWSTR'
error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [11]' to 'LPCWSTR'
error C2664: 'TextOutW' : cannot convert parameter 4 from 'char *' to 'LPCWSTR'
IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR"
不知道其他所有内容是否正确,但我现在只收到这 4 个错误
cpp文件:
/* Hoofdstuk 10, User Interface */
#include "Callback_NYCM.h"
// UI
int WINAPI WinMain(HINSTANCE thisInstance,HINSTANCE prevInstance,LPSTR lpCmdLine,int nShowCmd)
{
PAINTSTRUCT ps;
HDC hdc;
MSG msg;
HWND hwnd;
WNDCLASSEX wndclassex; //struct_WNDCLASSEX via windows.h
// toekenning
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = thisInstance;
wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor(thisInstance,IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = "WNDCLASSEX";
wndclassex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// functie aanroep
RegisterClassEx(&wndclassex);
// IfThen -> CreateWindows
if(!(hwnd = CreateWindowEx(NULL,"WNDCLASSEX","Hoofdstuk 10",WS_OVERLAPPEDWINDOW
| WS_VISIBLE,50,50,650,300,NULL,NULL,thisInstance,NULL)))
{
return 0;
}
// logische structuur
while(GetMessage(&msg, NULL, 0, 0))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
};
头文件:
/*Hoofdstuk 10, Deelnemer.h*/
//Declaratie
class Deelnemer
{
private:
char* nm;
public:
//Constructor
Deelnemer(){
}
//Methoden = prototype
void Deelnemer::Invoeren();
char* Deelnemer::WeergevenNaam();
};
//Implemenmtatie.Invoeren
void Deelnemer::Invoeren()
{
nm = "Roy";
}
//.Weergeven
char* Deelnemer::WeergevenNaam()
{
return nm;
}
回调_NYCM.h:
/*Hoofdstuk 10, Callback_NYCM*/
#include "Windows.h"
#include "Deelnemer.h"
// prototype
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam);
//Implementatie
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
//Constructie
PAINTSTRUCT ps;
HDC hdc;
MSG msg;
WNDCLASSEX wndclassex;
// HWND hwnd;
Deelnemer deelnemer1;
//UI
switch(message)
{
case WM_PAINT:
{
//Functieaanroep.Initialisatie
deelnemer1.Invoeren();
//.TextOut
TextOut(hdc,50,50,deelnemer1.WeergevenNaam(),
strlen(deelnemer1.WeergevenNaam()));
EndPaint(hwnd,&ps);
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
default:
{
return DefWindowProc(hwnd,message,wparam,lparam);
}
break;
}
return 0;
}
我认为我的构造函数或类似的东西是错误的,我的 char* Deelnemer::WeergevenNaam() 的返回值
有人可以解释一下我的代码有什么问题,以便我知道如何让它工作吗?
更新:
更新您的应用程序需要始终使用 UNICODE 字符串文字,即 L"MyString" 而不是 "MyString"。您还需要使用 WCHAR/wchar_t 代替 char
但是我如何用我的代码做到这一点,有人可以帮忙吗?
更新 2:
这解决了很多错误!
但是我在这部分还有一些错误
Deelnemer deelnemer1;
switch(message)
{
case WM_PAINT:
{
//Functieaanroep.Initialisatie
deelnemer1.Invoeren();
//.TextOut
TextOut(hdc,50,50,deelnemer1.WeergevenNaam(),
strlen(deelnemer1.WeergevenNaam()));
EndPaint(hwnd,&ps);
return 0;
}
所以错误在线:deelnemer1.WeergevenNaam()
-TextOutW' : 无法将参数 4 从 'char *' 转换为 'LPCWSTR'
-IntelliSense:“char *”类型的参数与“LPCWSTR”类型的参数不兼容
更新 3:
经过一些测试,我找到了一个解决方案(就像你们在下面所说的那样)但现在我只剩下这个了:
TextOut (hdc,50,50,deelnemer1.WeergevenNaam(),
//在 deelnemer1.weergevenNaam()
上,错误 C2664: 'TextOutW' : cannot convert parameter 4 from 'const char *'到“LPCWSTR”