c++在win32学校应用程序上构建错误

标签 c++ winapi char

我使用 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;
}

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

我认为我的构造函数或类似的东西是错误的,我对 char* Deelnemer::WeergevenNaam() 的返回值

有人可以向我解释我的代码中有什么问题,以便我知道如何让它工作吗?

更新:

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

但是我该如何用我的代码做到这一点,有人可以帮忙吗?

更新 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”:无法将参数 4 从“const char *”转换为“LPCWSTR”

最佳答案

您的代码被编写为编译为 ANSI,但您的解决方案设置包括 _UNICODE/UNICODE。您可以将您的解决方案设置为使用 ANSI 编码,方法是将字符集(在配置属性常规 节点上)从使用Unicode 字符集使用多字节字符集 或更新您的应用程序代码(推荐后者)。

更新您的应用程序需要在整个过程中使用 UNICODE 字符串文字,即 L"MyString" 而不是 "MyString"。您还需要使用 WCHAR/wchar_t 代替 char(如果适用)并调用 Windows API 的广泛版本。对于许多 API 调用,存在一个宽版本,其末尾有一个 W,例如创建窗口ExW。如果您使用的是标准 C++ 库,您还需要确保在需要字符编码的地方使用 UNICODE 变体(例如 std::wstring 而不是 std::string).可以在 Text and Strings in Visual C++ 找到更多信息。 .

关于这里发生的事情的更多背景信息:Windows API 和 Microsoft 的 CRT 实现可用于使用 ANSI/多字节字符集或 UNICODE 字符集编译代码。为了支持这两种字符编码,C/C++ 预处理器根据是否定义了 _UNICODEUNICODE 预处理器符号,将相应的字符类型和 API 调用替换为特定的实现。

例如,调用 CreateWindowEx 被扩展为 CreateWindowExACreateWindowExW。两种实现都有不同的字符串参数类型(分别为 char*wchar_t*)。要使用 ANSI/多字节编码,调用将是 CreateWindowExA(NULL,"WNDCLASSEX",...)。对于 UNICODE,它看起来像 CreateWindowExW(NULL,L"WNDCLASSEX",...)

要查看预处理器完成后代码的样子,您可以使用 /P/E编译器开关(假设您使用的是 Microsoft 编译器)。

注意:不要忘记阅读 The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) .

关于c++在win32学校应用程序上构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18470791/

相关文章:

c++ - 通用结束迭代器与容器的可递减要求 `end()`

c++ - 编写一个递归函数来反转输入字符串

C++/Win32 最小可能的 MessageBox ("Hello") app exe?

c - C语言从键盘输入一个字符

C++:将字符串文字作为字符串传递并导致编译失败,为什么

c++ - 在 C++ 中重载运算符

c++ - #define 以及如何使用它们 - C++

winapi - 使用 Windows API 获取文件的文件类型

c++ - GetThreadContext 返回错误 6,句柄无效?

c - 值(value)在C++中正在发生变化,我不知道为什么