c#下重写BUTTON控件

原创 2005年04月29日 15:57:00
XPButton.cs代码

using System;

using System.Windows;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows .Forms ;

 

namespace XPCtrl

{

     /// <summary>

     /// XPButton 的摘要说明。

     /// </summary>

     public class XPButton:System.Windows.Forms.Button

     {

         private bool mouseover=false;

         public XPButton()

         {

              //

              // TODO: 在此处添加构造函数逻辑

              //

                   this.Cursor = System.Windows.Forms.Cursors.Hand;

         }

         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)

         {

         //   base.OnPaint (e);

         //   System.Drawing.Graphics pp=this.CreateGraphics();

//            e.Graphics.Clear(Color.Transparent);

//            e.Graphics.DrawEllipse(new System.Drawing.Pen(System.Drawing.Color.WhiteSmoke,5),0,0,this.Width,this.Height);

//            System.Drawing.SolidBrush dd=new SolidBrush(System.Drawing.Color.WhiteSmoke);

//            e.Graphics.FillEllipse(dd,0,0,this.Width,this.Height);

             

              // (this.BackColor.ToString ())

              Color c5 = Color.FromArgb

                   (255,255,255);

              Color c2 = Color.FromArgb

                   (192,192,192);

              if(mouseover)

              {

                   c5=Color.FromArgb(245,245,245);

                   //c2=Color.FromArgb(192,192,192);

                   c2=Color.FromArgb(180,175,190);

              }

              Brush b = new System.Drawing.Drawing2D.LinearGradientBrush

                   (ClientRectangle, c5, c2, LinearGradientMode.Vertical);

              //System.Drawing.Region=new Region(

              int offsetwidth=this.Width/50;

              Point[] points=new Point[8];             

              points[0].X=offsetwidth;

              points[0].Y=0;

 

              points[1].X=this.Width-offsetwidth;

              points[1].Y=0;

 

              points[2].X=this.Width;

              points[2].Y=offsetwidth;

 

              points[3].X=this.Width;

              points[3].Y=this.Height-offsetwidth;

 

              points[4].X=this.Width-offsetwidth;

              points[4].Y=this.Height;

 

              points[5].X=offsetwidth;

              points[5].Y=this.Height;

 

              points[6].X=0;

              points[6].Y=this.Height-offsetwidth;

 

              points[7].X=0;

              points[7].Y=offsetwidth;

//            e.Graphics.FillRectangle (b, ClientRectangle);

              e.Graphics.FillPolygon(b,points,FillMode.Winding);

              if(this.Focused)

              {

                   int offsetwidth1=(this.Width-5)/50+2;

                   Point[] points1=new Point[8];

                   points1[0].X=offsetwidth1;

                   points1[0].Y=2;

 

                   points1[1].X=this.Width-offsetwidth1;

                   points1[1].Y=2;

 

                   points1[2].X=this.Width-1;

                   points1[2].Y=offsetwidth1;

 

                   points1[3].X=this.Width-1;

                   points1[3].Y=this.Height-offsetwidth1;

 

                   points1[4].X=this.Width-offsetwidth1;

                   points1[4].Y=this.Height-1;

 

                   points1[5].X=1;

                   points1[5].Y=this.Height-1;

 

                   points1[6].X=2;

                   points1[6].Y=this.Height-offsetwidth1;

 

                   points1[7].X=2;

                   points1[7].Y=offsetwidth1;

                   //   e.Graphics.DrawPolygon(new Pen(Color.Yellow,2),points1);

                   Pen p=new Pen(Color.Orange,2);

                   Pen p1=new Pen(Color.Wheat,2);

                   //p.DashStyle=DashStyle.DashDot;

                   e.Graphics.DrawLine(p1,points1[0],points1[1]);

 

                   e.Graphics.DrawLine(p,points1[1],points1[2]);

                   e.Graphics.DrawLine(p,points1[2],points1[3]);

                   e.Graphics.DrawLine(p,points1[3],points1[4]);

                   e.Graphics.DrawLine(p,points1[4],points1[5]);

                   e.Graphics.DrawLine(p,points1[5],points1[6]);

                   e.Graphics.DrawLine(p1,points1[6],points1[7]);

                   e.Graphics.DrawLine(p1,points1[7],points1[0]);              

              }            

              e.Graphics.DrawPolygon(new Pen(Color.DarkBlue,2),points);

//            e.Graphics.DrawLine(new Pen(Color.DarkBlue,2),new Point(0,0),new Point(this.Width,0));           

//            e.Graphics.DrawLine(new Pen(Color.DarkBlue,2),new Point(0,0),new Point(0,this.Height));

//            e.Graphics.DrawLine(new Pen(Color.DarkBlue,2),new Point(this.Width,this.Height),new Point(this.Width,0));

//            e.Graphics.DrawLine(new Pen(Color.DarkBlue,2),new Point(this.Width,this.Height),new Point(0,this.Height));

              StringFormat drawFormat = new StringFormat();

              drawFormat.FormatFlags = StringFormatFlags.DisplayFormatControl;

              drawFormat.LineAlignment=StringAlignment.Center;

              drawFormat.Alignment=System.Drawing.StringAlignment.Center; 

             

              e.Graphics.DrawString(this.Text,this.Font,new LinearGradientBrush(this.ClientRectangle,Color.Black,Color.Black,LinearGradientMode.Vertical),this.ClientRectangle,drawFormat);          

              b.Dispose();

 

 

         }

