Size of MessageBox

Minustar picture Minustar · Aug 5, 2011 · Viewed 12.9k times · Source

How does .NET's MessageBox determine its size relative to the resolution of the screen on which it is displayed?

I am writing a slightly more flexible dialog window for a WPF application. The layout of the window is layed out in a Grid:

+-----------------
| auto: Header      // A header for the dialog.
+-----------------
| auto: Content     // can be any FrameworkElement.
+-----------------
| auto: BottomPanel // With buttons <OK>, <Cancel>, <Delete>, etc. 
+-----------------

The Contentcell can be VERY large. In one of my use cases, the user wants to delete x elements from a list. The elements are then listed in the confirmation dialog. If there are many (let's say 50+) elements, the window can get large—too large for my taste.

What I would like is a function that determines the MaxHeightand MaxWidthproperties of the dialog window from the current screen in a way that mimics Microsoft's own MessageBoxdialog.

PS: I invoke the message dialog with the following static method:

// MessageDialog class
public static object Show(
        Window owner, 
        FrameworkElement content,
        string title,
        string header,
        params MessageDialogButton[] buttons
        );
/* The MessageDialogButton class has the following properties: 
 * Text, ReturnValue, IsDefault, IsCancel. The class produces 
 * System.Windows.Controls.Button objects that when clicked 
 * return the value of their ReturnValue property--which is then
 * returned by MessageDialog::Show(...)
 */

PPS: To determine the screen on which to display the dialog, the screen on which the MessageDialog window's Owner is located. As a fallback, the first (primary) screen is used.

Answer

Justin picture Justin · Aug 5, 2011

You aren't going to find anything like that documented, however according to Raymond in Windows Vista the message box algorithm determined the width of the message box by choosing the smallest of the following which resulted in a box that fits in the working area:

  • The width of the longest line
  • 278 DLU (Dialog units)
  • 5/8 of the width of the working area
  • 3/4 of the width of the working area
  • 7/8 of the width of the working area

(I interpred this to mean that (for example) the width will be 5/8th of the width of the working area unless this results in the dialog which is taller than the height of the working area, in which case it will use a wider width).

This should at least give you some pointers for choosing a maximum width that doesn't look out of place.

As far as I am aware the message box doesn't have a maximum height, but I imagine a similar algorithm would work nicely.

If your dialog genuinely is very large then you might want to consider just making the dialog resizable / maximizable. (I'm not a fan of dialogs that display lists that are too large but don't allow you to resize the dialog to a more suitable size).