cleanup MsgBox
This commit is contained in:
parent
1bea591e31
commit
d3cb2a4fb3
|
@ -1,9 +1,5 @@
|
|||
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
|
||||
|
@ -14,60 +10,111 @@ namespace BizHawk.Client.EmuHawk.CustomControls
|
|||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of the dialog box with a message and title.
|
||||
/// Create a new instance of the dialog box with a message and title and a standard windows MessageBox icon.
|
||||
/// </summary>
|
||||
/// <param name="message">Message text.</param>
|
||||
/// <param name="title">Dialog Box title.</param>
|
||||
public MsgBox(string message, string title)
|
||||
: this(message, title, MessageBoxIcon.None)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of the dialog box with a message and title and a standard windows messagebox icon.
|
||||
/// </summary>
|
||||
/// <param name="message">Message text.</param>
|
||||
/// <param name="title">Dialog Box title.</param>
|
||||
/// <param name="icon">Standard system messagebox icon.</param>
|
||||
public MsgBox(string message, string title, MessageBoxIcon icon)
|
||||
: this(message, title, getMessageBoxIcon(icon))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of the dialog box with a message and title and a custom windows icon.
|
||||
/// </summary>
|
||||
/// <param name="message">Message text.</param>
|
||||
/// <param name="title">Dialog Box title.</param>
|
||||
/// <param name="icon">Custom icon.</param>
|
||||
public MsgBox(string message, string title, Icon icon)
|
||||
/// <param name="boxIcon">Standard system MessageBox icon.</param>
|
||||
public MsgBox(string message, string title, MessageBoxIcon boxIcon)
|
||||
{
|
||||
var icon = GetMessageBoxIcon(boxIcon);
|
||||
InitializeComponent();
|
||||
|
||||
this.ControlBox = false; // Do not set in designer (causes problems with auto scaling)
|
||||
this.messageLbl.Text = message;
|
||||
this.Text = title;
|
||||
this.m_sysIcon = icon;
|
||||
ControlBox = false; // Do not set in designer (causes problems with auto scaling)
|
||||
messageLbl.Text = message;
|
||||
Text = title;
|
||||
_msgIcon = icon;
|
||||
|
||||
if (this.m_sysIcon == null)
|
||||
this.messageLbl.Location = new Point(FORM_X_MARGIN, FORM_Y_MARGIN);
|
||||
if (_msgIcon == null)
|
||||
{
|
||||
messageLbl.Location = new Point(FormXMargin, FormYMargin);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the button that was pressed.
|
||||
/// </summary>
|
||||
public DialogBoxResult DialogBoxResult { get; private set; }
|
||||
|
||||
public void SetMessageToAutoSize()
|
||||
{
|
||||
this.messageLbl.AutoSize = true;
|
||||
this.messageLbl.MaximumSize = new Size(this.MaximumSize.Width - this.m_sysIcon.Width - UIHelper.ScaleX(25), this.MaximumSize.Height);
|
||||
messageLbl.AutoSize = true;
|
||||
messageLbl.MaximumSize = new Size(MaximumSize.Width - _msgIcon.Width - UIHelper.ScaleX(25), MaximumSize.Height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get system icon for MessageBoxIcon.
|
||||
/// Create up to 3 buttons with given DialogResult values.
|
||||
/// </summary>
|
||||
/// <param name="icon">The MessageBoxIcon value.</param>
|
||||
/// <returns>SystemIcon type Icon.</returns>
|
||||
static Icon getMessageBoxIcon(MessageBoxIcon icon)
|
||||
/// <param name="names">Array of button names. Must of length 1-3.</param>
|
||||
/// <param name="results">Array of DialogResult values. Must be same length as names.</param>
|
||||
public void SetButtons(string[] names, DialogResult[] results)
|
||||
{
|
||||
SetButtons(names, results, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create up to 3 buttons with given DialogResult values.
|
||||
/// </summary>
|
||||
/// <param name="names">Array of button names. Must of length 1-3.</param>
|
||||
/// <param name="results">Array of DialogResult values. Must be same length as names.</param>
|
||||
/// <param name="def">Default Button number. Must be 1-3.</param>
|
||||
public void SetButtons(string[] names, DialogResult[] results, int def)
|
||||
{
|
||||
if (names == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(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
|
||||
_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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Paint the System Icon in the top left corner.
|
||||
/// </summary>
|
||||
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)
|
||||
{
|
||||
|
@ -84,97 +131,8 @@ namespace BizHawk.Client.EmuHawk.CustomControls
|
|||
}
|
||||
}
|
||||
|
||||
#region Setup API
|
||||
|
||||
/// <summary>
|
||||
/// Min set width.
|
||||
/// </summary>
|
||||
int m_minWidth;
|
||||
/// <summary>
|
||||
/// Min set height.
|
||||
/// </summary>
|
||||
int m_minHeight;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="width">Min width value.</param>
|
||||
/// <param name="height">Min height value.</param>
|
||||
public void SetMinSize(int width, int height)
|
||||
{
|
||||
m_minWidth = width;
|
||||
m_minHeight = height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create up to 3 buttons with no DialogResult values.
|
||||
/// </summary>
|
||||
/// <param name="names">Array of button names. Must of length 1-3.</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create up to 3 buttons with given DialogResult values.
|
||||
/// </summary>
|
||||
/// <param name="names">Array of button names. Must of length 1-3.</param>
|
||||
/// <param name="results">Array of DialogResult values. Must be same length as names.</param>
|
||||
public void SetButtons(string[] names, DialogResult[] results)
|
||||
{
|
||||
this.SetButtons(names, results, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create up to 3 buttons with given DialogResult values.
|
||||
/// </summary>
|
||||
/// <param name="names">Array of button names. Must of length 1-3.</param>
|
||||
/// <param name="results">Array of DialogResult values. Must be same length as names.</param>
|
||||
/// <param name="def">Default Button number. Must be 1-3.</param>
|
||||
public void SetButtons(string[] names, DialogResult[] results, int def)
|
||||
{
|
||||
if (names == null)
|
||||
throw new ArgumentNullException(nameof(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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The min required width of the button and checkbox row. Sum of button widths + checkbox width + margins.
|
||||
/// </summary>
|
||||
int m_minButtonRowWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Sets button text and returns the width.
|
||||
/// </summary>
|
||||
/// <param name="btn">Button object.</param>
|
||||
/// <param name="text">Text of the button.</param>
|
||||
/// <param name="tab">TabIndex of the button.</param>
|
||||
/// <param name="dr">DialogResult of the button.</param>
|
||||
/// <returns>Width of the button.</returns>
|
||||
static int setButtonParams(Button btn, string text, int tab, DialogResult dr)
|
||||
// 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;
|
||||
|
@ -183,163 +141,60 @@ namespace BizHawk.Client.EmuHawk.CustomControls
|
|||
return btn.Size.Width;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enables the checkbox. By default the checkbox is unchecked.
|
||||
/// </summary>
|
||||
/// <param name="text">Text of the checkbox.</param>
|
||||
public void SetCheckbox(string text)
|
||||
{
|
||||
this.SetCheckbox(text, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enables the checkbox and the default checked state.
|
||||
/// </summary>
|
||||
/// <param name="text">Text of the checkbox.</param>
|
||||
/// <param name="chcked">Default checked state of the box.</param>
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
private static readonly int FORM_Y_MARGIN = UIHelper.ScaleY(10);
|
||||
private static readonly int FORM_X_MARGIN = UIHelper.ScaleX(16);
|
||||
private static readonly int BUTTON_SPACE = UIHelper.ScaleX(5);
|
||||
private static readonly int CHECKBOX_SPACE = UIHelper.ScaleX(15);
|
||||
private static readonly int TEXT_Y_MARGIN = UIHelper.ScaleY(30);
|
||||
|
||||
/// <summary>
|
||||
/// Auto fits the dialog box to fit the text and the buttons.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the buttons and checkboxe location.
|
||||
/// </summary>
|
||||
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;
|
||||
SetButtons(new[] { "OK" }, new[] { DialogResult.OK });
|
||||
}
|
||||
|
||||
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);
|
||||
_minButtonRowWidth += 2 * FormXMargin; //add margin to the ends
|
||||
|
||||
SetDialogSize();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Icon Pain
|
||||
/// <summary>
|
||||
/// The icon to paint.
|
||||
/// </summary>
|
||||
Icon m_sysIcon;
|
||||
|
||||
/// <summary>
|
||||
/// Paint the System Icon in the top left corner.
|
||||
/// </summary>
|
||||
protected override void OnPaint(PaintEventArgs e)
|
||||
// Auto fits the dialog box to fit the text and the buttons.
|
||||
private void SetDialogSize()
|
||||
{
|
||||
if (m_sysIcon != null)
|
||||
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
|
||||
{
|
||||
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
|
||||
|
||||
/// <summary>
|
||||
/// If visible checkbox was checked.
|
||||
/// </summary>
|
||||
public bool CheckboxChecked
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.chkBx.Checked;
|
||||
}
|
||||
Width = requiredWidth > minSetWidth
|
||||
? requiredWidth
|
||||
: minSetWidth,
|
||||
Height = requiredHeight > minSetHeight
|
||||
? requiredHeight
|
||||
: minSetHeight
|
||||
};
|
||||
}
|
||||
|
||||
DialogBoxResult m_result;
|
||||
/// <summary>
|
||||
/// Gets the button that was pressed.
|
||||
/// </summary>
|
||||
public DialogBoxResult DialogBoxResult
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_result;
|
||||
}
|
||||
}
|
||||
|
||||
private void btn_Click(object sender, EventArgs e)
|
||||
private void ButtonClick(object sender, EventArgs e)
|
||||
{
|
||||
if (sender == btn1)
|
||||
this.m_result = DialogBoxResult.Button1;
|
||||
{
|
||||
DialogBoxResult = DialogBoxResult.Button1;
|
||||
}
|
||||
else if (sender == btn2)
|
||||
this.m_result = DialogBoxResult.Button2;
|
||||
{
|
||||
DialogBoxResult = DialogBoxResult.Button2;
|
||||
}
|
||||
else if (sender == btn3)
|
||||
this.m_result = DialogBoxResult.Button3;
|
||||
{
|
||||
DialogBoxResult = DialogBoxResult.Button3;
|
||||
}
|
||||
|
||||
if (((Button)sender).DialogResult == DialogResult.None)
|
||||
this.Close();
|
||||
if (((Button) sender).DialogResult == DialogResult.None)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public enum DialogBoxResult
|
||||
|
|
|
@ -60,7 +60,7 @@
|
|||
this.btn1.Text = "Button1";
|
||||
this.btn1.UseVisualStyleBackColor = true;
|
||||
this.btn1.Visible = false;
|
||||
this.btn1.Click += new System.EventHandler(this.btn_Click);
|
||||
this.btn1.Click += new System.EventHandler(this.ButtonClick);
|
||||
//
|
||||
// btn2
|
||||
//
|
||||
|
@ -73,7 +73,7 @@
|
|||
this.btn2.Text = "Button2";
|
||||
this.btn2.UseVisualStyleBackColor = true;
|
||||
this.btn2.Visible = false;
|
||||
this.btn2.Click += new System.EventHandler(this.btn_Click);
|
||||
this.btn2.Click += new System.EventHandler(this.ButtonClick);
|
||||
//
|
||||
// messageLbl
|
||||
//
|
||||
|
@ -96,7 +96,7 @@
|
|||
this.btn3.Text = "Button3";
|
||||
this.btn3.UseVisualStyleBackColor = true;
|
||||
this.btn3.Visible = false;
|
||||
this.btn3.Click += new System.EventHandler(this.btn_Click);
|
||||
this.btn3.Click += new System.EventHandler(this.ButtonClick);
|
||||
//
|
||||
// DialogBox
|
||||
//
|
||||
|
|
Loading…
Reference in New Issue