直接修改的基于Dialog的消息对话框
- #region 消息面板
- public class MessagePanel : Panel
- {
- 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 DialogResult DialogResult = DialogResult.None;
- public MessagePanel(Control cparent)
- {
- this.Visible = false;
- this.Width = cparent.Width;
- this.Height = 100;
- this.Top = cparent.Height;
- this.Left = 0;
- timer = new Timer();
- timer.Interval = 20;
- timer.Tick += new EventHandler(timer_Tick);
- this.DialogResult = DialogResult.None;
- cparent.Controls.Add(this);
- //cparent.Controls.SetChildIndex(this, 1);
- API.SetWindowPos(this.Handle, API.HWND_TOP, 0, 0, 0, 0, 3);
- }
- void timer_Tick(object sender, EventArgs e)
- {
- //移动窗口
- Top -= speed;
- if (Top <= this.Parent.Height - this.Height)
- {
- timer.Enabled = false;
- Top = this.Parent.Height - this.Height;
- }
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- Rectangle rectCaption = new Rectangle(0,0,this.Width,18);
- int iheight = rectCaption.Height;
- Font font = new Font("Tahoma",9F,FontStyle.Regular);
- 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 = this.Parent.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;
- //显示窗口
- return this.ShowDialog();
- }
- 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 = this.Parent.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;
- //显示窗口
- return this.ShowDialog();
- }
- private DialogResult ShowDialog()
- {
- this.Visible = true;
- do
- {
- Application.DoEvents();
- } while (this.DialogResult == DialogResult.None);
- return this.DialogResult;
- }
- void btnOK_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.OK;
- this.Parent.Controls.Remove(this);
- }
- void btnCancel_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.Cancel;
- this.Parent.Controls.Remove(this);
- }
- void btnYes_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.Yes;
- this.Parent.Controls.Remove(this);
- }
- void btnNo_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.No;
- this.Parent.Controls.Remove(this);
- }
- }
- #endregion