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# WinForm中添加用户控件

有的时候默认提供的工具
  • haelang
  • haelang
  • 2014年11月01日 15:49
  • 3371

win7工具箱(超实用工具)

绿色软件默认安装位置C:\Program Files(安装位置可选)只创建开始所有程序快捷方式,如要卸载直接删除文件及快捷方式即可 下载地址: 下载地址1:http://pan.baidu.com/...

c#下重写BUTTON控件

  • 2008年01月26日 16:33
  • 49KB
  • 下载

C#2005自定义button控件DLL

  • 2011年11月03日 14:07
  • 13KB
  • 下载

c# 使用Button和Panel 设计伸缩面板控件

说明:  此文使用的是Button 和Panel设计的伸缩面板 Button: 表示伸缩栏,可用别的控件表示,这里使用用于触发点击事件 Panel:  表示伸缩栏的容器,只要是容器控件就行,这里装控件...
  • taoerit
  • taoerit
  • 2015年12月30日 18:23
  • 4815

C#制作的渐变Button控件

  • 2009年01月01日 10:02
  • 26KB
  • 下载

c#的控件重绘 包括Button、RadioButton、ComboBox、TextBox、GroupBox

控件的形状可以通过Region属性来改变,用GraphicsPath来闭合一个特殊形状,然后传给Region属性就可以了。 通过GraphicsPath绘制的图形好像不能去掉锯齿,我写了抗锯齿但是没...

c#设置button、label等控件的背景色为透明

yous

C#之WinForm基础 获得button控件的宽与高(定值)

慈心积善融学习,技术愿为有情学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。 using System; using System.Collectio...

C# Listview 控件 加 button按钮的实现

没写过几篇博客,就当练练手,记录一下笔记,不喜勿喷!  开始正文 本程序实现的效果是当鼠标点击控件的一行,在所选行的末尾出现一个button按钮,并为button按钮添加响应事件。 1、工程准备。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:c#下重写BUTTON控件
举报原因:
原因补充:

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