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);
Comments (17)
  1. Dan Bugglin says:

    Just keep in mind that you can use Ctrl+C in (I think?) XP and up to copy the dialog contents… usually not a big deal but if you are putting something in the dialog the user might copy and paste into another program such as a url or a command line string… be careful of using such formatting tricks as they get copied as well!

  2. Doug says:

    If copying the text with formatting is a concern, Raymond's solution here works well. A non-breaking space should act just like a regular space if it were to appear in the middle of a line. Inserting a line break before level would not be as flexible.

    [Not only that, but it means that when you paste it, you get the non-breaking space, so the documented you pasted into also gets pleasant line breaks. Win-win! </typographynerd> -Raymond]
  3. Jason Warren says:

    Raymond, I've noticed over the years that you're pretty serious about not putting pictures in your blog (I'm sure you even have a blog post about this). I came here just to say that I'm very impressed how you imitated dialog windows using HTML tables (along with all the other tricks you've done over the years). They render even in my RSS reader!

  4. Miff says:

    I noticed something in the first older article you linked.

    > The old algorithm did not age well, but then again, it was written back in the days when the really cool kids had 1024×768 screens. Nowadays, even the kids from the wrong side of the tracks have screens that are regularly 1400 or even 1600 pixels wide.

    I have many feelings (sadness mostly) about the fact that screen sizes seem to be shrinking back down to 1366*768 as the standard. It took me forever to find a laptop with a suitable screen for less than 1000 bucks just last month, and god help you if you try to buy a non-professional-grade laptop from a brick and mortar store with a decent screen resolution.

  5. Adam Rosenfield says:

    @Jason Warren: Raymond does on rare occasion post real images (and even a video once or twice), but when he does, they're always images hosted elsewhere.  He can't upload new images to be hosted on this blog, AFAIK.  So if there happens to be an image already on the net that's relevant for the blog post (such as a screenshot from MSDN), he may use that, otherwise he recreates an approximation of the screenshot with HTML+CSS when possible.

  6. Anonymous Coward says:

    Fake imagery may be a nifty circumvention of the deficient blogging platform and all that rot, but it can lead to unintended effects. For me the ‘level’ gets its own line too, yielding a three-line message box.

  7. Anon says:

    @Nick

    I dream of the day when there are two Webs: The current, unvalidated one, and a second web that requires sites to meet 100% standards compliance, for their country of origin.

    Of course, I also dream of the day when Microsoft finally removes every single "compatibility" shim from IE, and all browsers report the exact same version information.

  8. Alex says:

    I should point out that the second dialog box has the same title text as the first and that title text doesn't really describe it accurately.

  9. Nick says:

    I've always been hesitant to use Unicode characters (even fairly common ones) in online "free-text" forms.  I've only gotten about a 50% success rate, and the cost of failure is kind of high.  Sending Mom flowers with a tag saying "Happy M?ther?s Day?? ?? L?ve y?u?? ??Nick" is less than ideal.

    With regards to images — Raymond *could* always use a data URI embedded in the blog post. Kind of bloated, but they do work very nicely in certain circumstances.  Not that I don't appreciate the text-rendered dialogs and whatnot. Reminds me of UIs built with DOS "drawing characters", but so much more fancy! :)

  10. cheong00 says:

    Regarding the HTML buttons, it'd be even better if there's onlick="return false;" on the buttons, so people who are curious like me won't get page refresh by try clicking the button.

    But perheps I shouldn't ask for too much. Good work.

  11. dx says:

    "onlick" sounds like the sort of thing that be more useful on touchscreens intended for cats.

  12. Neil says:

    @Anonymous Coward: Ah, the irony! (I have the same problem.)

  13. Cowardly Anonymous says:

    Anon: You're welcome to start this "second web where no sites are allowed without 100% standards compliance." See how many users you can get! (I predict 2)

  14. ulric says:

    it's crazy that messagebox doesn't suport n anymore since Vista, but it supports those unicode charcacters.  it sounds more like an unintended undocumented behaviour than a feature. or a technical work around for a designer's ego, the one that decided to no longer do n properly.

    [MessageBox doesn't care about n. It just hands the text to the static text control and lets the static text control figure it out. So go blame the static text control, not MessageBox. And the static text control just uses DrawText, so I guess the person to blame is DrawText. But DrawText uses LPK, so maybe the person to blame is LPK. Whatever it is, the problem is far, far away from MessageBox. -Raymond]
  15. Hm says:

    @ulric: What do you mean with "support n"? Did XP parse the text for this two characters? I don't see the use for this because you can embed a real line-break sequence  (CR+LF) into the string. And because file paths include , I don't see an easy way to distinguish between a n sequence meant as a line-break and n sequence as part of a filename.

  16. Miff says:

    @Anon – Oh it'll come when Microsoft and Mozilla finally fold and start using Blink like Opera did.

  17. Mark S says:

    I use this for smileys : )

    Tip: In Word and its ilk Ctrl-Shift-Space is non-breaking.

Comments are closed.

Skip to main content