Search code examples
c#.netwinformsmessageboxtext-size

Determine window size for custom message box based on the text


I am writing a custom message box that should behave pretty much like the MessageBox class in WinForms. All is good except when trying to determine the size of the form (including the label control displaying text).

I'm not sure how to determine the size since there are a number of factors involved including string length, embedded spaces and new lines in the text, and screen size.

Looking at the .NET reference source did not help either since the scaling portion seems to be implemented natively.

Any pointers would be appreciated.


Solution

  • You need to determine the maximum width of the message, and then figure out how much space the text wrapping takes. The TextRenderer.MeasureText function can provide this information for you:

    string textMessage = "some really long message..."
    
    int maxWidth = Screen.GetWorkingArea(this).Width - 480;
    int useWidth = Math.Min(TextRenderer.MeasureText(textMessage, 
                            Control.DefaultFont).Width, maxWidth);
    useWidth = Math.Max(useWidth, 640);
    int useHeight = Math.Max(64, TextRenderer.MeasureText(textMessage, 
                                 Control.DefaultFont,
                                 new Size(useWidth, 0), 
                                 TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak)
                                 .Height);
    
    using (Form f = new Form()) {
      f.Text = "Test Message";
      f.FormBorderStyle = FormBorderStyle.FixedDialog;
      f.MinimizeBox = false;
      f.MaximizeBox = false;
      f.StartPosition = FormStartPosition.CenterScreen;
      f.ClientSize = new Size(useWidth + 8, useHeight + 8);
    
      Label l = new Label { AutoSize = false };
      l.Text = textMessage;
      l.Font = Control.DefaultFont;
      l.TextAlign = ContentAlignment.MiddleCenter;
      l.Anchor = AnchorStyles.Left | AnchorStyles.Top |
                 AnchorStyles.Right | AnchorStyles.Bottom;
      l.Location = new Point(4, 4);
      l.Size = new Size(f.ClientSize.Width - 8, f.ClientSize.Height - 8);
      f.Controls.Add(l);
    
      f.ShowDialog(this);
    }