《C自义定Button控件重绘).docx》由会员分享,可在线阅读,更多相关《C自义定Button控件重绘).docx(4页珍藏版)》请在第一文库网上搜索。
1、C#自义定BUttOn控件(重绘)usingSystem;usingSystem.Co11ections.Generic;usingSystem.ComponentMode1;usingSystem.Data;usingSystem.Drawing;usingSystem.Drawing.Text;usingSystem.1inq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApp1ication1(pubicpartia1c1assForm1:Form(
2、pub1icForm1O(Initia1iZeComponentO;)privatevoidForm1_1oad(objectsender,EventArgse)pub1icpartia1c1assButtonEx:Button(pub1icButtonExO首先开启双缓冲,防止闪烁this.SetSty1e(Contro1Sty1es.UserPaint,true);this.SetSty1e(Contro1sty1es.ResizeRedraw,true);this.SetSty1e(Contro1sty1es.A11PaintingInWmPaint,true);this.SetSty1
3、e(Contro1Sty1es.OptiinizedDoub1eBuffer,true):this.SetSty1e(Contro1Sty1es.SupportsTransparentBackCo1or,true);this.BackCo1or=Co1or.Transparent;)用来标示是否鼠标正在悬浮在按钮上true:悬浮在按钮上fa1se:鼠标离开了按钮privateboo1BIsHover;/用来标示是否鼠标点击了按钮true:按下了按钮fa1se:松开了按钮privateboo1BIsDown;重载鼠标悬浮的事件protectedoverridevoidOnMouseEnter(E
4、ventArgse)当鼠标进入控件时,标示变量为进入了控件BIsHover=true;刷新面板触发OnPaint重绘this.InvaHdateO;base.OnMouseEnter(e);)重载鼠标离开的事件protectedoverridevoidOnMouse1eave(EventArgse)当鼠标离开控件时,标示变量为离开了控件BIsHover=fa1se;刷新面板触发OnPaint型绘this.Inva1idateO;base.OnMouse1eave(e);/重载鼠标按下的事件protectedoverridevoidOnMouseDown(MouseEventArgsmevent
5、)当鼠标按下控件时,标示变量为按下了控件BIsDown=true;刷新面板触发OnPaint垂绘this.Inva1idateO;base.OnMouseDown(mevent);)垂载鼠标松开的事件protectedoverridevoidOnMouse1Jp(MouscEventArgsmevent)当鼠标松开时,标示变量为按下并松开了控件BIsDown=fa1se;刷新面板触发OnPaint童绘this.Inva1idateO:base.OnMouseUp(mevent);)重载绘画事件protectedoverridevoidOnPaint(PaintEventArgspevent)b
6、ase.OnPaint(pevent);因为上面调用了base会绘制原生控件重刷一下背景清掉原生绘制不然自己绘制的是垄叠在原生绘制上base.OnPaintBackground(pevent);得到绘画句柄GraphicsGP=pevent.Graphics;定义字体格式StringFormatSFont=newStringFormat();SFont.1ignment=StringA1ignment.Center;SFont.1ineA1ignment=StringA1ignment.Center;处理热键当AIt点下时SFont.HotkeyPrefix=this.ShowKeyboard
7、Cues?HotkeyPrefix.Show:HotkeyPrefix.Hide;判断使用什么资源图BitmapbmpDraw=Properties.Resources,norma1;如果禁用了,则使用禁用时的样式图片绘制,否则调用其他满足条件的样式图片绘制if(!this.Enab1ed)bmpDraw=Properties.Resources,gray;e1seif(BIsDown)bmpDraw=Properties.Resources,down;e1seif(BIsHover)bmpDraw=Properties.Resources,high;e1seif(this.Focused)b
8、mpDraw=Properties.Resources,focus;RenderBackground(GP,bmpDraw,this.C1ientRectang1e);如果禁用了if(!this.Enab1ed)则绘制双重阴影文字GP.DrawString(this.Text,this.Font,Brushes.White,this.C1ientRectang1e,SFont);GP.Trans1ateTransform(-1,-1);左上移动一个单位坐标系GP.DrawString(this.Text,this.Font,Brushes.DarkGray,this.C1ientRectang
9、1e,SFont);GP.ResetTransformO;return;)否则,默认绘制正常字体using(So1idBrushsb=newSo1idBrush(this.ForeCo1or)GP.DrawString(this.Text,this.Font,sb,this.C1ientRectang1e,SFont);)pub1icstaticvoidRenderBackground(GraphicsGP,Imageimg,Rectang1erect)填充四个角GP.Drawimage(img,newRectang1e(rect.X,rect.Y,5,5),newRectang1e(O,O,
10、5,5),GraphicsUnit.Pixe1):GP.DrawImage(img,newRectang1e(rect.Right-5,rect.Y,5,5),newRectang1e(img.Width-5,O,5,5),Graphics11nit.Pixe1);GP.Drawimage(img,newRectang1e(rect.X,rect.Bottom-5,5,5),newRectangIe(O,img.Height-5,5,5),Graphics11nit.Pixe1);GP.DrawImage(img,newRectang1e(rect.Right-5,rect.Bottom-5,
11、5,5),newRectang1e(img.Width-5,img.Height-5,5,5),GraphicsUnit.Pixe1);四边GP.Drawimage(img,newRectang1e(rect.X,rect.Y+5,5,rect.Height-10),newRectangIe(0,5,5,img.Height-10),GraphicsUnit.Pixe1);GP.DrawImage(img,newRectang1e(rect.X+5,rect.Y,rect.Width-10,5),newRectangIe(5,O,img.Width-10,5),Graphics1Jnit.Pi
12、xe1);GP.DrawImage(img,newRectang1e(rect.Right-5,rect.Y+5,5,rect.Height-10),newRectangIe(img.Width-5,5,5,img.Height-10),GraphicsUnit.Pixe1);GP.DrawImage(img,newRectang1e(rect.X+5,rect.Bottom-5,rect.Width-10,5),newRectang1e(5,img.Height-5,img.Width-10,5),GraphicsUnit.Pixe1);中间GP.DrawImage(img,newRectang1e(rect.X+5,rect.Y+5,rect.Width-10,rect.Height-10),newRectangIe(5,5,img.Width-10,img.Height-10),GraphicsUnit.Pixe1);)