diff --git a/FlexibleMessageBox/FlexibleMessageBox.cs b/FlexibleMessageBox/FlexibleMessageBox.cs index f8539cb..65c4e5b 100644 --- a/FlexibleMessageBox/FlexibleMessageBox.cs +++ b/FlexibleMessageBox/FlexibleMessageBox.cs @@ -566,8 +566,17 @@ private static void SetDialogSizes(FlexibleMessageBoxForm flexibleMessageBoxForm var stringRows = GetStringRows(text); if (stringRows == null) return; - //Calculate whole text height - var textHeight = TextRenderer.MeasureText(text, FONT).Height; + //Calculate margins + var marginWidth = flexibleMessageBoxForm.Width - flexibleMessageBoxForm.richTextBoxMessage.Width; + var marginHeight = flexibleMessageBoxForm.Height - flexibleMessageBoxForm.richTextBoxMessage.Height; + + flexibleMessageBoxForm.richTextBoxMessage.MaximumSize = new Size(flexibleMessageBoxForm.MaximumSize.Width - marginWidth, + flexibleMessageBoxForm.MaximumSize.Height - marginHeight); + + //Calculate whole text height considering maximum size for richTextBoxMessage and WordBreak + TextFormatFlags textFormatFlags = new TextFormatFlags(); + textFormatFlags |= TextFormatFlags.WordBreak; + var textHeight = TextRenderer.MeasureText(text, FONT, flexibleMessageBoxForm.richTextBoxMessage.MaximumSize, textFormatFlags).Height; //Calculate width for longest text line const int SCROLLBAR_WIDTH_OFFSET = 15; @@ -575,11 +584,6 @@ private static void SetDialogSizes(FlexibleMessageBoxForm flexibleMessageBoxForm 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; - int magicAdd = 12; - marginHeight += magicAdd; //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); diff --git a/FlexibleMessageBoxDemo/Form1.Designer.cs b/FlexibleMessageBoxDemo/Form1.Designer.cs index 7b3f57c..3be7e00 100644 --- a/FlexibleMessageBoxDemo/Form1.Designer.cs +++ b/FlexibleMessageBoxDemo/Form1.Designer.cs @@ -35,6 +35,7 @@ private void InitializeComponent() this.btnNormal = new System.Windows.Forms.Button(); this.btnOneLine = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); + this.buttonLongLine = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 @@ -72,10 +73,18 @@ private void InitializeComponent() this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // + // buttonLongLine + // + resources.ApplyResources(this.buttonLongLine, "buttonLongLine"); + this.buttonLongLine.Name = "buttonLongLine"; + this.buttonLongLine.UseVisualStyleBackColor = true; + this.buttonLongLine.Click += new System.EventHandler(this.buttonLongLine_Click); + // // Form1 // resources.ApplyResources(this, "$this"); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.buttonLongLine); this.Controls.Add(this.button3); this.Controls.Add(this.btnOneLine); this.Controls.Add(this.btnNormal); @@ -93,6 +102,7 @@ private void InitializeComponent() private System.Windows.Forms.Button btnNormal; private System.Windows.Forms.Button btnOneLine; private System.Windows.Forms.Button button3; + private System.Windows.Forms.Button buttonLongLine; } } diff --git a/FlexibleMessageBoxDemo/Form1.cs b/FlexibleMessageBoxDemo/Form1.cs index d714616..a29d319 100644 --- a/FlexibleMessageBoxDemo/Form1.cs +++ b/FlexibleMessageBoxDemo/Form1.cs @@ -82,6 +82,18 @@ private void btnOneLine_Click(object sender, EventArgs e) MessageBoxIcon.Information); } + private void buttonLongLine_Click(object sender, EventArgs e) + { + double MAX_WIDTH_FACTOR_start = FlexibleMessageBox.MAX_WIDTH_FACTOR; + FlexibleMessageBox.MAX_WIDTH_FACTOR = 0.3f; + FlexibleMessageBox.Show(this, + "MyApp v1.0.1 this is a long line message - this is a long line message - this is a long line message - this is a long line message - this is a long line message - this is a long line message - this is a long line message - this is a long line message - this is a long line message - this is a long line message - this is a long line message - this is a long line message - this is a long line message - ", + "MyApp", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + FlexibleMessageBox.MAX_WIDTH_FACTOR = MAX_WIDTH_FACTOR_start; + } + private void button3_Click(object sender, EventArgs e) { } diff --git a/FlexibleMessageBoxDemo/Form1.resx b/FlexibleMessageBoxDemo/Form1.resx index a32a9d0..eb89642 100644 --- a/FlexibleMessageBoxDemo/Form1.resx +++ b/FlexibleMessageBoxDemo/Form1.resx @@ -141,7 +141,7 @@ $this - 4 + 5 392, 132 @@ -165,7 +165,7 @@ $this - 3 + 4 64, 64 @@ -189,7 +189,7 @@ $this - 2 + 3 190, 64 @@ -213,7 +213,7 @@ $this - 1 + 2 502, 132 @@ -237,6 +237,30 @@ $this + 1 + + + 301, 64 + + + 261, 23 + + + 0 + + + Long Line with MAX_WIDTH_FACTOR=0.3 + + + buttonLongLine + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + 0 @@ -249,7 +273,7 @@ 659, 336 - Meiryo UI, 9pt + Microsoft Sans Serif, 9pt Form1