internal static Rectangle ConstrainToScreenWorkingAreaBounds(Rectangle bounds) { return(ConstrainToBounds(Screen.GetWorkingArea(bounds), bounds)); }
private void InitFormsSize() { int tb_width = 0; // Max width of messagebox must be 60% of screen width int max_width = (int)(Screen.GetWorkingArea(this).Width * 0.6); // First we have to know the size of text + image Drawing.SizeF tsize = TextRenderer.MeasureString(msgbox_text, this.Font, max_width); text_rect.Size = tsize; if (icon_image != null) { tsize.Width += icon_image.Width + 10; if (icon_image.Height > tsize.Height) { // Place text middle-right text_rect.Location = new Point(icon_image.Width + space_image_text + space_border, (int)((icon_image.Height / 2) - (tsize.Height / 2)) + space_border); } else { text_rect.Location = new Point(icon_image.Width + space_image_text + space_border, 2 + space_border); } if (tsize.Height < icon_image.Height) { tsize.Height = icon_image.Height; } } else { text_rect.Location = new Point(space_border + button_space, space_border); } tsize.Height += space_border * 2; // Now we want to know the amount of buttons int buttoncount; switch (msgbox_buttons) { case MessageBoxButtons.OK: buttoncount = 1; break; case MessageBoxButtons.OKCancel: buttoncount = 2; break; case MessageBoxButtons.AbortRetryIgnore: buttoncount = 3; break; case MessageBoxButtons.YesNoCancel: buttoncount = 3; break; case MessageBoxButtons.YesNo: buttoncount = 2; break; case MessageBoxButtons.RetryCancel: buttoncount = 2; break; default: buttoncount = 0; break; } if (show_help) { buttoncount++; } // Calculate the width based on amount of buttons tb_width = (button_width + button_space) * buttoncount; // The form caption can also make us bigger SizeF caption = TextRenderer.MeasureString(Text, new Font(DefaultFont, FontStyle.Bold)); // Use the bigger of the caption size (plus some arbitrary borders/close button) // or the text size, up to 60% of the screen (max_size) Size new_size = new SizeF(Math.Min(Math.Max(caption.Width + 40, tsize.Width), max_width), tsize.Height).ToSize(); // Now we choose the good size for the form if (new_size.Width > tb_width) { this.ClientSize = new Size(new_size.Width + (space_border * 2), Height = new_size.Height + (space_border * 4)); } else { this.ClientSize = new Size(tb_width + (space_border * 2), Height = new_size.Height + (space_border * 4)); } // Now we set the left of the buttons button_left = (this.ClientSize.Width / 2) - (tb_width / 2) + 5; AddButtons(); size_known = true; // Still needs to implement defaultButton and options switch (msgbox_default) { case MessageBoxDefaultButton.Button2: { if (this.buttons[1] != null) { ActiveControl = this.buttons[1]; } break; } case MessageBoxDefaultButton.Button3: { if (this.buttons[2] != null) { ActiveControl = this.buttons[2]; } break; } } }