您当前的位置:首页 > 计算机论文>软件开发论文

基于c#的自制控件:Button

2015-08-21 08:51 来源:学术参考网 作者:未知

摘 要:

关键词:c#;button;usercontrol
一、概述:
  有时Visual Studio提供的控件不能满足用户的需求,此时我们可以去购买第三方控件,但是一些简单的控件我们完全可以自己制作出来,接下来,我们将自己动手制作一个圆形的button控件,它实现了button控件所需要具有的几乎全部特性。Control是定义控件的基类,控件是带有可视化表示形式的组件。所以我们让RoundButton继承自Control,且实现IButtonControl接口,IButtonControl接口将允许控件在窗体上的作用就像按钮一样。类关系图如图1-1所示。



 
          图1-1                  图1-2  图1-3            图1-4
                                                   
代码实现:
1. 字段的设置
  public class RoundButton : Control, IButtonControl
    {
          private ButtonState myButtonState;
          private DialogResult myDialogResult;
          private bool IsDefault { get; set; }
      //...other method
  }
2. Button外观的的绘制
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            PaintStandart(e.Graphics);
        }
        private void PaintStandart(Graphics g)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;//指定消除锯齿的呈现。
            Rectangle rect = new Rectangle(2, 2, this.Width - 4, this.Height - 4);
            if (this.Focused)
            {
                rect.Inflate(-2, -2);
    DrawFocus(g);
            }
            PaintButtonBackGround();
            Draw3DBorder(rect, g);
            DrawText(rect, g);
        }
        private void PaintButtonBackGround()
        {
            GraphicsPath myPath = new GraphicsPath();
            myPath.AddEllipse(0, 0, this.Width, this.Height);
            Region myRegion = new Region(myPath);
            this.Region = myRegion;
        }
        private void Draw3DBorderPushed(Rectangle rect, Graphics g)
        {
            using (Pen myPen = new Pen(SystemColors.ControlDarkDark, 3))
            {
                g.DrawEllipse(myPen, rect);
            }
        }
        private void Draw3DBorder(Rectangle rect, Graphics g)
        {
            if (myButtonState == ButtonState.Pushed)
                Draw3DBorderPushed(rect, g);
            else
                Draw3DBorderNormal(rect, g);
        }
        private void Draw3DBorderNormal(Rectangle rect, Graphics g)
        {
            LinearGradientBrush myBrush =
                new LinearGradientBrush(rect, Color.White, Color.Black,
                    LinearGradientMode.ForwardDiagonal);
            using (Pen myPen = new Pen(myBrush, 3))
            {
                g.DrawEllipse(myPen, rect);
            }
        }
        private void DrawFocus(Graphics g)
        {
            Rectangle myRect =
                new Rectangle(1, 1, this.Width - 2, this.Height - 2);
            using (Pen myPen = new Pen(Color.Black))
            {
                g.DrawEllipse(myPen, myRect);
            }
        }
        private void DrawText(Rectangle rect, Graphics g)
        {
            StringFormat myFormat = new StringFormat();
            myFormat.Alignment = StringAlignment.Center;
            myFormat.LineAlignment = StringAlignment.Center;
            if (this.Enabled)
            {
                rect.Offset(1, 1);
                g.DrawString(this.Text, this.Font,
                    new SolidBrush(this.ForeColor), rect, myFormat);
            }
            else
            {
                rect.Offset(1, 1);
                g.DrawString(this.Text, this.Font,
                    new SolidBrush(Color.White), rect, myFormat);
                rect.Offset(-1, -1);
                g.DrawString(this.Text, this.Font,
                    new SolidBrush(SystemColors.GrayText), rect, myFormat);
            }
        }
3. IButtonControl接口的实现
        [Category(""Behavior""), DefaultValue(DialogResult.None)]
        public virtual DialogResult DialogResult
        {
            get{ return myDialogResult;}
            set{myDialogResult = value;}
        }
        public void NotifyDefault(bool value)
        {
            if (this.IsDefault != value)
                this.IsDefault = value;
            if (this.IsDefault == true) this.Focus();
  else
  this.OnLostFocus(EventArgs.Empty);     
  }
        public void PerformClick()
        {
            if (this.CanSelect)
                this.OnClick(EventArgs.Empty);
        }
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            ((Form)this.TopLevelControl).DialogResult = myDialogResult;
        }
二、总结:
  本文通过一个简单的例子起到抛砖引玉的作用,依照这些步骤,大家完全可以制作出属于自己的更有个性的button。用户界面由各种控件组成,应用自制的控件将会使用户界面更为丰富,使他人眼前一亮。


相关文章
学术参考网 · 手机版
https://m.lw881.com/
首页