用c#实现Xp风格的按钮

/* * 作者:zh * 完成时间:2005年8月17日 */ using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.ComponentModel; using System.Windows; using System.Windows.Forms; #region XP风格的按钮 /// /// XP风格的按钮 /// #endregion [ToolboxItem(true)] [ToolboxBitmap(typeof(XpStyleButton),"XpStyleButton.bmp")] public class XpStyleButton : System.Windows.Forms.Button { #region 是否设置XP样式 /// /// 是否设置XP样式 /// #endregion private bool _xpStyle; #region xpStyle的边框值 /// /// xpStyle的边框值 /// /// /// 应用XpStyle时边框值 /// #endregion private const int BORDER = 4; #region 按钮当前状态 /// /// 按钮当前状态 /// #endregion private ButtonType _buttonType; #region 左/右垂直坐标数组 /// /// 左/右垂直坐标数组 /// #endregion private System.Drawing.Rectangle[] rectVertical; #region 边框画笔 /// /// 边框画笔 /// #endregion private Pen borderPen; #region 背景渐变画笔 /// /// 背景渐变画笔 /// #endregion private LinearGradientBrush backgroundBrush; #region 垂直渐变画笔 /// /// 垂直渐变画笔 /// #endregion private LinearGradientBrush hverBrush; #region 顶部画笔 /// /// 顶部画笔 /// #endregion private Pen topPen; #region 底部画笔 /// /// 底部画笔 /// #endregion private Pen bottomPen; #region 按下时的画笔 /// /// 按下时的画笔 /// #endregion private Brush pushBrush; #region 默认构造函数 /// /// 默认构造函数 /// #endregion public XpStyleButton() { this._buttonType = ButtonType.Normal; this._xpStyle = true; } #region 设置按钮状态 /// /// 设置按钮状态 /// #endregion [Category("按钮状态"),DefaultValue(ButtonType.Normal),Description("设置按钮状态")] public ButtonType SetButtonType { get{ return this._buttonType; } set{ this._buttonType = value; } } #region 是否使用XP格式按钮 /// /// 是否使用XP格式按钮 /// #endregion [Category("XP格式"),DefaultValue(true),Description("是否使用XP格式按钮")] public bool SetXpStyle { get{ return this._xpStyle; } set{ this._xpStyle = value; } } #region 重写的事件函数 protected override void OnPaint(PaintEventArgs e) { /* * 是否使用XpStyle风格 */ if( !this._xpStyle ) { base.OnPaint( e ); return; } #region 画XpStyle风格 int X = this.Width; int Y = this.Height; e.Graphics.FillRectangle( new SolidBrush(this.BackColor),new Rectangle(0,0,X,Y) ); this.CreateBrush(); this.CreateRect(); switch( this._buttonType ) { case ButtonType.Normal: //正常状态 e.Graphics.FillRectangle( this.backgroundBrush,2,2,X-4,Y-4 ); this.DrawBorder( e ); break; case ButtonType.MouseOver: //鼠标停留时 e.Graphics.FillRectangle( this.backgroundBrush,2,2,X-4,Y-4 ); //画背景 this.DrawBorder( e ); //垂直 e.Graphics.FillRectangles( this.hverBrush,this.rectVertical ); //画顶部线 e.Graphics.DrawLine( this.topPen,3,2,X-4,2 ); e.Graphics.DrawLine( this.topPen,2,3,X-3,3 ); //画底部线 e.Graphics.DrawLine( this.bottomPen,4,Y-3,X-5,Y-3 ); e.Graphics.DrawLine( this.bottomPen,3,Y-4,X-4,Y-4 ); break; case ButtonType.Push: //按下按钮时 e.Graphics.FillRectangle(this.pushBrush, 2, 2, X-4, Y-4); this.DrawBorder( e ); break; case ButtonType.Disable: e.Graphics.FillRectangle( this.backgroundBrush,2,2,X-4,Y-4 ); this.DrawBorder( e ); break; } this.DisposeBrush(); #endregion #region 绘制图片 //获取图片 Image image = null; if( this.Image != null ) image = this.Image; else { if( this.ImageList != null && this.ImageList.Images.Count > this.ImageIndex ) { image = this.ImageList.Images[ this.ImageIndex ]; } } if( image != null ) { StringFormat imgFormat = new StringFormat(); imgFormat.Alignment = StringAlignment.Near; VerticalAlignment vAlign = VerticalAlignment.TOP; this.ImageTextAlign( this.ImageAlign,ref imgFormat,ref vAlign ); //默认X,Y坐标 int _x; int _y; int nWidth,nHeight; if( image.Height <= ( this.Height - 2*XpStyleButton.BORDER ) && image.Width <= ( this.Width-2*XpStyleButton.BORDER ) ) { nWidth = image.Width; nHeight = image.Height; _x = this.XLocation( image.Width,imgFormat.Alignment ); _y = this.YLocation( image.Height,vAlign ); } else { _x = XpStyleButton.BORDER + 1; _y = XpStyleButton.BORDER + 1; nWidth = this.Width - 2*XpStyleButton.BORDER; nHeight = this.Height - 2*XpStyleButton.BORDER; } //绘制图像 e.Graphics.DrawImage( image, _x,_y,nWidth,nHeight ); } #endregion #region 绘制文本 if( this.Text != string.Empty ) { //定义起始点坐标 int _x = XpStyleButton.BORDER; int _y = XpStyleButton.BORDER; e.Graphics.PageUnit = GraphicsUnit.Pixel; //画布可用宽度 int gdiWidth = this.Width - 2*XpStyleButton.BORDER; //计算按当前字体大小绘制按钮文本所需的长度 SizeF fontLength = e.Graphics.MeasureString( this.Text,this.Font ); StringFormat fontFormat = new StringFormat(); //垂直对齐方式 VerticalAlignment vAlign = VerticalAlignment.TOP; //用于绘制的文字 string drawText = this.Text; //如果当前文本超过一行,则截断 int fontWidth = (int)fontLength.Width + 1; if( fontWidth > gdiWidth ) { //当前宽度可绘制的字符数 int fonts = gdiWidth / ( fontWidth/this.Text.Length ) + 1; if( fonts <= this.Text.Length ) drawText = this.Text.Substring( 0,fonts ); } //计算水平及垂直对齐方式 this.ImageTextAlign( this.TextAlign,ref fontFormat,ref vAlign ); _x = this.XLocation( (int)fontLength.Width,fontFormat.Alignment ); _y = this.YLocation( (int)this.Font.Height,vAlign ); //画字符串 e.Graphics.DrawString( drawText,this.Font,new SolidBrush(this.ForeColor),new Rectangle( _x,_y,fontWidth,(int)fontLength.Height ),fontFormat ); } #endregion } private void Test( PaintEventArgs e,string s ) { e.Graphics.DrawString(s,this.Font,new SolidBrush(this.ForeColor),5,5,new StringFormat() ); } protected override void OnMouseHover(EventArgs e) { base.OnMouseHover (e); this._buttonType = ButtonType.MouseOver; this.Invalidate(); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave (e); this._buttonType = ButtonType.Normal; this.Invalidate(); } protected override void OnMouseDown(MouseEventArgs e) { this._buttonType = ButtonType.Push; base.OnMouseDown (e); } protected override void OnEnabledChanged(EventArgs e) { this._buttonType = ButtonType.Disable; base.OnEnabledChanged (e); } #endregion #region 计算字符和图片的水平及垂直对齐方式 /// /// 计算字符和图片的水平及垂直对齐方式 /// /// 设置值 /// 水平 /// 垂直 #endregion private void ImageTextAlign( System.Drawing.ContentAlignment align, ref StringFormat fontFormat,ref VerticalAlignment vAlign ) { #region 计算字符和图片的水平及垂直对齐方式 switch( align ) { case ContentAlignment.BottomCenter: fontFormat.Alignment = StringAlignment.Center; vAlign = VerticalAlignment.BOTTOM; break; case ContentAlignment.BottomLeft: fontFormat.Alignment = StringAlignment.Near; vAlign = VerticalAlignment.BOTTOM; break; case ContentAlignment.BottomRight: vAlign = VerticalAlignment.BOTTOM; fontFormat.Alignment = StringAlignment.Far; break; case ContentAlignment.MiddleCenter: vAlign = VerticalAlignment.MIDDLE; fontFormat.Alignment = StringAlignment.Center; break; case ContentAlignment.MiddleLeft: vAlign = VerticalAlignment.MIDDLE; fontFormat.Alignment = StringAlignment.Near; break; case ContentAlignment.MiddleRight: vAlign = VerticalAlignment.MIDDLE; fontFormat.Alignment = StringAlignment.Far; break; case ContentAlignment.TopCenter: vAlign = VerticalAlignment.TOP; fontFormat.Alignment = StringAlignment.Center; break; case ContentAlignment.TopLeft: vAlign = VerticalAlignment.TOP; fontFormat.Alignment = StringAlignment.Near; break; case ContentAlignment.TopRight: vAlign = VerticalAlignment.TOP; fontFormat.Alignment = StringAlignment.Far; break; } #endregion } #region 计算当前水平对齐方式下X的坐标位置 /// /// 计算当前水平对齐方式下X的坐标位置 /// /// 所有字体的宽度 /// 水平对齐方式 /// #endregion protected int XLocation( int fontWidth,StringAlignment align ) { switch( align ) { case StringAlignment.Near: // 左对齐 return XpStyleButton.BORDER + 1; case StringAlignment.Center: //居中对齐 return this.Width/2 - fontWidth/2; case StringAlignment.Far: //右对齐 return this.Width - XpStyleButton.BORDER - fontWidth; } return XpStyleButton.BORDER+1; } #region 计算当前垂直对齐方式下Y的坐标位置 /// /// 计算当前垂直对齐方式下Y的坐标位置 /// /// 字体的高度 /// 垂直对齐方式 /// #endregion protected int YLocation( int fontHeight,VerticalAlignment align ) { switch( align ) { case VerticalAlignment.TOP: //顶部对齐 return XpStyleButton.BORDER + 1; case VerticalAlignment.MIDDLE: //居中对齐 return this.Height / 2 - fontHeight / 2; case VerticalAlignment.BOTTOM: //底部对齐 return this.Height - XpStyleButton.BORDER - fontHeight; } return XpStyleButton.BORDER + 1; } #region 创建画笔 /// /// 创建画笔 /// #endregion protected virtual void CreateBrush() { int X = this.Width; int Y = this.Height; switch( this._buttonType ) { case ButtonType.Normal: //正常 this.borderPen = new Pen( new SolidBrush( Color.FromArgb(0,60,116) ),1.0f ); this.backgroundBrush = new LinearGradientBrush( new Rectangle(2,2,X-4,Y-4),Color.FromArgb(255,255,255),Color.FromArgb(214,208,197),LinearGradientMode.Vertical ); break; case ButtonType.MouseOver: this.borderPen = new Pen( new SolidBrush( Color.FromArgb(0,60,116) ),1.0f ); this.backgroundBrush = new LinearGradientBrush( new Rectangle(2,2,X-4,Y-4),Color.FromArgb(255,255,255),Color.FromArgb(214,208,197),LinearGradientMode.Vertical ); this.hverBrush = new LinearGradientBrush( new Rectangle(2,3,X-4,Y-6),Color.FromArgb(253,224,155),Color.FromArgb( 249,183,58 ),LinearGradientMode.Vertical ); this.topPen = new Pen( new SolidBrush( Color.FromArgb( 253,216,137 ) ),1.0f ); this.bottomPen = new Pen( new SolidBrush( Color.FromArgb( 229,151,0 ) ),1.0f ); break; case ButtonType.Push: this.borderPen = new Pen( new SolidBrush( Color.FromArgb(0,60,116) ),1.0f ); this.pushBrush = new SolidBrush( Color.FromArgb( 232,230,218 ) ); this.topPen = new Pen( new SolidBrush( Color.FromArgb(232,230,218) ),1.0f ); break; case ButtonType.Disable: this.borderPen = new Pen( new SolidBrush( Color.FromArgb(209,205,202) ),1.0f ); this.backgroundBrush = new LinearGradientBrush( new Rectangle(2,2,X-4,Y-4),Color.FromArgb( 247,245,232 ),Color.FromArgb( 247,245,232 ),LinearGradientMode.Horizontal ); break; } } #region 画边框 /// /// 画边框 /// #endregion private void DrawBorder( PaintEventArgs e ) { int X = this.Width; int Y = this.Height; e.Graphics.DrawLine(this.borderPen, 1, 3, 3, 1); // / top.left e.Graphics.DrawLine(this.borderPen, X-2, 3, X-4, 1); // / top.right e.Graphics.DrawLine(this.borderPen, 1, Y-4, 3, Y-2); // / bottom.left e.Graphics.DrawLine(this.borderPen, X-2, Y-4, X-4, Y-2); // / bottom.right e.Graphics.DrawLine(this.borderPen, 1, 2, 2, 1); // / top.left e.Graphics.DrawLine(this.borderPen, 1, Y-3, 2, Y-2); // / bottom.left e.Graphics.DrawLine(this.borderPen, X-2, 2, X-3, 1); // / top.right e.Graphics.DrawLine(this.borderPen, X-2, Y-3, X-3, Y-2); // / bottom.right e.Graphics.DrawLine(this.borderPen, 3, 1, X-4, 1); // ---- top e.Graphics.DrawLine(this.borderPen, 3, Y-2, X-4, Y-2); // ---- bottom e.Graphics.DrawLine(this.borderPen, 1, 3, 1, Y-4); // | left e.Graphics.DrawLine(this.borderPen, X-2, 3, X-2, Y-4); // | right } #region 释放画笔所占用的资源 /// /// 释放画笔所占用的资源 /// #endregion protected virtual void DisposeBrush() { if( this.borderPen != null ) this.borderPen.Dispose(); if( this.backgroundBrush != null ) this.backgroundBrush.Dispose(); if( this.hverBrush != null ) this.hverBrush.Dispose(); if( this.topPen != null ) this.topPen.Dispose(); if( this.bottomPen != null ) this.bottomPen.Dispose(); if( this.pushBrush != null ) this.pushBrush.Dispose(); } #region 创建矩形数组 /// /// 创建矩形数组 /// #endregion protected virtual void CreateRect() { this.rectVertical = new Rectangle[ 2 ]; this.rectVertical[0] = new Rectangle(2, 4, 2, this.Height-6); this.rectVertical[1] = new Rectangle(this.Width-4, 4, 2, this.Height-6); } } //class XpStyleButton #region 按钮的五种状态类型 /// /// 按钮的五种状态类型 /// #endregion public enum ButtonType { #region 正常状态 /// /// 正常状态 /// #endregion Normal = 0x10, #region 鼠标停留在按钮上 /// /// 鼠标停留在按钮上 /// #endregion MouseOver = 0x30, #region 按下按钮时 /// /// 按下按钮时 /// #endregion Push = 0x40, #region 按钮禁用时 /// /// 按钮禁用时 /// #endregion Disable = 0x50 } // enum ButtonType #region 垂直对齐方式 /// /// 垂直对齐方式 /// #endregion public enum VerticalAlignment { #region 顶部对齐 /// /// 顶部对齐 /// #endregion TOP = 0x10, #region 垂直居中对齐 /// /// 垂直居中对齐 /// #endregion MIDDLE = 0x20, #region 底部对齐 /// /// 底部对齐 /// #endregion BOTTOM = 0x30 } //enum VerticalAlignment
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值