using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public partial class ButtonEx : Button
{
public ButtonEx() {
//首先开启双缓冲,防止闪烁
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
}
//用来标示是否鼠标正在悬浮在按钮上 true:悬浮在按钮上 false:鼠标离开了按钮
private bool BIsHover;
//用来标示是否鼠标点击了按钮 true:按下了按钮 false:松开了按钮
private bool BIsDown;
//重载鼠标悬浮的事件
protected override void OnMouseEnter(EventArgs e) {
//当鼠标进入控件时,标示变量为进入了控件
BIsHover = true;
//刷新面板触发OnPaint重绘
this.Invalidate();
base.OnMouseEnter(e);
}
//重载鼠标离开的事件
protected override void OnMouseLeave(EventArgs e) {
//当鼠标离开控件时,标示变量为离开了控件
BIsHover = false;
//刷新面板触发OnPaint重绘
this.Invalidate();
base.OnMouseLeave(e);
}
//重载鼠标按下的事件
protected override void OnMouseDown(MouseEventArgs mevent) {
//当鼠标按下控件时,标示变量为按下了控件
BIsDown = true;
//刷新面板触发OnPaint重绘
this.Invalidate();
base.OnMouseDown(mevent);
}
//重载鼠标松开的事件
protected override void OnMouseUp(MouseEventArgs mevent) {
//当鼠标松开时,标示变量为按下并松开了控件
BIsDown = false;
//刷新面板触发OnPaint重绘
this.Invalidate();
base.OnMouseUp(mevent);
}
//重载绘画事件
protected override void OnPaint(PaintEventArgs pevent) {
base.OnPaint(pevent);
//因为上面调用了base会绘制原生控件 重刷一下背景清掉原生绘制 不然自己绘制的是重叠在原生绘制上
base.OnPaintBackground(pevent);
//得到绘画句柄
Graphics GP = pevent.Graphics;
//定义字体格式
StringFormat SFont = new StringFormat();
SFont.Alignment = StringAlignment.Center;
SFont.LineAlignment = StringAlignment.Center;
//处理热键 当Alt点下时
SFont.HotkeyPrefix = this.ShowKeyboardCues ? HotkeyPrefix.Show : HotkeyPrefix.Hide;
//判断使用什么资源图
Bitmap bmpDraw = Properties.Resources.normal;
//如果禁用了,则使用禁用时的样式图片绘制,否则调用其他满足条件的样式图片绘制
if (!this.Enabled) bmpDraw = Properties.Resources.gray;
else if (BIsDown) bmpDraw = Properties.Resources.down;
else if (BIsHover) bmpDraw = Properties.Resources.high;
else if (this.Focused) bmpDraw = Properties.Resources.focus;
RenderBackground(GP, bmpDraw, this.ClientRectangle);
//如果禁用了
if (!this.Enabled) {
//则绘制双重阴影文字
GP.DrawString(this.Text, this.Font, Brushes.White, this.ClientRectangle, SFont);
GP.TranslateTransform(-1, -1);//左上移动一个单位坐标系
GP.DrawString(this.Text, this.Font, Brushes.DarkGray, this.ClientRectangle, SFont);
GP.ResetTransform();
return;
}
//否则,默认绘制正常字体
using (SolidBrush sb = new SolidBrush(this.ForeColor)) {
GP.DrawString(this.Text, this.Font, sb, this.ClientRectangle, SFont);
}
}
public static void RenderBackground(Graphics GP, Image img, Rectangle rect) {
//填充四个角
GP.DrawImage(img, new Rectangle(rect.X, rect.Y, 5, 5),
new Rectangle(0, 0, 5, 5), GraphicsUnit.Pixel);
GP.DrawImage(img, new Rectangle(rect.Right - 5, rect.Y, 5, 5),
new Rectangle(img.Width - 5, 0, 5, 5), GraphicsUnit.Pixel);
GP.DrawImage(img, new Rectangle(rect.X, rect.Bottom - 5, 5, 5),
new Rectangle(0, img.Height - 5, 5, 5), GraphicsUnit.Pixel);
GP.DrawImage(img, new Rectangle(rect.Right - 5, rect.Bottom - 5, 5, 5),
new Rectangle(img.Width - 5, img.Height - 5, 5, 5), GraphicsUnit.Pixel);
//四边
GP.DrawImage(img, new Rectangle(rect.X, rect.Y + 5, 5, rect.Height - 10),
new Rectangle(0, 5, 5, img.Height - 10), GraphicsUnit.Pixel);
GP.DrawImage(img, new Rectangle(rect.X + 5, rect.Y, rect.Width - 10, 5),
new Rectangle(5, 0, img.Width - 10, 5), GraphicsUnit.Pixel);
GP.DrawImage(img, new Rectangle(rect.Right - 5, rect.Y + 5, 5, rect.Height - 10),
new Rectangle(img.Width - 5, 5, 5, img.Height - 10), GraphicsUnit.Pixel);
GP.DrawImage(img, new Rectangle(rect.X + 5, rect.Bottom - 5, rect.Width - 10, 5),
new Rectangle(5, img.Height - 5, img.Width - 10, 5), GraphicsUnit.Pixel);
//中间
GP.DrawImage(img,
new Rectangle(rect.X + 5, rect.Y + 5, rect.Width - 10, rect.Height - 10),
new Rectangle(5, 5, img.Width - 10, img.Height - 10), GraphicsUnit.Pixel);
}
}
}
}
?
|