今天看了《自己动手用c#写控件》一文,写出了自己的源代码,但仍有属性不能立即问题。

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Windows.Forms;

namespace LinearGradientButtonLib
{
 /// <summary>
 /// UserControl1 的摘要说明。
 /// </summary>
 public class LinearGradientButton :System.Windows.Forms.Button
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  private Color frontColor;//按钮渐变的前景色
  private Color backColor;//按钮渐变的背景色
  private bool isUseAngle;//是否使用角度
  private float angle;//设置角度
  private LinearGradientMode mode;//设置线性渐变的方向
  private StringAlignment textLineAlignment;//设置按钮文字的水平对齐方式
  private StringAlignment textVertAlignment;//设置按钮文字的垂直对齐方式
  private HatchStyle hatchStyle;//设置文本的填充图案
  private bool isUseHatchStyle;//设置是否用图案填充

  public LinearGradientButton()
  {
   // 该调用是 Windows.Forms 窗体设计器所必需的。
   InitializeComponent();
   this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |
    ControlStyles.DoubleBuffer, true);
   // TODO: 在 InitComponent 调用后添加任何初始化

  }

  [Description("设置按钮渐变的前景色"),
  Category("Appearance"),
  System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
  public Color FrontColor
  {
   get
   {
    if(frontColor == Color.Empty)
    {
     frontColor = Color.Black;
    }
    return frontColor;
   }
   set
   {
    frontColor = value;
   }
  }

  [Description("设置按钮渐变的背景色"),
  Category("Appearance"),
  System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
  public Color BackgroundColor
  {
   get
   {
    if(backColor == Color.Empty)
    {
     backColor = Color.White;
    }
    return backColor;
   }
   set
   {
    backColor = value;
   }
  }

  [DefaultValue(false),Description("是否工人设定渐变角度")]
  public bool UseAngle
  {
   get
   {
    return isUseAngle;
   }
   set
   {
    isUseAngle = value;
   }
  }
  
  [Description("设定渐变角度"),Category("Appearance"),
  System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
  public float Angle
  {
   get
   {
    /*
    while(angle<0)
    {
     angle += 360;
    }
    while(angle>360)
    {
     angle -= 360;
    }
    */
    return angle;
   }
   set
   {
    angle = value;
   }
  }
  /*
    [DefaultValue(StringAlignment.Center),Description("设置文本的水平对齐方式"),Category("Appearance")]
    public StringAlignment TextLineAlignment
    {
     get
     {
      return textLineAlignment;
     }
     set
     {
      value = textLineAlignment;
     }
    }

    [DefaultValue(StringAlignment.Center),Description("设置文本的垂直对齐方式"),Category("Appearance")]
    public StringAlignment TextVertAlignment
    {
     get
     {
      return textVertAlignment;
     }
     set
     {
      value = textVertAlignment;
     }
    }
  */
  [DefaultValue(false),Description("是否使用图案填充文本"),
  System.ComponentModel.RefreshProperties(RefreshProperties.All)]
  public bool UseHatchStyle
  {
   get
   {
    return isUseHatchStyle;
   }
   set
   {
    isUseHatchStyle = value;
   }
  }

  [DefaultValue(0),Description("当UseAngle=false时,设置渐变方向"),Category("Appearance"),
  System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
  public LinearGradientMode Mode
  {
   get
   {
    return mode;
   }
   set
   {
    mode = value;
   }
  }

  [DefaultValue(1),Description("设置文本要填充的图案"),
  Category("Appearance"),
  System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
  public HatchStyle FillStyle
  {
   get
   {
    return hatchStyle;
   }
   set
   {
    hatchStyle = value;
   }
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if( components != null )
     components.Dispose();
   }
   base.Dispose( disposing );
  }

  #region 组件设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   components = new System.ComponentModel.Container();
  }
  #endregion

  //使用角度重画按钮
  private void DrawButtonWithAngle(Graphics g)
  {
   LinearGradientBrush lgb
    = new LinearGradientBrush(
    new Rectangle(0,0,this.Width,this.Height),
    frontColor,backColor,angle
    );
   g.FillRectangle(lgb,0,0,this.Width,this.Height);
   lgb.Dispose();
   
  }

  //使用渐变模式填充按钮
  private void DrawButtonWithMode(Graphics g,LinearGradientMode mode)
  {
   LinearGradientBrush lgb
    =new LinearGradientBrush(
    new Rectangle(0,0,this.Width,this.Height),
    frontColor,backColor,mode);
   g.FillRectangle(lgb,0,0,this.Width,this.Height);
   lgb.Dispose();
   
  }

  //不用图案画文字
  private void DrawButtonText(Graphics g)
  {
   StringFormat sf=new StringFormat();
   //sf.LineAlignment = textLineAlignment;
   //sf.Alignment= textVertAlignment;
   sf.LineAlignment = StringAlignment.Center;
   sf.Alignment= StringAlignment.Center;
   g.DrawString(this.Text,this.Font,
    new SolidBrush(this.ForeColor),
    new Rectangle(0,0,this.Width,this.Height),
    sf);
   
  }

  //使用图案绘制文字
  private void DrawButtonText(Graphics g,HatchStyle hs)
  {
   StringFormat sf=new StringFormat();
   //sf.LineAlignment = textLineAlignment;
   //sf.Alignment= textVertAlignment;
   sf.LineAlignment = StringAlignment.Center;
   sf.Alignment= StringAlignment.Center;
   g.DrawString(this.Text,this.Font,
    new HatchBrush(hs,this.ForeColor,Color.Aquamarine),
    new Rectangle(0,0,this.Width,this.Height),
    sf);
   
  }

  //重写OnPaint事件
  protected override void OnPaint(PaintEventArgs pea)
  {
   Graphics g = pea.Graphics;
   base.OnPaint(pea);
   if(UseAngle)
    DrawButtonWithAngle(g);
   else
    DrawButtonWithMode(g,Mode);
   if(UseHatchStyle)
    DrawButtonText(g,FillStyle);
   else
    DrawButtonText(g);
  }
 }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值