using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; // 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 { /// /// Create a new instance of the dialog box with a message and title. /// /// Message text. /// Dialog Box title. public MsgBox(string message, string title) : this(message, title, MessageBoxIcon.None) { } /// /// 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 icon) : this(message, title, getMessageBoxIcon(icon)) { } /// /// Create a new instance of the dialog box with a message and title and a custom windows icon. /// /// Message text. /// Dialog Box title. /// Custom icon. public MsgBox(string message, string title, Icon icon) { InitializeComponent(); this.messageLbl.Text = message; this.Text = title; this.m_sysIcon = icon; if (this.m_sysIcon == null) this.messageLbl.Location = new Point(FORM_X_MARGIN, FORM_Y_MARGIN); } public void SetMessageToAutoSize() { this.messageLbl.AutoSize = true; this.messageLbl.MaximumSize = new Size(this.MaximumSize.Width - this.m_sysIcon.Width - 25, this.MaximumSize.Height); } /// /// Get system icon for MessageBoxIcon. /// /// The MessageBoxIcon value. /// SystemIcon type Icon. 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; } } #region Setup API /// /// Min set width. /// int m_minWidth; /// /// Min set height. /// int m_minHeight; /// /// Sets the min size of the dialog box. If the text or button row needs more size then the dialog box will size to fit the text. /// /// Min width value. /// Min height value. public void SetMinSize(int width, int height) { m_minWidth = width; m_minHeight = height; } /// /// Create up to 3 buttons with no DialogResult values. /// /// Array of button names. Must of length 1-3. public void SetButtons(params string[] names) { DialogResult[] drs = new DialogResult[names.Length]; for (int i = 0; i < names.Length; i++) drs[i] = DialogResult.None; this.SetButtons(names, drs); } /// /// 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. public void SetButtons(string[] names, DialogResult[] results) { this.SetButtons(names, results, 1); } /// /// 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. public void SetButtons(string[] names, DialogResult[] results, int def) { if (names == null) throw new ArgumentNullException("names", "Button Text is null"); int count = names.Length; if (count < 1 || count > 3) throw new ArgumentException("Invalid number of buttons. Must be between 1 and 3."); //---- Set Button 1 m_minButtonRowWidth += setButtonParams(btn1, names[0], def == 1 ? 1 : 2, results[0]); //---- Set Button 2 if (count > 1) { m_minButtonRowWidth += setButtonParams(btn2, names[1], def == 2 ? 1 : 3, results[1]) + BUTTON_SPACE; } //---- Set Button 3 if (count > 2) { m_minButtonRowWidth += setButtonParams(btn3, names[2], def == 3 ? 1 : 4, results[2]) + BUTTON_SPACE; } } /// /// The min required width of the button and checkbox row. Sum of button widths + checkbox width + margins. /// int m_minButtonRowWidth; /// /// Sets button text and returns the width. /// /// Button object. /// Text of the button. /// TabIndex of the button. /// DialogResult of the button. /// Width of the button. 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; } /// /// Enables the checkbox. By default the checkbox is unchecked. /// /// Text of the checkbox. public void SetCheckbox(string text) { this.SetCheckbox(text, false); } /// /// Enables the checkbox and the default checked state. /// /// Text of the checkbox. /// Default checked state of the box. public void SetCheckbox(string text, bool chcked) { this.chkBx.Visible = true; this.chkBx.Text = text; this.chkBx.Checked = chcked; this.m_minButtonRowWidth += this.chkBx.Size.Width + CHECKBOX_SPACE; } #endregion #region Sizes and Locations private void DialogBox_Load(object sender, EventArgs e) { if (!btn1.Visible) this.SetButtons(new string[] { "OK" }, new DialogResult[] { DialogResult.OK }); m_minButtonRowWidth += 2 * FORM_X_MARGIN; //add margin to the ends this.setDialogSize(); //this.setButtonRowLocations(); } const int FORM_Y_MARGIN = 10; const int FORM_X_MARGIN = 16; const int BUTTON_SPACE = 5; const int CHECKBOX_SPACE = 15; const int TEXT_Y_MARGIN = 30; /// /// Auto fits the dialog box to fit the text and the buttons. /// void setDialogSize() { int requiredWidth = this.messageLbl.Location.X + this.messageLbl.Size.Width + FORM_X_MARGIN; requiredWidth = requiredWidth > m_minButtonRowWidth ? requiredWidth : m_minButtonRowWidth; int requiredHeight = this.messageLbl.Location.Y + this.messageLbl.Size.Height - this.btn2.Location.Y + this.ClientSize.Height + TEXT_Y_MARGIN; int minSetWidth = this.ClientSize.Width > this.m_minWidth ? this.ClientSize.Width : this.m_minWidth; int minSetHeight = this.ClientSize.Height > this.m_minHeight ? this.ClientSize.Height : this.m_minHeight; Size s = new Size(); s.Width = requiredWidth > minSetWidth ? requiredWidth : minSetWidth; s.Height = requiredHeight > minSetHeight ? requiredHeight : minSetHeight; this.ClientSize = s; } /// /// Sets the buttons and checkboxe location. /// void setButtonRowLocations() { int formWidth = this.ClientRectangle.Width; int x = formWidth - FORM_X_MARGIN; int y = btn1.Location.Y; if (btn3.Visible) { x -= btn3.Size.Width; btn3.Location = new Point(x, y); x -= BUTTON_SPACE; } if (btn2.Visible) { x -= btn2.Size.Width; btn2.Location = new Point(x, y); x -= BUTTON_SPACE; } x -= btn1.Size.Width; btn1.Location = new Point(x, y); if (this.chkBx.Visible) this.chkBx.Location = new Point(FORM_X_MARGIN, this.chkBx.Location.Y); } #endregion #region Icon Pain /// /// The icon to paint. /// Icon m_sysIcon; /// /// Paint the System Icon in the top left corner. /// /// protected override void OnPaint(PaintEventArgs e) { if (m_sysIcon != null) { Graphics g = e.Graphics; g.DrawIconUnstretched(m_sysIcon, new Rectangle(FORM_X_MARGIN, FORM_Y_MARGIN, m_sysIcon.Width, m_sysIcon.Height)); } base.OnPaint(e); } #endregion #region Result API /// /// If visible checkbox was checked. /// public bool CheckboxChecked { get { return this.chkBx.Checked; } } DialogBoxResult m_result; /// /// Gets the button that was pressed. /// public DialogBoxResult DialogBoxResult { get { return m_result; } } private void btn_Click(object sender, EventArgs e) { if (sender == btn1) this.m_result = DialogBoxResult.Button1; else if (sender == btn2) this.m_result = DialogBoxResult.Button2; else if (sender == btn3) this.m_result = DialogBoxResult.Button3; if (((Button)sender).DialogResult == DialogResult.None) this.Close(); } #endregion } public enum DialogBoxResult { Button1, Button2, Button3 } }