【问题标题】:LPCWSTR will not Cast Correctly on TextOut() MethodLPCWSTR 不会在 TextOut() 方法上正确转换
【发布时间】:2014-02-07 07:14:41
【问题描述】:

完整的代码 sn-p...

#include <windows.h>
#include <string>
#include <vector>
using namespace std;
//=========================================================
// Globals.
HWND ghMainWnd = 0;
HINSTANCE ghAppInst = 0;
struct TextObj
{
    string s; // The string object.
    POINT p; // The position of the string, relative to the
    // upper-left corner of the client rectangle of
    // the window.
};
vector<TextObj> gTextObjs;

// Step 1: Define and implement the window procedure.
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    // Objects for painting.
    HDC hdc = 0;
    PAINTSTRUCT ps;
    TextObj to;
    switch( msg )
    {
        // Handle left mouse button click message.
        case WM_LBUTTONDOWN:
            {
                to.s = "Hello, World.";
                // Point that was clicked is stored in the lParam.
                to.p.x = LOWORD(lParam);
                to.p.y = HIWORD(lParam);
                // Add to our global list of text objects.
                gTextObjs.push_back( to );
                InvalidateRect(hWnd, 0, false);
                return 0;
            }
        // Handle paint message.
        case WM_PAINT:
            {
                hdc = BeginPaint(hWnd, &ps);
                for(int i = 0; i < gTextObjs.size(); ++i)
                TextOut(hdc,
                        gTextObjs[i].p.x,
                        gTextObjs[i].p.y,
                        gTextObjs[i].s.c_str(),
                        gTextObjs[i].s.size());
                        EndPaint(hWnd, &ps);
                return 0;
            }
        // Handle key down message.
        case WM_KEYDOWN:
            {
                if( wParam == VK_ESCAPE )
                DestroyWindow(ghMainWnd);
                return 0;
                // Handle destroy window message.
                case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
            }
    }
    // Forward any other messages we didn't handle to the
    // default window procedure.
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

问题是我从 Visual Studio 2012 收到一个错误,告诉我“const char*”类型的参数与 LPCWSTR 类型的参数不兼容。这发生在这行代码上:

hdc = BeginPaint(hWnd, &ps);
                for(int i = 0; i < gTextObjs.size(); ++i)
                TextOut(hdc,
                        gTextObjs[i].p.x,
                        gTextObjs[i].p.y,gTextObjs[i].s.c_str(), // <---happens here
                        gTextObjs[i].s.size());
                        EndPaint(hWnd, &ps);
                return 0;

我尝试进行转换 ((LPCWSTR)gTextObjs[i].s.c_str()),但显然这是非常错误的,因为每个 char 数组操作一个字节到两个?在此更改后还收到了"C4018: '&lt;' : signed/unsigned mismatch" 的警告。我是 C++ 新手,考虑到转换错误,我对这个错误非常迷茫。

我已经查看了 SO 中的一些不同线程,但似乎没有任何内容适合这种特定情况(或者我只是不太了解这些线程与我的线程之间的相关性)。 :[

另外,为了澄清......转换“有效”,但它打印了一些看起来很奇怪的日文/中文符号,而且总是不同。

【问题讨论】:

    标签: c++ wm-paint lpcwstr textout


    【解决方案1】:

    TextOut 正在解析为 TextOutW。这需要 UTF-16 文本。您传递 8 位文本。要么切换到保存在 wstring 中的 UTF-16,要么调用 TextOutA。

    【讨论】:

      猜你喜欢
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      • 2011-11-13
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多