如果要改变Messagebox上按钮的文本和自动关闭Messagebox提示框,一种方法是自定义一个winform窗口模仿替代Messagebox,变通的实现效果,另一种方法是通过调用系统的API来实现,因为C#没有对Messagebox提供相关的关闭方法。
第一种方法(自定义winform窗口实现):
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private extern static bool MessageBeep(uint type);
[DllImport("Shell32.dll")]
public extern static int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);
private IntPtr[] largeIcon;
private IntPtr[] smallIcon;
private MyMsgBox newMessageBox;
private Label frmTitle;
private Label frmMessage;
private PictureBox pIcon;
private FlowLayoutPanel flpButtons;
private Icon frmIcon;
private Button btnOK;
private Button btnAbort;
private Button btnRetry;
private Button btnIgnore;
private Button btnCancel;
private Button btnYes;
private Button btnNo;
private DialogResult CYReturnButton;
/// <summary>
/// 对话框图标
/// </summary>
public enum MyIcon
{
Error,
Explorer,
Find,
Information,
Mail,
Media,
Print,
Question,
RecycleBinEmpty,
RecycleBinFull,
Stop,
User,
Warning
}
/// <summary>
/// 对话框上的按钮
/// </summary>
public enum MyButtons
{
AbortRetryIgnore,
OK,
OKCancel,
RetryCancel,
YesNo,
YesNoCancel
}
/// <summary>
/// 绘制对话框
/// </summary>
/// <param name="title">对话框标题</param>
private void BuildMessageBox(string title)
{
newMessageBox = new MyMsgBox();
newMessageBox.Text = title;
newMessageBox.Size = new System.Drawing.Size(400, 200);
newMessageBox.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
newMessageBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
newMessageBox.Paint += new PaintEventHandler(newMessageBox_Paint);
newMessageBox.BackColor = System.Drawing.Color.White;
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.RowCount = 3;
tlp.ColumnCount = 0;
tlp.Dock = System.Windows.Forms.DockStyle.Fill;
tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 22));
tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50));
tlp.BackColor = System.Drawing.Color.Transparent;
tlp.Padding = new Padding(2, 5, 2, 2);
frmTitle = new Label();
frmTitle.Dock = System.Windows.Forms.DockStyle.Fill;
frmTitle.BackColor = System.Drawing.Color.Transparent;
frmTitle.ForeColor = System.Drawing.Color.White;
frmTitle.Font = new Font("Tahoma", 9, FontStyle.Bold);
frmMessage = new Label();
frmMessage.Dock = System.Windows.Forms.DockStyle.Fill;
frmMessage.BackColor = System.Drawing.Color.White;
frmMessage.Font = new Font("Tahoma", 9, FontStyle.Regular);
frmMessage.Text = "hiii";
largeIcon = new IntPtr[250];
smallIcon = new IntPtr[250];
pIcon = new PictureBox();
ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 250);
flpButtons = new FlowLayoutPanel();
flpButtons.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
flpButtons.Padding = new Padding(0, 5, 5, 0);
flpButtons.Dock = System.Windows.Forms.DockStyle.Fill;
flpButtons.BackColor = System.Drawing.Color.FromArgb(240, 240, 240);
TableLayoutPanel tlpMessagePanel = new TableLayoutPanel();
tlpMessagePanel.BackColor = System.Drawing.Color.White;
tlpMessagePanel.Dock = System.Windows.Forms.DockStyle.Fill;
tlpMessagePanel.ColumnCount = 2;
tlpMessagePanel.RowCount = 0;
tlpMessagePanel.Padding = new Padding(4, 5, 4, 4);
tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50));
tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
tlpMessagePanel.Controls.Add(pIcon);
tlpMessagePanel.Controls.Add(frmMessage);
tlp.Controls.Add(frmTitle);
tlp.Controls.Add(tlpMessagePanel);
tlp.Controls.Add(flpButtons);
newMessageBox.Controls.Add(tlp);
}
/// <summary>
/// 显现对话框
/// </summary>
/// <param name="timeout">自动关闭时间,以毫秒为单位,如果设置为0则不自动关闭</param>
/// <param name="Message">对话框中显示的提示内容</param>
/// <returns></returns>
public DialogResult ShowMsg(int timeout, string Message)
{
if (timeout > 0)
{
StartTimer(timeout);
}
BuildMessageBox("");
frmMessage.Text = Message;
ShowOKButton();
newMessageBox.ShowDialog();
return CYReturnButton;
}
/// <summary>
/// 显示对话框