/// <summary> /// Calculate the dialogs start size (Try to auto-size width to show longest text row). /// Also set the maximum dialog size. /// </summary> /// <param name="flexibleMessageBoxForm">The FlexibleMessageBox dialog.</param> /// <param name="text">The text (the longest text row is used to calculate the dialog width).</param> /// <param name="text">The caption (this can also affect the dialog width).</param> private static void SetDialogSizes(FlexibleMessageBoxForm flexibleMessageBoxForm, string text, string caption) { //First set the bounds for the maximum dialog size flexibleMessageBoxForm.MaximumSize = new Size(Convert.ToInt32(SystemInformation.WorkingArea.Width * FlexibleMessageBoxForm.GetCorrectedWorkingAreaFactor(0.7)), Convert.ToInt32(SystemInformation.WorkingArea.Height * FlexibleMessageBoxForm.GetCorrectedWorkingAreaFactor(0.9))); //Get rows. Exit if there are no rows to render... var stringRows = GetStringRows(text); if (stringRows == null) { return; } //Calculate whole text height var textHeight = TextRenderer.MeasureText(text, FONT).Height; //Calculate width for longest text line const int SCROLLBAR_WIDTH_OFFSET = 15; var longestTextRowWidth = stringRows.Max(textForRow => TextRenderer.MeasureText(textForRow, FONT).Width); var captionWidth = TextRenderer.MeasureText(caption, SystemFonts.CaptionFont).Width; var textWidth = Math.Max(longestTextRowWidth + SCROLLBAR_WIDTH_OFFSET, captionWidth); //Calculate margins var marginWidth = flexibleMessageBoxForm.Width - flexibleMessageBoxForm.richTextBoxMessage.Width; var marginHeight = flexibleMessageBoxForm.Height - flexibleMessageBoxForm.richTextBoxMessage.Height; //Set calculated dialog size (if the calculated values exceed the maximums, they were cut by windows forms automatically) flexibleMessageBoxForm.Size = new Size(textWidth + marginWidth, textHeight + marginHeight); }
/// <summary> /// Calculate the dialogs start size (Try to auto-size width to show longest text row). /// Also set the maximum dialog size. /// </summary> /// <param name="flexibleMessageBoxForm">The FlexibleMessageBox dialog.</param> /// <param name="text">The text (the longest text row is used to calculate the dialog width).</param> private static void SetDialogSizes(FlexibleMessageBoxForm flexibleMessageBoxForm, string text) { //Set maximum dialog size flexibleMessageBoxForm.MaximumSize = new Size(Convert.ToInt32(SystemInformation.WorkingArea.Width * FlexibleMessageBoxForm.GetCorrectedWorkingAreaFactor(MAX_WIDTH_FACTOR)), Convert.ToInt32(SystemInformation.WorkingArea.Height * FlexibleMessageBoxForm.GetCorrectedWorkingAreaFactor(MAX_HEIGHT_FACTOR))); //Calculate dialog start size: Try to auto-size width to show longest text row var rowSize = Size.Empty; var maxTextRowWidth = 0; var maxTextRowHeight = 0f; var stringRows = GetStringRows(text); using (var graphics = flexibleMessageBoxForm.CreateGraphics()) { maxTextRowHeight = graphics.MeasureString(" ", FONT).Height; foreach (var textForRow in stringRows) { rowSize = graphics.MeasureString(textForRow, FONT, flexibleMessageBoxForm.MaximumSize.Width).ToSize(); if (rowSize.Width > maxTextRowWidth) { maxTextRowWidth = rowSize.Width; } } } //Set dialog start size flexibleMessageBoxForm.Size = new Size(maxTextRowWidth + flexibleMessageBoxForm.Width - flexibleMessageBoxForm.richTextBoxMessage.Width + 10, Convert.ToInt32(maxTextRowHeight * stringRows.Count()) + flexibleMessageBoxForm.Height - flexibleMessageBoxForm.richTextBoxMessage.Height + 10); }