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

Raymond Chen

Raymond

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

0 comments

Comments are closed. Login to edit/delete your existing comments