配色: 字号:
C#自义定Button控件(重绘)
2022-04-21 | 阅:  转:  |  分享 
  
C#自义定Button控件(重绘)usingSystem;usingSystem.Collections.Generic;usingS
ystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;using
System.Drawing.Text;usingSystem.Linq;usingSystem.Text;usingSy
stem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindows
FormsApplication1{publicpartialclassForm1:Form{publicFor
m1(){InitializeComponent();}privatevoidForm1_Load(objectse
nder,EventArgse){}publicpartialclassButtonEx:Button{
publicButtonEx(){//首先开启双缓冲,防止闪烁this.SetStyle(ControlStyles.Us
erPaint,true);this.SetStyle(ControlStyles.ResizeRedraw,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);this.Set
Style(ControlStyles.OptimizedDoubleBuffer,true);this.SetStyle(C
ontrolStyles.SupportsTransparentBackColor,true);this.BackColor
=Color.Transparent;}//用来标示是否鼠标正在悬浮在按钮上true:悬浮在按钮上false:鼠标离开了
按钮privateboolBIsHover;//用来标示是否鼠标点击了按钮true:按下了按钮false:松开了按钮
privateboolBIsDown;//重载鼠标悬浮的事件protectedoverridevoidOnMous
eEnter(EventArgse){//当鼠标进入控件时,标示变量为进入了控件BIsHover=true;//刷新
面板触发OnPaint重绘this.Invalidate();base.OnMouseEnter(e);}//重载鼠标离
开的事件protectedoverridevoidOnMouseLeave(EventArgse){//当鼠标离开控
件时,标示变量为离开了控件BIsHover=false;//刷新面板触发OnPaint重绘this.Invalidate
();base.OnMouseLeave(e);}//重载鼠标按下的事件protectedoverridevoid
OnMouseDown(MouseEventArgsmevent){//当鼠标按下控件时,标示变量为按下了控件BIsDow
n=true;//刷新面板触发OnPaint重绘this.Invalidate();base.OnMouseDown(m
event);}//重载鼠标松开的事件protectedoverridevoidOnMouseUp(MouseEve
ntArgsmevent){//当鼠标松开时,标示变量为按下并松开了控件BIsDown=false;//刷新面板触发
OnPaint重绘this.Invalidate();base.OnMouseUp(mevent);}//重载绘画事件
protectedoverridevoidOnPaint(PaintEventArgspevent){base.OnP
aint(pevent);//因为上面调用了base会绘制原生控件重刷一下背景清掉原生绘制不然自己绘制的是重叠在原生绘制上
base.OnPaintBackground(pevent);//得到绘画句柄GraphicsGP=pevent.Gra
phics;//定义字体格式StringFormatSFont=newStringFormat();SFont.Al
ignment=StringAlignment.Center;SFont.LineAlignment=StringAli
gnment.Center;//处理热键当Alt点下时SFont.HotkeyPrefix=this.ShowKeybo
ardCues?HotkeyPrefix.Show:HotkeyPrefix.Hide;//判断使用什么资源图Bitm
apbmpDraw=Properties.Resources.normal;//如果禁用了,则使用禁用时的样式图片绘制,否
则调用其他满足条件的样式图片绘制if(!this.Enabled)bmpDraw=Properties.Resource
s.gray;elseif(BIsDown)bmpDraw=Properties.Resources.down;el
seif(BIsHover)bmpDraw=Properties.Resources.high;elseif(th
is.Focused)bmpDraw=Properties.Resources.focus;RenderBackgroun
d(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,th
is.ClientRectangle,SFont);GP.ResetTransform();return;}//否则,默
认绘制正常字体using(SolidBrushsb=newSolidBrush(this.ForeColor)){
GP.DrawString(this.Text,this.Font,sb,this.ClientRectangle,SFo
nt);}}publicstaticvoidRenderBackground(GraphicsGP,Image
img,Rectanglerect){//填充四个角GP.DrawImage(img,newRectangle(re
ct.X,rect.Y,5,5),newRectangle(0,0,5,5),GraphicsUnit.Pix
el);GP.DrawImage(img,newRectangle(rect.Right-5,rect.Y,5,5
),newRectangle(img.Width-5,0,5,5),GraphicsUnit.Pixel);G
P.DrawImage(img,newRectangle(rect.X,rect.Bottom-5,5,5),n
ewRectangle(0,img.Height-5,5,5),GraphicsUnit.Pixel);GP.Dr
awImage(img,newRectangle(rect.Right-5,rect.Bottom-5,5,5)
,newRectangle(img.Width-5,img.Height-5,5,5),GraphicsUn
it.Pixel);//四边GP.DrawImage(img,newRectangle(rect.X,rect.Y+
5,5,rect.Height-10),newRectangle(0,5,5,img.Height-10)
,GraphicsUnit.Pixel);GP.DrawImage(img,newRectangle(rect.X+5
,rect.Y,rect.Width-10,5),newRectangle(5,0,img.Width-1
0,5),GraphicsUnit.Pixel);GP.DrawImage(img,newRectangle(rect.
Right-5,rect.Y+5,5,rect.Height-10),newRectangle(img.W
idth-5,5,5,img.Height-10),GraphicsUnit.Pixel);GP.DrawIma
ge(img,newRectangle(rect.X+5,rect.Bottom-5,rect.Width-1
0,5),newRectangle(5,img.Height-5,img.Width-10,5),GraphicsUnit.Pixel);//中间GP.DrawImage(img,newRectangle(rect.X+5,rect.Y+5,rect.Width-10,rect.Height-10),newRectangle(5,5,img.Width-10,img.Height-10),GraphicsUnit.Pixel);}}}}
献花(0)
+1
(本文系新用户99204...首藏)