自定义xp风格工具栏
代码实现如下:
需要注意的地方是从ToolBar继承的自定义控件不能直接override它的OnPaint函数,必须在它的construct函数设置它的外观由用户绘制,定义代码如下:
this.SetStyle(ControlStyles.UserPaint,true);
/// <summary>
/// 实现XP风格的工具栏按钮
/// </summary>
public class ToolBarXP : System.Windows.Forms.ToolBar
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Color fillColorHover = Color.FromArgb(182,189,210);
private Color borderColorHover = Color.Navy;
private Color fillDownColor = Color.RosyBrown;
private Color borderDownColor = Color.SeaGreen;
public ToolBarXP(System.ComponentModel.IContainer container)
{
///
/// Required for Windows.Forms Class Composition Designer support
///
container.Add(this);
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
this.SetStyle(ControlStyles.UserPaint,true);
}
public ToolBarXP()
{
///
/// Required for Windows.Forms Class Composition Designer support
///
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
this.SetStyle(ControlStyles.UserPaint,true);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 鼠标在按钮之上时,按钮呈现的背景色
/// </summary>
[Description("鼠标在按钮之上时,按钮呈现的背景色"),
Category("Natrpan")]
public Color FillColorHover
{
get { return fillColorHover;}
set { fillColorHover = value;}
}
/// <summary>
/// 鼠标在按钮之上时,按钮呈现的边框色
/// </summary>
[Description("鼠标在按钮之上时,按钮呈现的边框色"),
Category("Natrpan")]
public Color BorderColorHover
{
get {return borderColorHover;}
set { borderColorHover = value;}
}
/// <summary>
/// 按钮被按下时的填充背景色
/// </summary>
[Description("按钮被按下时的背景色"),
Category("Natrpan")]
public Color FillDownColor
{
get {return fillDownColor;}
set { fillDownColor = value;}
}
/// <summary>
/// 按钮被按下时的边框色
/// </summary>
[Description("按钮被按下时的边框色"),
Category("Natrpan")]
public Color BorderDownColor
{
get {return borderDownColor;}
set { borderDownColor = value;}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
for(int i=0;i<this.Buttons.Count;i++)
{
if(this.Buttons[i].Rectangle.Contains(PointToClient(MousePosition)))
{
g.FillRectangle(new SolidBrush(this.fillColorHover),this.Buttons[i].Rectangle);
g.DrawImage(this.ImageList.Images[i],this.Buttons[i].Rectangle.X+1,this.Buttons[i].Rectangle.Y+1);
g.DrawRectangle(new Pen(this.borderColorHover),this.Buttons[i].Rectangle.X,this.Buttons[i].Rectangle.Y,this.Buttons[i].Rectangle.Width-2,this.Buttons[i].Rectangle.Height-2);
continue;
}
// g.DrawRectangle(Pens.Red,this.Buttons[i].Rectangle);
g.DrawImage(this.ImageList.Images[i],this.Buttons[i].Rectangle.X,this.Buttons[i].Rectangle.Y);
}
// g.FillRectangle(new SolidBrush(Color.Purple),this.ClientRectangle);
// base.OnPaint(e);
}
protected override void OnMouseHover(EventArgs e)
{
// Graphics g = this.CreateGraphics();
// g.DrawRectangle(Pens.SaddleBrown,this.Buttons[0].Rectangle);
// g.Dispose();
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}