#region 消息对话框
public class MessageBoxEx : ExtForm
{
public static Form _ownner;
private bool bOwnnerTopMost = false;
private Image btnImage = null;
private string _caption = "";
private string _text = "";
private Timer timer = null;
private const int speed = 5;
private const int buttonLeft = 30;
private const int buttonHeight = 18;
private const int buttonSp = 3;
private const int bottomSp = 8;
private int _height = -1;
public MessageBoxEx()
{
if (_ownner != null)
{
this.Owner = _ownner;
bOwnnerTopMost = _ownner.TopMost;
_ownner.TopMost = true;
}
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.Visible = false;
this.Width = Screen.PrimaryScreen.Bounds.Width;
this.Height = 100;
this.Top = 0;
timer = new Timer();
timer.Interval = 20;
timer.Tick += new EventHandler(timer_Tick);
this.DialogResult = DialogResult.Cancel;
}
void timer_Tick(object sender, EventArgs e)
{
//移动窗口
Top -= speed;
if (Top <= Screen.PrimaryScreen.Bounds.Height - this.Height)
{
timer.Enabled = false;
Top = Screen.PrimaryScreen.Bounds.Height - this.Height;
}
}
internal override void ChangeOrientation()
{
this.Width = Screen.PrimaryScreen.Bounds.Width;
_height = -1;
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rectCaption = new Rectangle(0,0,this.Width,18);
int iheight = rectCaption.Height;
Font font = this.Font;
Brush brush = new SolidBrush(Color.Black);
StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
if(_caption==null || _caption.Trim().Length == 0)
{
SizeF size= e.Graphics.MeasureString(_text,font);
if(_text.IndexOf('/n') < 0 && size.Width < this.Width)
{
_caption = _text;
_text = "";
}
else
_caption = Language.S_MESSAGEBOX_TITLE;
}
e.Graphics.FillRectangle(new SolidBrush(Color.Blue), rectCaption);
e.Graphics.DrawString(_caption, font, new SolidBrush(Color.White), rectCaption, format);
if (_text != null && _text.Trim().Length > 0)
{
iheight += 2;
string[] strs = _text.Split('/n');
foreach (string s in strs)
{
SizeF size = e.Graphics.MeasureString(s, font);
Rectangle rectText = new Rectangle(5, iheight, this.Width - 10, (int)size.Height + 2);
e.Graphics.DrawString(s, font, brush, rectText, format);
iheight += rectText.Height;
}
}
if(_height < 0)
{
if (this.Controls.Count == 2)
{
_height = iheight + this.Controls[0].Height * 2 + bottomSp * 2 + buttonSp;
this.Height = _height;
}
else if (this.Controls.Count == 1)
{
_height = iheight + this.Controls[0].Height + bottomSp * 2;
this.Height = _height;
}
}
base.OnPaint(e);
}
public DialogResult Show(string text)
{
this.Top = Screen.PrimaryScreen.Bounds.Height;
this.Left = 0;
//设置信息
this._text = text;
//加载按钮
Button2 btnOK = new Button2();
btnOK.Text = Language.S_OK;
btnOK.TextAlign = OpenNETCF.Drawing.ContentAlignment2.MiddleCenter;
btnOK.BackgroundImage = btnImage;
btnOK.Top = this.Height - buttonHeight - bottomSp;
btnOK.Left = buttonLeft;
btnOK.Width = this.Width - 2 * buttonLeft;
btnOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
btnOK.Click += new EventHandler(btnOK_Click);
this.Controls.Add(btnOK);
//启动时钟
timer.Enabled = true;
//显示窗口
try
{
return this.ShowDialog();
}
finally
{
if (_ownner != null)
_ownner.TopMost = bOwnnerTopMost;
}
}
public DialogResult Show(string text, string caption)
{
_caption = caption;
return Show(text);
}
public DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxDefaultButton defaultButton)
{
this.Top = Screen.PrimaryScreen.Bounds.Height;
this.Left = 0;
//设置信息
this._text = text;
this._caption = caption;
//加载按钮
if (buttons == MessageBoxButtons.YesNo || buttons == MessageBoxButtons.OKCancel)
{
Button2 btnOK = new Button2();
btnOK.TextAlign = OpenNETCF.Drawing.ContentAlignment2.MiddleCenter;
btnOK.BackgroundImage = btnImage;
btnOK.Top = this.Height - buttonHeight * 2 - bottomSp - buttonSp;
btnOK.Left = buttonLeft;
btnOK.Width = this.Width - 2 * buttonLeft;
btnOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
Button2 btnCancel = new Button2();
btnCancel.TextAlign = OpenNETCF.Drawing.ContentAlignment2.MiddleCenter;
btnCancel.BackgroundImage = btnImage;
btnCancel.Top = this.Height - buttonHeight - bottomSp;
btnCancel.Left = buttonLeft;
btnCancel.Width = this.Width - 2 * buttonLeft;
btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
if (buttons == MessageBoxButtons.OKCancel)
{
btnOK.Text = Language.S_OK;
btnOK.Click += new EventHandler(btnOK_Click);
btnCancel.Text = Language.S_CANCEL;
btnCancel.Click +=new EventHandler(btnCancel_Click);
}
else
{
btnOK.Text = Language.S_YES;
btnOK.Click += new EventHandler(btnYes_Click);
btnCancel.Text = Language.S_NO;
btnCancel.Click += new EventHandler(btnNo_Click);
}
this.Controls.Add(btnOK);
this.Controls.Add(btnCancel);
if (defaultButton == MessageBoxDefaultButton.Button2)
btnCancel.Focus();
else
btnOK.Focus();
}
else
{
Button2 btnOK = new Button2();
btnOK.Text = Language.S_OK;
btnOK.TextAlign = OpenNETCF.Drawing.ContentAlignment2.MiddleCenter;
btnOK.BackgroundImage = btnImage;
btnOK.Top = this.Height - buttonHeight - bottomSp;
btnOK.Left = buttonLeft;
btnOK.Width = this.Width - 2 * buttonLeft;
btnOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
btnOK.Click += new EventHandler(btnOK_Click);
this.Controls.Add(btnOK);
}
//启动时钟
timer.Enabled = true;
//显示窗口
try
{
return this.ShowDialog();
}
finally
{
if (_ownner != null)
_ownner.TopMost = bOwnnerTopMost;
}
}
void btnOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
void btnYes_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Yes;
this.Close();
}
void btnNo_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.No;
this.Close();
}
}
#endregion