using System; using System.Drawing; using System.Windows.Forms; using BizHawk.Common; // http://www.codeproject.com/Articles/154680/A-customizable-NET-WinForms-Message-Box namespace BizHawk.Client.EmuHawk.CustomControls { /// /// A customizable Dialog box with 3 buttons, custom icon, and checkbox. /// partial class MsgBox : Form { private readonly Icon _msgIcon; private static readonly int FormYMargin = UIHelper.ScaleY(10); private static readonly int FormXMargin = UIHelper.ScaleX(16); private static readonly int ButtonSpace = UIHelper.ScaleX(5); private static readonly int TextYMargin = UIHelper.ScaleY(30); // The min required width of the button and checkbox row. Sum of button widths + checkbox width + margins. private int _minButtonRowWidth; /// /// Create a new instance of the dialog box with a message and title and a standard windows MessageBox icon. /// /// Message text. /// Dialog Box title. /// Standard system MessageBox icon. public MsgBox(string message, string title, MessageBoxIcon boxIcon) { var icon = GetMessageBoxIcon(boxIcon); InitializeComponent(); ControlBox = false; // Do not set in designer (causes problems with auto scaling) messageLbl.Text = message; Text = title; _msgIcon = icon; if (_msgIcon == null) { messageLbl.Location = new Point(FormXMargin, FormYMargin); } } /// /// Gets the button that was pressed. /// public DialogBoxResult DialogBoxResult { get; private set; } public void SetMessageToAutoSize() { messageLbl.AutoSize = true; messageLbl.MaximumSize = new Size(MaximumSize.Width - _msgIcon.Width - UIHelper.ScaleX(25), MaximumSize.Height); } /// /// Create up to 3 buttons with given DialogResult values. /// /// Array of button names. Must of length 1-3. /// Array of DialogResult values. Must be same length as names. /// Default Button number. Must be 1-3. /// length of is not in range 1..3 /// is null public void SetButtons(string[] names, DialogResult[] results, int def = 1) { if (names == null) { throw new ArgumentNullException(nameof(names), "Button Text is null"); } int count = names.Length; if (!1.RangeTo(3).Contains(count)) { throw new ArgumentException("Invalid number of buttons. Must be between 1 and 3."); } //---- Set Button 1 _minButtonRowWidth += SetButtonParams(btn1, names[0], def == 1 ? 1 : 2, results[0]); //---- Set Button 2 if (count > 1) { _minButtonRowWidth += SetButtonParams(btn2, names[1], def == 2 ? 1 : 3, results[1]) + ButtonSpace; } //---- Set Button 3 if (count > 2) { _minButtonRowWidth += SetButtonParams(btn3, names[2], def == 3 ? 1 : 4, results[2]) + ButtonSpace; } } /// /// Paint the System Icon in the top left corner. /// protected override void OnPaint(PaintEventArgs e) { if (_msgIcon != null) { Graphics g = e.Graphics; g.DrawIconUnstretched(_msgIcon, new Rectangle(FormXMargin, FormYMargin, _msgIcon.Width, _msgIcon.Height)); } base.OnPaint(e); } // Get system icon for MessageBoxIcon. private static Icon GetMessageBoxIcon(MessageBoxIcon icon) { switch (icon) { case MessageBoxIcon.Asterisk: return SystemIcons.Asterisk; case MessageBoxIcon.Error: return SystemIcons.Error; case MessageBoxIcon.Exclamation: return SystemIcons.Exclamation; case MessageBoxIcon.Question: return SystemIcons.Question; default: return null; } } // Sets button text and returns the width. private static int SetButtonParams(Button btn, string text, int tab, DialogResult dr) { btn.Text = text; btn.Visible = true; btn.DialogResult = dr; btn.TabIndex = tab; return btn.Size.Width; } private void DialogBox_Load(object sender, EventArgs e) { if (!btn1.Visible) { SetButtons(new[] { "OK" }, new[] { DialogResult.OK }); } _minButtonRowWidth += 2 * FormXMargin; //add margin to the ends SetDialogSize(); } // Auto fits the dialog box to fit the text and the buttons. private void SetDialogSize() { int requiredWidth = messageLbl.Location.X + messageLbl.Size.Width + FormXMargin; requiredWidth = requiredWidth > _minButtonRowWidth ? requiredWidth : _minButtonRowWidth; int requiredHeight = messageLbl.Location.Y + messageLbl.Size.Height - btn2.Location.Y + ClientSize.Height + TextYMargin; int minSetWidth = ClientSize.Width; int minSetHeight = ClientSize.Height; ClientSize = new Size { Width = requiredWidth > minSetWidth ? requiredWidth : minSetWidth, Height = requiredHeight > minSetHeight ? requiredHeight : minSetHeight }; } private void ButtonClick(object sender, EventArgs e) { if (sender == btn1) { DialogBoxResult = DialogBoxResult.Button1; } else if (sender == btn2) { DialogBoxResult = DialogBoxResult.Button2; } else if (sender == btn3) { DialogBoxResult = DialogBoxResult.Button3; } if (((Button) sender).DialogResult == DialogResult.None) { Close(); } } } public enum DialogBoxResult { Button1, Button2, Button3 } }