internal System.Windows.Forms.DialogResult ShowDialog(String message, String caption, String description, SWDialogBox.MessageBoxButton button, SWDialogBox.MessageBoxIcon icon) { try { String iconKey = null; System.Resources.ResourceManager rm = new System.Resources.ResourceManager("SWDialogBox.SWDialogBox", this.GetType().Assembly); //根据当前的消息编号得到其对应的消息类型(设置消息框图标类型) switch((icon & (SWDialogBox.MessageBoxIcon)ICONMASKWORD)) { case SWDialogBox.MessageBoxIcon.Critical: iconKey = "ICON-Critical"; break; case SWDialogBox.MessageBoxIcon.Question: iconKey = "ICON-Question"; break; case SWDialogBox.MessageBoxIcon.Exclamation: iconKey = "ICON-Exclamation"; break; case SWDialogBox.MessageBoxIcon.Information: iconKey = "ICON-Information"; break; } //装载消息框指示图标 if(iconKey != null && iconKey != String.Empty) this.MessageIcon.Image = ((System.Drawing.Icon)rm.GetObject(iconKey)).ToBitmap(); //装载消息框窗体图标 // if(this.Icon != null) // this.Icon = (System.Drawing.Icon)rm.GetObject("ICON-App"); //设置消息框中各个信息文本 this.Text = caption; lblMessage.Text = message; txtDescription.Text = description; //得到消息文本框的Graphics对象 Graphics g = lblMessage.CreateGraphics(); //得到将显示的消息文本的显示区域/大小 SizeF messageSize = new SizeF(); messageSize = g.MeasureString(lblMessage.Text, lblMessage.Font, lblMessage.Width); //设置消息文本框的高度 lblMessage.Height = (Int32)messageSize.Height; //设置按钮的位置 btnDetail.Top = lblMessage.Top + lblMessage.Height + 32; //设置消息框窗体的高度 this.ClientSize = new System.Drawing.Size(this.ClientSize.Width, btnDetail.Top + btnDetail.Height + 16); //创建消息框的按钮并排版消息框窗体界面 this.CreateButton(button); //打开模式对话框 return this.ShowDialog(); } catch { throw; } }
private void CreateButton(SWDialogBox.MessageBoxButton buttonStyle) { System.Resources.ResourceManager rm = new System.Resources.ResourceManager("SWDialogBox.SWDialogBox", this.GetType().Assembly); System.Collections.ArrayList buttonList = new System.Collections.ArrayList(6); //判断是否需要设置“确定(OK)”按钮 if((buttonStyle & (SWDialogBox.MessageBoxButton)BUTTONMASKWORD) == SWDialogBox.MessageBoxButton.OK || (buttonStyle & SWDialogBox.MessageBoxButton.OKCancel) == SWDialogBox.MessageBoxButton.OKCancel) { System.Windows.Forms.Button btnOK = new System.Windows.Forms.Button(); btnOK.Anchor = System.Windows.Forms.AnchorStyles.Top; btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; btnOK.Location = new System.Drawing.Point(-100, this.btnDetail.Top); btnOK.Name = "btnOK"; btnOK.Size = new System.Drawing.Size(80, 24); btnOK.Text = rm.GetString(OKBUTTON); buttonList.Add(btnOK); this.Controls.Add(btnOK); } //判断是否需要设置“重试(Retry)”按钮 if((buttonStyle & SWDialogBox.MessageBoxButton.RetryCancel) == SWDialogBox.MessageBoxButton.RetryCancel) { System.Windows.Forms.Button btnRetry = new System.Windows.Forms.Button(); btnRetry.Anchor = System.Windows.Forms.AnchorStyles.Top; btnRetry.DialogResult = System.Windows.Forms.DialogResult.Retry; btnRetry.Location = new System.Drawing.Point(-100, this.btnDetail.Top); btnRetry.Name = "btnRetry"; btnRetry.Size = new System.Drawing.Size(80, 24); btnRetry.Text = rm.GetString(RETRYBUTTON); buttonList.Add(btnRetry); this.Controls.Add(btnRetry); } //判断是否需要设置“是(Yes)”和“否(No)”按钮 if((buttonStyle & SWDialogBox.MessageBoxButton.YesNo) == SWDialogBox.MessageBoxButton.YesNo || (buttonStyle & SWDialogBox.MessageBoxButton.YesNoCancel) == SWDialogBox.MessageBoxButton.YesNoCancel) { System.Windows.Forms.Button btnYes = new System.Windows.Forms.Button(); System.Windows.Forms.Button btnNo = new System.Windows.Forms.Button(); btnYes.Anchor = System.Windows.Forms.AnchorStyles.Top; btnYes.DialogResult = System.Windows.Forms.DialogResult.Yes; btnYes.Location = new System.Drawing.Point(-100, this.btnDetail.Top); btnYes.Name = "btnYes"; btnYes.Size = new System.Drawing.Size(80, 24); btnYes.Text = rm.GetString(YESBUTTON); btnNo.Anchor = System.Windows.Forms.AnchorStyles.Top; btnNo.DialogResult = System.Windows.Forms.DialogResult.No; btnNo.Location = new System.Drawing.Point(-100, this.btnDetail.Top); btnNo.Name = "btnNo"; btnNo.Size = new System.Drawing.Size(80, 24); btnNo.Text = rm.GetString(NOBUTTON); buttonList.Add(btnYes); this.Controls.Add(btnYes); buttonList.Add(btnNo); this.Controls.Add(btnNo); } //判断是否需要设置“取消(Cancel)”按钮 if((buttonStyle & SWDialogBox.MessageBoxButton.OKCancel) == SWDialogBox.MessageBoxButton.OKCancel || (buttonStyle & SWDialogBox.MessageBoxButton.RetryCancel) == SWDialogBox.MessageBoxButton.RetryCancel || (buttonStyle & SWDialogBox.MessageBoxButton.YesNoCancel) == SWDialogBox.MessageBoxButton.YesNoCancel) { System.Windows.Forms.Button btnCancel = new System.Windows.Forms.Button(); btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Top; btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; btnCancel.Location = new System.Drawing.Point(-100, this.btnDetail.Top); btnCancel.Name = "btnCancel"; btnCancel.Size = new System.Drawing.Size(80, 24); btnCancel.Text = rm.GetString(CANCELBUTTON); buttonList.Add(btnCancel); this.Controls.Add(btnCancel); } //设置“详细(Detail)”按钮的显示状态 btnDetail.Visible = (buttonStyle & SWDialogBox.MessageBoxButton.Detail) == SWDialogBox.MessageBoxButton.Detail; if((buttonStyle & SWDialogBox.MessageBoxButton.Detail) == SWDialogBox.MessageBoxButton.Detail) { btnDetail.Text = rm.GetString(DETAILBUTTON); buttonList.Add(btnDetail); } //设置消息框的默认按钮(第一个按钮) if(buttonList.Count >= 1) this.AcceptButton = (System.Windows.Forms.Button)buttonList[0]; //设置消息框的默认按钮(第二个按钮) if((buttonStyle & SWDialogBox.MessageBoxButton.DefaultButton2) == SWDialogBox.MessageBoxButton.DefaultButton2 && buttonList.Count >= 2) this.AcceptButton = (System.Windows.Forms.Button)buttonList[1]; //设置消息框的默认按钮(第三个按钮) if((buttonStyle & SWDialogBox.MessageBoxButton.DefaultButton3) == SWDialogBox.MessageBoxButton.DefaultButton3 && buttonList.Count >= 3) this.AcceptButton = (System.Windows.Forms.Button)buttonList[2]; //循环设置每个按钮的左边界位置 for(int i=0; i<buttonList.Count; i++) { //依次设置每个按钮的TabIndex属性 ((System.Windows.Forms.Button)buttonList[i]).TabIndex = 2 + i; if(i==0) ((System.Windows.Forms.Button)buttonList[i]).Left = (this.ClientSize.Width - (80 * buttonList.Count + 8 * (buttonList.Count - 1))) / 2; else ((System.Windows.Forms.Button)buttonList[i]).Left = ((System.Windows.Forms.Button)buttonList[i-1]).Left + ((System.Windows.Forms.Button)buttonList[i-1]).Width + 8; } }
/// <summary> /// 显示具有指定文本、标题、详细注视、按钮、图标和默认按钮的消息框。 /// </summary> /// <param name="message">消息框中显示的文本。</param> /// <param name="caption">消息框的标题栏中显示的文本。</param> /// <param name="description">消息框的详细注视栏中显示的文本。</param> /// <param name="button">SWDialogBox.MessageBoxButton 值之一,它指定在消息框中显示哪些按钮及默认按钮。</param> /// <param name="icon">SWDialogBox.MessageBoxIcon 值之一,它指定在消息框中显示哪个图标。</param> /// <returns>System.Window.Forms.DialogResult 值之一。</returns> public static System.Windows.Forms.DialogResult Show(String message, String caption, String description, SWDialogBox.MessageBoxButton button, SWDialogBox.MessageBoxIcon icon) { SWDialogBox.FMessageBox messageForm = null; try { messageForm = new SWDialogBox.FMessageBox(); messageForm.Icon = moIcon; return(messageForm.ShowDialog(message, caption, description, button, icon)); } catch { throw; } finally { if (messageForm != null) { messageForm.Dispose(); } } }