         protected override void OnLeave(EventArgs e)

         {

              base.OnLeave (e);

         }

 

//       protected override void OnMouseHover(EventArgs e)

//       {

//

//            mouseover=true;

//            this.Invalidate(false);

//            base.OnMouseHover (e);

//       }

         protected override void OnMouseEnter(EventArgs e)

         {

              mouseover=true;

              this.Invalidate(false);

              base.OnMouseEnter (e);

         }

 

         protected override void OnNotifyMessage(System.Windows.Forms.Message m)

         {            

              base.OnNotifyMessage (m);

         }

 

         protected override void OnMouseLeave(EventArgs e)

         {

              mouseover=false;  

              this.Invalidate(false);

              base.OnMouseLeave (e);

         }

         private void DrawButton( System.Drawing.Graphics g)

         {

        

         }

         protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)

         {

             

//            Color c5 = Color.FromArgb

//                 (255,255,255);

//            Color c2 = Color.FromArgb

//                 (192,192,192);

//            if(mouseover)

//            {

//                 c5=Color.FromArgb(245,245,245);

//                 //c2=Color.FromArgb(192,192,192);

//                 c2=Color.FromArgb(180,175,190);

//            }

//            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush

//                 (ClientRectangle, c5, c2, LinearGradientMode.Vertical);

 

              //pevent.Graphics .DrawRectangle(new Pen(Color.Transparent,2),this.ClientRectangle);

              pevent.Graphics .Clear(Color.Wheat );

                            //base.OnPaintBackground (pevent);

 

         }

 

     }

}

 

在Form1.cs中调用 

我的方法是先拖一个windows的Button过来,再改两个地方

一个是变量定义:

private XPCtrl.XPButton button1;

另一个是Windows 窗体设计器生成的代码中:

this.button1 = new XPCtrl.XPButton();



c#下重写BUTTON控件

XPButton.cs代码using System;using System.Windows;using System.Drawing;using System.Drawing.Drawing2D;u...
  • semisweet
  • semisweet
  • 2004年11月18日 11:47
  • 1962

C#开发学习笔记:编写自定义Button控件的自定义事件

编写自定义Button控件的自定义事件
  • JustWantToFly
  • JustWantToFly
  • 2016年07月19日 18:52
  • 3402

C#中Button的DoubleClick等的实现(不用重写类)

在C#中,有些情况下客户会有比较奇怪的要求,比如说:button类的DoubleClick事件触发。 为什么我会说它奇怪呢?因为button类虽然有doubleclick事件,但是在design的时...
  • zuoshengting
  • zuoshengting
  • 2017年10月12日 19:04
  • 221

c# 如何重写控件的OnPaint事件

使用OnPaint事件可以随时绘制图形 调用窗体的OnPaint事件protected override void OnPaint(PaintEventArgs e){    base.OnPaint...
  • daming_ocean
  • daming_ocean
  • 2008年11月26日 11:46
  • 9719

重写button样式

(1)在App.xaml文件里自定义一个按钮样式 ,"MyWpfButton": Application x:Class="WPFCustomerStyleStudy.App" ...
  • a376143220
  • a376143220
  • 2017年05月26日 09:03
  • 235

MFC 重写Button类

.h class CZPButton : public CButton{public: CZPButton(); virtual ~CZPButton();protected: CBitmap m_b...
  • zhengkangchen
  • zhengkangchen
  • 2009年02月25日 14:39
  • 5004

Winform自定义控件—Button(上)

在开发中用的最多的相信就是Button控件,但是Button本身是在是太丑陋了,自己还背景图还会产生"黑线",为了使用方便,并且美观,我们采用迂回的方式来实现Button的效果。 在这里使用UserC...
  • wangjihuanghun
  • wangjihuanghun
  • 2016年11月11日 18:20
  • 3117

C#控件的事件重写的理解与自我观点

由于我是从VFP开发转往C#开发的,所以常常想在C#理去实现VFP中的一些写法,例如这次.. 在VFP中,我们的为了适应开发的多变性,所以我们开发了很多继承于VFP控件的类控件,把很多常用的处理代码,...
  • turejackon
  • turejackon
  • 2009年10月21日 12:12
  • 4123

winform自定义控件之ComboBox简单重写

由于项目需要,现有的ComboBox控件满足不了需求,需要重写做一些小小的改变。要求ComboBox每一项前增加图片显示,使边框颜色修改,及禁用鼠标滚轮修改当前选项。 定义ComboBox选择项类us...
  • Neil198
  • Neil198
  • 2010年04月14日 18:46
  • 4759

重绘Button控件

分享到 一键分享QQ空间新浪微博百度云收藏人人网腾讯微博百度相册开心网腾讯朋友百度贴吧豆瓣网搜狐微博百度新首页QQ好友和讯微博更多... 百度分享 ...
  • u012792791
  • u012792791
  • 2015年08月02日 00:29
  • 592
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:c#下重写BUTTON控件
举报原因:
原因补充:

(最多只允许输入30个字)