September 26th, 2013

Don't forget, Unicode includes formatting characters which can be used to influence output formatting

Consider this simple function:

void Reminder(HWND hwnd)
{
    MessageBoxW(nullptr,
        L"Please print out this form in triplicate "
        L"and bring it to the assistance desk "
        L"on level 2.",
        L"Reminder", MB_OK);
}

Depending on your screen resolution and font choices, this may end up displaying like this:

Reminder
Please print out this form in triplicate and bring it to the assistance desk on level
2.

That line break was awfully unfortunate, stranding the number 2 on a line by itself. (In publishingspeak, this is known as a orphan.)

You can’t control where the Message­Box function will insert line breaks, but you can try to influence it with the use of Unicode formatting characters. Here, we can change the space before the 2 to a Unicode non-breaking space, U+00A0.

void Reminder(HWND hwnd)
{
    MessageBoxW(nullptr,
        L"Please print out this form in triplicate "
        L"and bring it to the assistance desk "
        L"on level" L"\u00A0" L"2.",
        // could also have been written
        // L"on level\u00A02.",
        // but is harder to read
        L"Reminder", MB_OK);
}

The result is slightly less awful.

Reminder
Please print out this form in triplicate and bring it to the assistance desk on
level 2.

Unfortunately, I haven’t had much luck with the soft hyphen, but the zero-width space seems to work.

    MessageBoxW(nullptr,
        L"Gooooooo\u200Booooooo\u200Booooooo\u200Booooooo\u200B"
        L"ooooooo\u200Booooooo\u200Booooooo\u200Booooooo\u200B"
        L"ooooooo\u200Booooooo\u200Booooooo\u200Booooooo\u200B"
        L"ooooooo\u200Booooooo\u200Booooooo\u200Booooooo\u200B"
        L"ooooooo\u200Bal!",
        L"Gentle reminder", MB_OK);
Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.