{"id":10343,"date":"2011-06-24T07:00:00","date_gmt":"2011-06-24T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/06\/24\/why-doesnt-my-messagebox-wrap-at-the-right-location\/"},"modified":"2011-06-24T07:00:00","modified_gmt":"2011-06-24T07:00:00","slug":"why-doesnt-my-messagebox-wrap-at-the-right-location","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110624-00\/?p=10343","title":{"rendered":"Why doesn&#039;t my MessageBox wrap at the right location?"},"content":{"rendered":"<p>A customer reported that the <code>MessageBox<\/code> function was wrapping text &#8220;at the wrong location.&#8221;<\/p>\n<blockquote class=\"q\"><p> Our program displays a message with the <code>MessageBox<\/code> function, and we use the &#8216;\\n&#8217; character to force line breaks at positions we choose. But we&#8217;ve found that starting in Windows&nbsp;Vista, the line breaks we placed are not working. The <code>MessageBox<\/code> function is inserting its own line breaks, which interferes with our custom text layout. It used to be that the width of the message box would expand to fit the longest line in the message. <\/p><\/blockquote>\n<p> The <code>MessageBox<\/code> function is one of those &#8220;leave the driving to us&#8221; type of functions. You give it a string to display, select which buttons you want, and sit back and relax while the <code>MessageBox<\/code> function does the work. The trade-off for the simplicity is that you also lose control over the experience. The <code>MessageBox<\/code> function decides where to place the dialog and how big to make it, which in turn determines where the line breaks go.\n The algorithm used by <code>MessageBox<\/code> to determine the size of the message box has changed many times over the lifetime of Windows. In Windows&nbsp;3.1, it was the width of the longest line, or 5\/8 of the width of the screen, whichever was smaller. Windows&nbsp;95 chose the smallest of the following which resulted in a dialog box that fit inside the working area:<\/p>\n<ul>\n<li>the width of the longest line, <\/li>\n<li>5\/8 of the width of the working area, <\/li>\n<li>3\/4 of the width of the working area, <\/li>\n<li>7\/8 of the width of the working area, <\/li>\n<\/ul>\n<p> Notice that even in Windows&nbsp;XP, the dialog box was not guaranteed to be the width of the longest line. If the longest line was more than 5\/8 of the width of the working area, the <code>MessageBox<\/code> is probably going to insert its own line breaks beyond ones you inserted explicitly.\n Windows&nbsp;Vista changed the algorithm again, in recognition of two things. First, monitors are now much larger than they were in 1995. And second, the consequence of these larger monitors is that the 7\/8 rule resulted in message boxes that were unreadable because they were 10 inches wide and half an inch tall. The old algorithm did not age well, but then again, it was written back in the days when the really cool kids had 1024&times;768 screens. Nowadays, even the kids from the wrong side of the tracks have screens that are regularly 1400 or even 1600 pixels wide.\n The new algorithm merely adds another option to the table of choices:<\/p>\n<ul>\n<li>the width of the longest line, <\/li>\n<li>278 DLU, <\/li>\n<li>5\/8 of the width of the working area, <\/li>\n<li>3\/4 of the width of the working area, <\/li>\n<li>7\/8 of the width of the working area, <\/li>\n<\/ul>\n<p> Note that the details of the <code>MessageBox<\/code> line breaking algorithm are provided for historical purposes only. Do not write code which relies on them, because who knows, they may change again in the next version of Windows.<\/p>\n<p> The algorithm was changed at the request of the visual design team; after all, it&#8217;s the job of the visual design team to make these sorts of visual design decisions. If you don&#8217;t want some black-turtleneck-wearing designer in Redmond to decide where your line breaks go, then don&#8217;t use <code>MessageBox<\/code>. And you don&#8217;t have to go and convert every single message box to its own dialog template; just write a function called <code>MessageBoxWithLineBreaksExactlyWhereIPutThem<\/code> function that takes the same parameters as <code>MessageBox<\/code> but lays out the text exactly the way you want it. (The <code>DT_CALCRECT<\/code> flag will be useful here.) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer reported that the MessageBox function was wrapping text &#8220;at the wrong location.&#8221; Our program displays a message with the MessageBox function, and we use the &#8216;\\n&#8217; character to force line breaks at positions we choose. But we&#8217;ve found that starting in Windows&nbsp;Vista, the line breaks we placed are not working. The MessageBox function [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-10343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer reported that the MessageBox function was wrapping text &#8220;at the wrong location.&#8221; Our program displays a message with the MessageBox function, and we use the &#8216;\\n&#8217; character to force line breaks at positions we choose. But we&#8217;ve found that starting in Windows&nbsp;Vista, the line breaks we placed are not working. The MessageBox function [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=10343"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10343\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=10343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=10343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=10343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}