简体   繁体   中英

Chinese characters in Title bar

I am creating a Win32 application, ASCII only. However, when I create the window, the title bar shows chinese characters, instead of the intended text:

//registering the window class:
WNDCLASSEXA wc = { };

wc.hInstance = hThisInstance;
wc.lpszClassName = g_classname;
wc.lpfnWndProc = WndPrc;
wc.style = 0;
wc.cbSize = sizeof (WNDCLASSEXA);
wc.hIcon = (HICON) LoadImage(GetModuleHandle(NULL),
                             MAKEINTRESOURCE(IDI_APPICO), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
wc.hIconSm = (HICON) LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APPICO),
                            IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.cbClsExtra = 0;              /* no extra bytes after the window class */
wc.cbWndExtra = 0;                   /* structure or the window instance */
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); /*(HBRUSH) GetStockObject(WHITE_PEN);*/

if (!RegisterClassEx(&wc))
    return FAIL;
return OK;

    g_hwndMain = CreateWindow(g_classname, "Hello World", WS_OVERLAPPEDWINDOW,
                              g_progsettings.winX, 
                              g_progsettings.winY,
                              g_progsettings.winWidth,
                              g_progsettings.winHeight, 
                              0, 0, NULL, 0);

    if (!g_hwndMain) {
        MessageBox(NULL, "Couldn't make window", "CreateWindowEx", 
                          MB_OK | MB_ICONERROR);
        exit(EXIT_FAILURE);
    }

    if (g_progsettings.winX == -1 && g_progsettings.winY == -1)
        centerwin(g_hwndMain);

    /* Make the window visible on the screen */

    ShowWindow(g_hwndMain, nCmdShow);
    UpdateWindow(g_hwndMain);

Here is the titlebar:

在此处输入图片说明

When I run the program repeatedly, I still get the same titlebar. UNICODE is not #define d.

Unlike other similar questions, I am not casting a const char * into a LPCWSTR or vice versa, so that shouldn't be the cause of the problem.

Here is my WinMain (a lot of irrelevant details)

INT WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,LPSTR lpszArgument, int nCmdShow)
{
    MSG msg;
    HACCEL hAccel;
    BOOL msgRet;
    INITCOMMONCONTROLSEX icex;

    if (signal(SIGSEGV, onSegfault) == SIG_ERR) {
        errmsg("Failed to register handler for SIGSEGV", "");
        return EXIT_FAILURE;
    }

    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_STANDARD_CLASSES;
    if (InitCommonControlsEx(&icex) == FALSE) {
        fmtmsgbox(MB_ICONERROR, "extended common controls err: %s", fmtmsg());
        return EXIT_FAILURE;
    }

    if (winregister(hInst) == FAIL)
        errmsg("window class error", "window class");


    memset(&g_progsettings, 0, sizeof (settings));
    if (g_progsettings.readSettings() == FAIL)
        g_progsettings.setDefaults();

    mainwindow(hInst, nCmdShow);
    hAccel = LoadAccelerators(hInst, MAKEINTRESOURCEA(IDR_ACCEL));


    onCreat();

    while ((msgRet = GetMessageW(&msg, NULL, 0, 0)) != 0) {
        if (msgRet == -1) {
            errmsg("GetMessage -1", fmtmsg());
            exit(EXIT_FAILURE);
        }
        if (!g_fd.isdlgmsg(&msg)) {
            if (!TranslateAcceleratorW(g_hwndMain, hAccel, &msg)) {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }
    return 0;
}

You are creating an Ansi-based window via RegisterClassExA() and CreateWindowA() , but you are running a Unicode-based message loop by calling GetMessageW() (and TranslateAcceleratorW() ) instead of GetMessageA() (and TranslateAcceleratorA() ), and then you are using DispatchMessageA() to dispatch those messages. Presumably your WndProc is also calling DefWindowProcA() instead of DefWindowProcW() . So, even though you are creating an Ansi window with an Ansi title, the default window painting is not going to draw the title data correctly when processing Unicode messages as Ansi (or vise versa). You should not mix Ansi and Unicode APIs together when handling your windows. If you create an Ansi window, use an Ansi message loop. If you create a Unicode window (which you should be), use a Unicode message loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM