1

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
    Make sure you are using "Unicode Character Set" under "Project Properties -> Configuration Properties -> Advanced -> Character Set" or " Unicode" in the .vcxproj file. – Christian.K Jan 02 '23 at 07:01
  • Does this answer your question? [How to enable unicode when compiling using cl.exe from Visual Studio Build Tools?](https://stackoverflow.com/questions/71040396/how-to-enable-unicode-when-compiling-using-cl-exe-from-visual-studio-build-tools) – Christian.K Jan 02 '23 at 07:02
  • If I'm understanding you correctly, you are saying that this code (with escape characters) is working. Would it not have been better to also post the code that is not working, since that's the code you want help with? – john Jan 02 '23 at 07:16
  • @john It's not even working with the escape characters – Ashutosh Jha Jan 02 '23 at 07:54
  • 1
    Works fine for me running on Windows 10 https://i.imgur.com/iYfrbUT.png https://i.imgur.com/Bv2DMVo.png The code is good, so it's more a Windows setup issue. Maybe you're missing fonts and/or languages. – Simon Mourier Jan 02 '23 at 08:06
  • Incorrect font seems like a possible explanation. Not all fonts can display all Unicode characters. – john Jan 02 '23 at 08:39
  • No repro for me on Windows 10. Pictures here: https://imgur.com/a/jaJvxUE – selbie Jan 02 '23 at 08:48
  • 1
    Since we cannot tell from the code provided, are you calling `DefWindowProcA` or `DefWindowProcW`? – IInspectable Jan 02 '23 at 09:06
  • Wierdly enough I am able to get Message box correct not the window https://postimg.cc/3WZbYY22 Working Message Box https://postimg.cc/2VKGS6yL/82eda8b6 Not working Window Title Should I test any other language? – Ashutosh Jha Jan 02 '23 at 09:43
  • @john It's not working with escape characters as well....What I meant was to test in escape character... Coz if it works I will make an converter easily.. but first this should run correctly... Please refer to image I posted... Is it due to a save format? Or compiler thing? – Ashutosh Jha Jan 02 '23 at 09:45
  • @AshutoshJha change `DefWindowProc` to `DefWindowProcW` to match all of the other W functions you are calling. Also, `if(Message.message == WM_QUIT)` in your message loop will never evaluate to true since `GetMessage()` returns 0, breaking the loop, when `WM_QUIT` is received. – Remy Lebeau Jan 02 '23 at 10:26
  • It also works fine for me on win11.Did you compile your application as Unicode? I suggest you could refer to the thread: https://stackoverflow.com/questions/417004/unicode-characters-in-window-caption – Jeaninez - MSFT Jan 03 '23 at 01:53

0 Answers0