自己动手用c#写控件(下)

因为我们这个控件是实现背景渐变及文字填充,所以override Paint事件以完成自画。

为了完成override,现在以下的准备工作(写几个在Paint事件用的着的事件).

//使用角度的方法渐近重画Button

          private void DrawButtonWithAngle(Graphics dbg)

         {

              LinearGradientBrush brush=new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height),froColor,backColor,angle);

              dbg.FillRectangle(brush,0,0,this.Width,this.Height);

              brush.Dispose();

         }

         ////使用模式的方法渐近重画Button

          private void DrawButtonWithMode(Graphics dbg,LinearGradientMode Mode)

         {

              LinearGradientBrush brush=new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height),froColor,backColor,Mode);

              dbg.FillRectangle(brush,0,0,this.Width,this.Height);

              brush.Dispose();

         }

         //重画Button的文本(Text),不使用图案填充

          private void DrawButtonText(Graphics dbg)

         {

              StringFormat format=new StringFormat();

              format.LineAlignment=StringAlignment.Center;

              format.Alignment=StringAlignment.Center;

              dbg.DrawString(this.Text,this.Font,new SolidBrush(this.ForeColor),new Rectangle(0,0,this.Width,this.Height),format);

         }

          //override DrawButtonText函数,使之可以用图案填充文本

          private  void DrawButtonText(Graphics dbg, HatchStyle hs)

         {

              StringFormat format=new StringFormat();

              format.LineAlignment=StringAlignment.Center;

              format.Alignment=StringAlignment.Center;

              dbg.DrawString(this.Text,this.Font,new HatchBrush(hs,this.ForeColor,Color.Aquamarine),new Rectangle(0,0,this.Width,this.Height),format);

     }

好了,现在开始重写Paint事件了.

protected override void OnPaint(PaintEventArgs pe)

         {

             

              Graphics g=pe.Graphics;

              base.OnPaint(pe); //调用父控件的方法

              if(isUseFloat==true) //假如使用角度控制渐变的角度

                   DrawButtonWithAngle(g);

              if(isUseFloat==false)

                   DrawButtonWithMode(g,mode);

              if(isUseStyle==true)//假如使用图案填充文字

             DrawButtonText(g,hatchStyle);

              else

                   DrawButtonText(g);

     }

好了,现在大功告成了,进行保存,生成。

创建测试项目

1.        文件菜单上,指向添加项目,然后单击新建项目以打开添加新项目对话框。

2.        选择“Visual C# 项目节点,然后单击“Windows 应用程序

3.        名称框中键入 Test

4.        在解决方案资源管理器中,右击测试项目的引用节点,然后从快捷菜单中选择添加引用以显示添加引用对话框。

5.        单击标记为项目的选项卡。

6.        双击 LinearGradientButtonLib 项目,并注意该项目此时出现在选定的组件窗格中。

添加引用后,应将新控件添加到工具箱。如果您的控件已经出现在工具箱中,则应该跳过下一节。

将控件添加到工具箱

1.        右击工具箱,然后从快捷菜单中选择自定义工具箱

“自定义工具箱”对话框打开。

2.        选择“.NET 框架组件选项卡并单击浏览。浏览到 LinearGradientButtonLib/bin/debug 文件夹并选择 LinearGradientButtonLib.dll

LinearGradientButton 出现在“自定义工具箱”对话框的组件列表中。

3.        自定义工具箱对话框中,单击 LinearGradientButton 旁的框并关闭窗口。

LinearGradientButton 被添加到选定的工具箱的选项卡上。

将控件添加到窗体

1.        在解决方案资源管理器中,右击“Form1.cs”,然后从快捷菜单中选择视图设计器

2.        在工具箱中,向下滚动直到到达标记为 LinearGradientButton 的图标。双击该图标。

窗体上显示一个LinearGradientButton”。

3.        右击LinearGradientButton并从快捷菜单中选择属性

4.        属性窗口中检查该控件的属性。注意,它们与标准按钮公开的属性相同,不同的是多了我们自己加入的一些属性

5.        设定本控件的前景色及背景色,然后可以选择是否填充文字,是使用角度还是使用系统设定值进行渐变角度的变化。

6.        调试菜单中选择启动 出现 Form1

谁如果需要源码的话,请给我发信.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值