C#自定义控件二XP按钮
C#自定义控件二XP按钮
效果图:
通过继承 Button按钮,重写OnPaint事件重新绘制。
为了增强效果,可以构造函数中this.Cursor = Cursors.Hand; 将鼠标形状改成手势。
第一步:重写 OnPaint事件
private bool mouseover = false;
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Color c5 = Color.FromA#ffffff;
Color c2 = Color.FromA#c0c0c0;
if (mouseover)
{
c5 = Color.FromA#f5f5f5;
c2 = Color.FromA#b4afbe;
}
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c5, c2, LinearGradientMode.Vertical);
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.FillPolygon(b, points, FillMode.Winding);
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;
Pen p = new Pen(Color.Orange, 2);
Pen p1 = new Pen(Color.Wheat, 2);
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);
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 OnMouseEnter(EventArgs e)
{
mouseover = true;
this.Invalidate(false);
base.OnMouseEnter(e);
}
第三步:重写鼠标移出事件
protected override void OnMouseLeave(EventArgs e)
{
mouseover = false;
this.Invalidate(false);
base.OnMouseLeave(e);
}
提示:可以将Color c5 和Color c2 改成自己想要的颜色,也可写成属性让用户选择,会产生不一样的效果哦!试试看吧!
贴出全部代码:
//控件名:myXPButton
//作者:刘典武
//时间:2011-06-02
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace myControl
{
public partial class myXPButton : Button
{
public myXPButton()
{
this.Cursor = Cursors.Hand;
}
private bool mouseover = false;
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Color c5 = Color.FromA#ffffff;//按钮表面上部颜色
Color c2 = Color.FromA#c0c0c0;//按钮表面下部颜色
if (mouseover)//鼠标移入的话重新改变
{
c5 = Color.FromA#f5f5f5;
c2 = Color.FromA#b4afbe;
}
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c5, c2, LinearGradientMode.Vertical);
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.FillPolygon(b, points, FillMode.Winding);
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;
Pen p = new Pen(Color.Orange, 2);
Pen p1 = new Pen(Color.Wheat, 2);
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);
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 OnMouseEnter(EventArgs e)
{
mouseover = true;
this.Invalidate(false);
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
mouseover = false;
this.Invalidate(false);
base.OnMouseLeave(e);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】