标题栏中的汉字

时间:2015-10-27 01:34:35

标签: c++ winapi

我正在创建一个Win32应用程序,仅限ASCII。但是,当我创建窗口时,标题栏显示中文字符,而不是预期的文本:

//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);

这是标题栏:

enter image description here

当我反复运行程序时,我仍然得到相同的标题栏。 UNICODE不是#define d。

与其他类似的问题不同,我不会将const char *转换为LPCWSTR,反之亦然,因此这不应成为问题的原因。

这是我的WinMain(很多不相关的细节)

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

1 个答案:

答案 0 :(得分:8)

您正在通过RegisterClassExA()CreateWindowA()创建基于Ansi的窗口,但您正在通过调用GetMessageW()(和TranslateAcceleratorW())来运行基于Unicode的消息循环GetMessageA()(和TranslateAcceleratorA()),然后您使用DispatchMessageA()来分发这些消息。大概你的WndProc也在调用DefWindowProcA()而不是DefWindowProcW()。因此,即使您正在创建带有Ansi标题的Ansi窗口,默认窗口绘制也不会在将Unicode消息作为Ansi处理时正确绘制标题数据(或反之亦然)。在处理窗口时,不应将Ansi和Unicode API混合在一起。如果您创建一个Ansi窗口,请使用Ansi消息循环。如果您创建一个Unicode窗口(您应该使用它),请使用Unicode消息循环。