自定义消息对话框基于Panel

直接修改的基于Dialog的消息对话框

  1. #region 消息面板
  2.     public class MessagePanel : Panel
  3.     {
  4.         private Image btnImage = null;
  5.         private string _caption = "";
  6.         private string _text = "";
  7.         private Timer timer = null;
  8.         private const int speed = 5;
  9.         private const int buttonLeft = 30;
  10.         private const int buttonHeight = 18;
  11.         private const int buttonSp = 3;
  12.         private const int bottomSp = 8;
  13.         private int _height = -1;
  14.         public DialogResult DialogResult = DialogResult.None;
  15.         public MessagePanel(Control cparent)
  16.         {
  17.             this.Visible = false;
  18.             this.Width = cparent.Width;
  19.             this.Height = 100;
  20.             this.Top = cparent.Height;
  21.             this.Left = 0;
  22.             timer = new Timer();
  23.             timer.Interval = 20;
  24.             timer.Tick += new EventHandler(timer_Tick);
  25.             this.DialogResult = DialogResult.None;
  26.             cparent.Controls.Add(this);
  27.             //cparent.Controls.SetChildIndex(this, 1);
  28.             API.SetWindowPos(this.Handle, API.HWND_TOP, 0, 0, 0, 0, 3);
  29.         }
  30.         void timer_Tick(object sender, EventArgs e)
  31.         {
  32.             //移动窗口
  33.             Top -= speed;
  34.             if (Top <= this.Parent.Height - this.Height)
  35.             {
  36.                 timer.Enabled = false;
  37.                 Top = this.Parent.Height - this.Height;
  38.             }
  39.         }
  40.         protected override void OnPaint(PaintEventArgs e)
  41.         {
  42.             Rectangle rectCaption = new Rectangle(0,0,this.Width,18);
  43.             int iheight = rectCaption.Height;
  44.             Font font = new Font("Tahoma",9F,FontStyle.Regular);
  45.             Brush brush = new SolidBrush(Color.Black);
  46.             StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
  47.             format.Alignment = StringAlignment.Center;
  48.             format.LineAlignment = StringAlignment.Center;
  49.             if(_caption==null || _caption.Trim().Length == 0)
  50.             {   
  51.                 SizeF size= e.Graphics.MeasureString(_text,font);
  52.                 if(_text.IndexOf('/n') < 0 && size.Width < this.Width)
  53.                 {
  54.                     _caption = _text;
  55.                     _text = "";
  56.                 }
  57.                 else
  58.                     _caption = Language.S_MESSAGEBOX_TITLE;
  59.             }
  60.             e.Graphics.FillRectangle(new SolidBrush(Color.Blue), rectCaption);
  61.             e.Graphics.DrawString(_caption, font, new SolidBrush(Color.White), rectCaption, format);
  62.             if (_text != null && _text.Trim().Length > 0)
  63.             {
  64.                 iheight += 2;
  65.                 string[] strs = _text.Split('/n');
  66.                 foreach (string s in strs)
  67.                 {
  68.                     SizeF size = e.Graphics.MeasureString(s, font);
  69.                     Rectangle rectText = new Rectangle(5, iheight, this.Width - 10, (int)size.Height + 2);
  70.                     e.Graphics.DrawString(s, font, brush, rectText, format);
  71.                     iheight += rectText.Height;
  72.                 }
  73.             }
  74.             if(_height < 0)
  75.             {
  76.                 if (this.Controls.Count == 2)
  77.                 {
  78.                     _height = iheight + this.Controls[0].Height * 2 + bottomSp * 2 + buttonSp;
  79.                     this.Height = _height;
  80.                 }
  81.                 else if (this.Controls.Count == 1)
  82.                 {
  83.                     _height = iheight + this.Controls[0].Height + bottomSp * 2;
  84.                     this.Height = _height;
  85.                 }
  86.             }
  87.             base.OnPaint(e);
  88.         }
  89.         public DialogResult Show(string text)
  90.         {
  91.             this.Top = this.Parent.Height;
  92.             this.Left = 0;
  93.             //设置信息
  94.             this._text = text;
  95.             //加载按钮
  96.             Button2 btnOK = new Button2();
  97.             btnOK.Text = Language.S_OK;
  98.             btnOK.TextAlign = OpenNETCF.Drawing.ContentAlignment2.MiddleCenter;
  99.             btnOK.BackgroundImage = btnImage;
  100.             btnOK.Top = this.Height - buttonHeight - bottomSp;
  101.             btnOK.Left = buttonLeft;
  102.             btnOK.Width = this.Width - 2 * buttonLeft;
  103.             btnOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
  104.             btnOK.Click += new EventHandler(btnOK_Click);
  105.             this.Controls.Add(btnOK);
  106.             //启动时钟
  107.             timer.Enabled = true;
  108.             //显示窗口
  109.             return this.ShowDialog();
  110.         }
  111.         public DialogResult Show(string text, string caption)
  112.         {
  113.             _caption = caption;
  114.             return Show(text);
  115.         }
  116.         public DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxDefaultButton defaultButton)
  117.         {
  118.             this.Top = this.Parent.Height;
  119.             this.Left = 0;
  120.             //设置信息
  121.             this._text = text;
  122.             this._caption = caption;
  123.             //加载按钮
  124.             if (buttons == MessageBoxButtons.YesNo || buttons == MessageBoxButtons.OKCancel)
  125.             {
  126.                 Button2 btnOK = new Button2();
  127.                 btnOK.TextAlign = OpenNETCF.Drawing.ContentAlignment2.MiddleCenter;
  128.                 btnOK.BackgroundImage = btnImage;
  129.                 btnOK.Top = this.Height - buttonHeight * 2 - bottomSp - buttonSp;
  130.                 btnOK.Left = buttonLeft;
  131.                 btnOK.Width = this.Width - 2 * buttonLeft;
  132.                 btnOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
  133.                 Button2 btnCancel = new Button2();
  134.                 btnCancel.TextAlign = OpenNETCF.Drawing.ContentAlignment2.MiddleCenter;
  135.                 btnCancel.BackgroundImage = btnImage;
  136.                 btnCancel.Top = this.Height - buttonHeight - bottomSp;
  137.                 btnCancel.Left = buttonLeft;
  138.                 btnCancel.Width = this.Width - 2 * buttonLeft;
  139.                 btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
  140.                 if (buttons == MessageBoxButtons.OKCancel)
  141.                 {
  142.                     btnOK.Text = Language.S_OK;
  143.                     btnOK.Click += new EventHandler(btnOK_Click);
  144.                     btnCancel.Text = Language.S_CANCEL;
  145.                     btnCancel.Click +=new EventHandler(btnCancel_Click);
  146.                 }
  147.                 else
  148.                 {
  149.                     btnOK.Text = Language.S_YES;
  150.                     btnOK.Click += new EventHandler(btnYes_Click);
  151.                     btnCancel.Text = Language.S_NO;
  152.                     btnCancel.Click += new EventHandler(btnNo_Click);
  153.                 }
  154.                 this.Controls.Add(btnOK);
  155.                 this.Controls.Add(btnCancel);
  156.                 if (defaultButton == MessageBoxDefaultButton.Button2)
  157.                     btnCancel.Focus();
  158.                 else
  159.                     btnOK.Focus();
  160.             }
  161.             else
  162.             {
  163.                 Button2 btnOK = new Button2();
  164.                 btnOK.Text = Language.S_OK;
  165.                 btnOK.TextAlign = OpenNETCF.Drawing.ContentAlignment2.MiddleCenter;
  166.                 btnOK.BackgroundImage = btnImage;
  167.                 btnOK.Top = this.Height - buttonHeight - bottomSp;
  168.                 btnOK.Left = buttonLeft;
  169.                 btnOK.Width = this.Width - 2 * buttonLeft;
  170.                 btnOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
  171.                 btnOK.Click += new EventHandler(btnOK_Click);
  172.                 this.Controls.Add(btnOK);
  173.             }
  174.             //启动时钟
  175.             timer.Enabled = true;
  176.             //显示窗口
  177.             return this.ShowDialog();
  178.         }
  179.         private DialogResult ShowDialog()
  180.         {
  181.             this.Visible = true;
  182.             do
  183.             {
  184.                 Application.DoEvents();
  185.             } while (this.DialogResult == DialogResult.None);
  186.             return this.DialogResult;
  187.         }
  188.         void btnOK_Click(object sender, EventArgs e)
  189.         {
  190.             this.DialogResult = DialogResult.OK;
  191.             this.Parent.Controls.Remove(this);
  192.         }
  193.         void btnCancel_Click(object sender, EventArgs e)
  194.         {
  195.             this.DialogResult = DialogResult.Cancel;
  196.             this.Parent.Controls.Remove(this);
  197.         }
  198.         void btnYes_Click(object sender, EventArgs e)
  199.         {
  200.             this.DialogResult = DialogResult.Yes;
  201.             this.Parent.Controls.Remove(this);
  202.         }
  203.         void btnNo_Click(object sender, EventArgs e)
  204.         {
  205.             this.DialogResult = DialogResult.No;
  206.             this.Parent.Controls.Remove(this);
  207.         }
  208.     }
  209.     #endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值