问题描述
我没有在c ++编程非常长,但需要为我的学校做一个Win32应用程序。
错误:
pre> $例如,调用 CreateWindowEx CreateWindowExA 或 CreateWindowExW 。两个实现对于字符串参数(分别为 char * 和 wchar_t * )具有不同的参数类型。要使用ANSI /多字节编码,调用将是 CreateWindowExA(NULL,WNDCLASSEX,...)。对于UNICODE它看起来像 CreateWindowExW(NULL,LWNDCLASSEX,...)。
在预处理程序通过之后代码的外观,您可以使用编译器开关(假设您使用的是Microsoft编译器)。
注意:不要忘记阅读。
I haven't been programming in c++ very long, but need to make an Win32 application for my school. The teacher helped me a lot with information but after a few days of trying I am still stuck.
Errors:
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"
Don't know if all the other suff is right, but i only get those 4 error now
cpp file:
/* 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; };
header file:
/*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; }
callback_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; }
I think my constructor or something like that is wrong and my return value for char* Deelnemer::WeergevenNaam()
Could somebody explain me what is wrong in my code so I know how to get it working?
UPDATE:
But how do i do this with my code, could somebody help?
UPDATE 2:
That solved al lot of errors!
But i have some more errors left in this part
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; }
so the errors are on line: deelnemer1.WeergevenNaam()
-TextOutW' : cannot convert parameter 4 from 'char *' to 'LPCWSTR'
-IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR"
UPDATE 3:
After some testing i found a solution (like you guys said below) But now i only got this one left:TextOut (hdc,50,50,deelnemer1.WeergevenNaam(), // on the deelnemer1.weergevenNaam()
with error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char *' to 'LPCWSTR'
Your code is written to compile as ANSI but your solution settings include _UNICODE/UNICODE. You can either set your solution to use ANSI encoding by changing Character Set (on the General node of the Configuration Properties) from Use Unicode Character Set to Use Multi-Byte Character Set or update your application code (the latter is recommended).
Updating your application requires to use UNICODE string literals throughout, i.e. L"MyString" instead of "MyString". You also need to use WCHAR/wchar_t in place of char (where applicable) and call the wide versions of the Windows API. For many API calls there exists a wide version that has a W at the end, e.g. CreateWindowExW. If you are using the Standard C++ Library you will also want to make sure to use the UNICODE variants where character encoding is necessary (e.g. std::wstring instead of std::string). Additional information can be found at Text and Strings in Visual C++.
A bit more background about what is going on here: The Windows API and Microsoft's CRT implementation can be used to compile code using ANSI/Multi-Byte character set or UNICODE character set. To support both character encodings the C/C++ preprocessor replaces the respective character types and API calls with the specific implementations depending on whether or not the _UNICODE and UNICODE preprocessor symbol is defined.
For example, the call CreateWindowEx is expanded to either CreateWindowExA or CreateWindowExW. Both implementations have different parameter types for string arguments (char* and wchar_t* respectively). To use ANSI/Multi-Byte encoding the call would be CreateWindowExA(NULL,"WNDCLASSEX",...). For UNICODE it would look like CreateWindowExW(NULL,L"WNDCLASSEX",...).
To see what the code looks like after the preprocessor is through you can use the /P or /E compiler switches (assuming you're using the Microsoft Compiler).
Note: Don't forget to read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).
这篇关于c ++在win32学校应用程序中构建错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!