在这之前 首先的准备好素材导入到工程资源里面去:
图什么的 这个就自己搞定吧 到处去扒一下皮 反正、、重点在于会搞就行、、- -!、、其实吧 这些图完全可以做成一张图 然后绘制的时候 自己从大图上面切图绘制就行了、、不然到时候 会有很多图看着别扭、、
代码直接贴上来、、 我觉得 代码完全不用解释、、本来就很简单、、
public partial class ButtonEx : Button { public ButtonEx() { //双缓冲的一大堆设置 具体参数含义参照msdn的ControlStyles枚举值 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; } private bool m_bMouseHover; private bool m_bMouseDown; protected override void OnMouseEnter(EventArgs e) { m_bMouseHover = true; this.Invalidate(); base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { m_bMouseHover = false; this.Invalidate(); base.OnMouseLeave(e); } protected override void OnMouseDown(MouseEventArgs mevent) { m_bMouseDown = true; this.Invalidate(); base.OnMouseDown(mevent); } protected override void OnMouseUp(MouseEventArgs mevent) { m_bMouseDown = false; this.Invalidate(); base.OnMouseUp(mevent); } protected override void OnPaint(PaintEventArgs pevent) { base.OnPaint(pevent); //因为上面调用了base会绘制原生控件 重刷一下背景清掉原生绘制 不然自己绘制的是重叠在原生绘制上 base.OnPaintBackground(pevent); Graphics g = pevent.Graphics; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; //处理热键 当Alt点下时 sf.HotkeyPrefix = this.ShowKeyboardCues ? HotkeyPrefix.Show : HotkeyPrefix.Hide; //判断使用什么资源图 Bitmap bmpDraw = Properties.Resources.QBtn_Normal; if (!this.Enabled) bmpDraw = Properties.Resources.Qbtn_Gray; else if (m_bMouseDown) bmpDraw = Properties.Resources.QBtn_Down; else if (m_bMouseHover) bmpDraw = Properties.Resources.QBtn_High; else if (this.Focused) bmpDraw = Properties.Resources.QBtn_Focus; //绘制背景(若不知道这句啥意思 参照九宫切图里面的代码) RenderHelper.RenderBackground(g, bmpDraw, this.ClientRectangle); if (!this.Enabled) { //绘制双重阴影文字 g.DrawString(this.Text, this.Font, Brushes.White, this.ClientRectangle, sf); g.TranslateTransform(-1, -1);//左上移动一个单位坐标系 g.DrawString(this.Text, this.Font, Brushes.DarkGray, this.ClientRectangle, sf); g.ResetTransform(); return; } using (SolidBrush sb = new SolidBrush(this.ForeColor)) { g.DrawString(this.Text, this.Font, sb, this.ClientRectangle, sf); } } }- -!、、上面几十行就搞定了、、下图是运行效果
分别是 正常状态的按钮 鼠标移动上去 获得焦点 禁用