Can't display unicode on title bar in WIN32

368 Views Asked by At

I have been trying to create a Window whose title bar could be in any language. But I am getting something like ▯*. I writing as escape characters as of now. I am attesting the entirity of the code.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <string>

LRESULT CALLBACK WindowProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam) {
    switch(Message) {
        case WM_KEYDOWN: {
            switch(WParam) { 
                case 'O': { 
                    DestroyWindow(Window); 
                } break; 
            }
        } break;
        case WM_DESTROY: { 
            PostQuitMessage(0); 
        } break;
        default: { 
            return DefWindowProc(Window, Message, WParam,  LParam); 
        }
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, PSTR CmdLine, int CmdShow) {

    WNDCLASSW WindowClass = {0};
    const wchar_t* ClassName = L"MyWindowClass";

    WindowClass.lpfnWndProc = WindowProc;
    WindowClass.hInstance = Instance;
    WindowClass.lpszClassName = ClassName;
    WindowClass.hCursor = LoadCursor(NULL, IDC_CROSS);
    MessageBoxW(0, L"\u0906\u092a", L"\u0906\u092a", 0);

    if(!RegisterClassW(&WindowClass)) {
        MessageBoxW(0, L"RegisterClass failed", 0, 0);
        return GetLastError();
    }
    
    HWND Window = CreateWindowExW(0, ClassName, L"\u0906\u092a",
                                 WS_OVERLAPPEDWINDOW|WS_VISIBLE,
                                 CW_USEDEFAULT, CW_USEDEFAULT,
                                 CW_USEDEFAULT, CW_USEDEFAULT,
                                 0, 0, Instance, 0);
    
    if(!Window) {
        MessageBoxW(0, L"CreateWindowEx failed", 0, 0);
        return GetLastError();
    }
    
    int Running = 1;
    
    while(Running) {
        MSG Message;
        while(GetMessageW(&Message, NULL, 0, 0)) {
            if(Message.message == WM_QUIT) Running = 0;
            TranslateMessage(&Message);
            DispatchMessage(&Message);
        }
    }
    
    return 0;
}

I was expecting the title bar to be something like 'आप'

1

There are 1 best solutions below

0
On

You unintentionally mixed ansi and wide character api probably due to incorrect project settings. The general advice is to explicitly use the wide character functions noted by a W at the end of the function name (e.g: CreateWindowW, GetMessageW) whenever possible. Avoid mixing ansi and wide character functions and avoid mixing the compatibility macros with explicit functions. Otherwise make absolutly sure that the UNICODE and _UNICODE preprocessor directives are always defined as you would expect it.

If the unicode definitions are enabled, wide character functions are used resulting in success. This is likely the reason many people say it worked on their machine. Your issue can be reproduced by using the ansi functions instead of macros.

Microsoft in Unicode and ANSI Functions:

New applications should always call the Unicode versions. Many world languages require Unicode. If you use ANSI strings, it will be impossible to localize your application. The ANSI versions are also less efficient, because the operating system must convert the ANSI strings to Unicode at run time.

Your code snippet still has two places where wide character functions are mandatory to support unicode titles. The DefWindowProc macro should become DefWindowProcW and the DispatchMessage macro should become DispatchMessageW. The exception is LoadCursor which can stay in the macro form due to the dependence on resource IDC_CROSS.

FYI: The double while around your message loop is unnecessary because GetMessageW returns 0 when PostQuitMessage(0) is called, thus ending your message loop automatically.