WinApi DrawText 和 TextOut 无法显示 unicode 字符 U+5167

wem*_*kon 2 unicode winapi drawtext textout

Windows 程序员您好!

我是使用 winapi 进行 Windows 编程的新手。我正在阅读这本非常好的书,当我使用 DrawText 和 TextOut 在客户区显示 unicode 字符 U+5167 (?) 时遇到了问题(显示为黑框)。神秘的是,这个特殊的 unicode 字符正确显示在 windows 标题区域中。当我使用 MessageBox 显示它时,这个 unicode 字符也能正确显示。最后,我尝试显示与此 unicode 相对接近的其他 unicode 字符,如 U+5166、U+5168、U+5157 和 U+5177;

这是标准定义的此 unicode 字符的链接。 http://unicode-table.com/en/#5167

注意:我正在使用 Visual Studio 2010 使用 Unicode 编译此代码

下面是我的代码。


#include<windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("HelloWin");
    HWND hwnd;
    MSG msg;

    WNDCLASS wndclass;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if(!RegisterClass (&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName,
                            TEXT("?Sample text ?? ??? ?? ?????? ?????"),
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            NULL,
                            NULL,
                            hInstance,
                            NULL);
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    LPTSTR c = TEXT("?Sample text ?? ??? ?? ?????? ?????");

    switch(message)
    {
        case WM_CREATE:
            PlaySound(TEXT("shutda.wav"), NULL, SND_FILENAME | SND_ASYNC);
MessageBox(hwnd, c, c, 0);
            return 0;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);         
            DrawTextEx(hdc, c, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER, NULL);
            TextOut(hdc, 100, 100, TEXT("?? ??? ????? unicode"), 19);
            EndPaint(hwnd, &ps);
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}
Run Code Online (Sandbox Code Playgroud)

对于非 Windows 程序员,您可以通过将其直接复制并粘贴到 .cpp 文件来编译和运行此代码。如果您使用的是 VS2010,您只需要创建新的“Win32 应用程序”项目并选择“空项目”。之后,您需要在项目的源文件夹中添加一个 cpp 文件,例如“test.cpp”。然后将代码复制并粘贴到“test.cpp”,然后构建并运行它。你现在应该看到我的问题了。:)

Dav*_*nan 5

当有效字符显示为矩形框时,表示该字体不包含字符的字形。为了解决这个问题,您需要使用一种字体,该字体确实具有该字符的字形。