`
leonardleonard
  • 浏览: 821170 次
社区版块
存档分类
最新评论

c#下重写BUTTON控件

阅读更多
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控件GlassButton

    本案例中的"C#重写的button控件GlassButton"就是这样一个例子,它通过代码实现了玻璃效果。玻璃效果在视觉上给人一种透明、光滑且有深度的感觉,常用于现代应用的界面设计中,能提升整体的美观度和用户体验。 在C#...

    C#实现按钮Button的重写

    本文将深入探讨如何在C#中实现对按钮控件(Button)的重写,以创建自定义的“漂亮”按钮。 首先,我们要理解重写(Override)的概念。在面向对象编程中,重写是指子类重新定义父类中的某个方法,以便提供自己的实现...

    11、如何重写WinForm工程下Button控件实现渐变背景.zip

    总结来说,通过重写Button控件并覆盖OnPaint方法,我们可以实现自定义的渐变背景效果。同时,通过设置公共属性,允许开发者在设计时轻松调整颜色,提高了应用的定制性。这种方法不仅适用于Button控件,还可以应用到...

    C#制作的渐变Button控件

    在这个场景中,我们讨论的是一个使用C#创建的特殊Button控件,它具有渐变颜色的效果。这个控件利用了GDI+(Graphics Device Interface Plus)技术,这是一个Windows Presentation Foundation (WPF)和Windows Forms...

    C# 自定义控件 自定义Button控件 水晶按钮

    在这个例子中,我们的水晶按钮将会基于System.Windows.Forms.Button控件进行扩展。首先,创建一个新的类并让它继承自Button类: ```csharp public class CrystalButton : Button { // 控件的初始化和属性设置 } ``...

    C# winform 用户自定义控件--带checkbox功能的按钮

    在这个案例中,我们将基于Button控件进行扩展,因为它是最基本的交互元素,而CheckBox功能则可以增加更多的选择性操作。 创建自定义控件的步骤如下: 1. **创建新类**:新建一个C#类,例如命名为`UserButton`,并...

    重绘 C# button按钮

    默认情况下,系统会自动处理控件的绘制,但我们可以覆盖`OnPaint`事件来接管这个过程,从而实现自定义的绘图逻辑。 1. **创建自定义按钮类** 首先,我们需要创建一个继承自`System.Windows.Forms.Button`的新类。...

    C#自定义控件-三角形按钮

    1. **绘制三角形**:在自定义控件的`OnPaint`方法中,需要重写父类的绘图逻辑。使用`Graphics`对象,可以调用`DrawPolygon`方法绘制三角形。要绘制一个等腰三角形,可以设置三个点作为顶点,然后传递给`DrawPolygon`...

    C#自定义控件库

    1. **继承与派生**:在C#中,自定义控件通常是通过继承已有的系统控件(如Button、Label等)或UserControl类来实现的。这样可以从父类继承一些基本属性和方法,并添加新的功能。例如,你可以创建一个继承自TextBox的...

    重写TreeView控件

    本项目中的“重写TreeView控件”就实现了在节点后添加图片、Checkbox和Button等额外控件的功能,提升了用户体验和交互性。 首先,我们关注`MyTreeViewControl.cs`这个文件,它很可能是自定义`TreeView`控件的核心...

    c# 动态改变控件位置、控件大小、拖动控件

    当窗体大小改变时,你可以重写窗体的`SizeChanged`事件,根据新的窗体大小来调整控件的大小。例如,保持控件与窗体的比例关系: ```csharp private void Form1_SizeChanged(object sender, EventArgs e) { ...

    c#实现圆形按钮控件

    在C#编程中,创建一个圆形按钮控件可以极大地提升应用程序的界面美观度和用户体验。以下将详细讲解如何实现这个功能,以及相关的知识点。 首先,我们需要理解C#中的控件和自定义控件的概念。控件是用户界面的基本...

    C#自定义开关按钮控件,c#自定义控件方法,C#

    在你的主窗体(Form)中,你可以像使用普通Button控件一样实例化并添加自定义的`MySwitchButton`: ```csharp MySwitchButton mySwitch = new MySwitchButton(); mySwitch.Location = new Point(10, 10); ...

    c#自定义透明按钮控件

    "c#自定义透明按钮控件"就是这样一个实例,它展示了如何利用C#语言和.NET Framework或.NET Core来扩展系统默认的Button控件,以实现透明的效果。下面我们将深入探讨这个主题。 首先,自定义控件的创建通常涉及继承...

    C# 实现动态创建控件 调整大小 拖动 自适应窗体

    在C#编程中,动态创建控件是一项常用的技术,它允许开发者在运行时根据需要创建和添加用户界面元素。这通常用于构建可扩展性更强、更灵活的应用程序,比如自定义表单或复杂的用户交互界面。本文将深入探讨如何在C#中...

    C#实现在控件上画直线

    本文将深入探讨如何使用C#实现在WinForm控件上画直线,并介绍相关的绘图API和技术。 首先,我们需要了解C#中的Graphics类,它是.NET Framework提供的一个核心绘图类,允许开发者在各种图形设备上进行绘制,包括屏幕...

    C#自定义控件.rar

    这可以是系统提供的任何控件,如`System.Windows.Forms.Control`,`Button`,`Label`等。基础类的选择取决于你希望新控件具备的功能和行为。 2. **绘制控件**:C#控件的外观由`OnPaint`事件处理程序决定。你可以...

    C#实现各种自定义控件

    这通常通过继承已有的控件类,如Button、TextBox等,并在其基础上添加新的属性、方法和事件。自定义控件可以增强UI的交互性和视觉效果,比如创建一个带有动画效果的按钮。 2. **绘制控件**:自定义控件的一个关键...

Global site tag (gtag.js) - Google Analytics