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 'आप'
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 theUNICODE
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:
Your code snippet still has two places where wide character functions are mandatory to support unicode titles. The
DefWindowProc
macro should becomeDefWindowProcW
and theDispatchMessage
macro should becomeDispatchMessageW
. The exception isLoadCursor
which can stay in the macro form due to the dependence on resourceIDC_CROSS
.FYI: The double while around your message loop is unnecessary because
GetMessageW
returns 0 whenPostQuitMessage(0)
is called, thus ending your message loop automatically.