. NET 技术讨论

学于明志,交流增加见识,讨论改变思维
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

实现XP风格的工具栏按钮

Posted on 2006-05-12 11:07    阅读(378)  评论(0编辑  收藏  举报
  1 using System;
  2 using System.ComponentModel;
  3 using System.Drawing;
  4 using System.Data;
  5 using System.Windows.Forms;
  6 
  7 namespace DocCenter
  8 
  9     /// <summary>
 10     /// 实现XP风格的工具栏按钮
 11     /// </summary>
 12     public class ToolBarXP : System.Windows.Forms.ToolBar
 13     {
 14         /// <summary>
 15         /// 需要用到的一些变量
 16         /// </summary>
 17         private System.ComponentModel.Container components = null;
 18         private Color fillColorHover = Color.FromArgb(182,189,210);
 19         private Color borderColorHover = Color.Navy;
 20 //        private Color fillDownColor = Color.RosyBrown;
 21 //        private Color borderDownColor = Color.SeaGreen;    
 22 
 23  
 24         public ToolBarXP(System.ComponentModel.IContainer container)
 25         {
 26             ///
 27             /// Required for Windows.Forms Class Composition Designer support
 28             ///
 29             container.Add(this);
 30             InitializeComponent();
 31             //指定控件的样式和行为
 32             this.SetStyle(
 33                 ControlStyles.AllPaintingInWmPaint |
 34                 ControlStyles.DoubleBuffer |
 35                 ControlStyles.ResizeRedraw |
 36                 ControlStyles.UserPaint,true);        
 37 
 38         }
 39  
 40         public ToolBarXP()
 41         {
 42             ///
 43             /// Required for Windows.Forms Class Composition Designer support
 44             ///
 45             InitializeComponent(); 
 46             //
 47             // TODO: Add any constructor code after InitializeComponent call
 48             //
 49 
 50             //指定控件的样式和行为,避免闪烁
 51             this.SetStyle(
 52                 ControlStyles.AllPaintingInWmPaint |
 53                 ControlStyles.DoubleBuffer |
 54                 ControlStyles.ResizeRedraw |
 55                 ControlStyles.UserPaint 
 56                  , true);
 57 
 58         }
 59         protected override void Dispose( bool disposing )
 60         {
 61             if( disposing )
 62             {
 63                 if(components != null)
 64                 {
 65                     components.Dispose();
 66                 }
 67             }
 68             base.Dispose( disposing );
 69         }
 70  
 71         /// <summary>
 72         /// 鼠标在按钮之上时,按钮呈现的背景色
 73         /// </summary>
 74         [Description("鼠标在按钮之上时,按钮呈现的背景色"),Category("Natrpan")]
 75         public  Color FillColorHover
 76         {
 77             get { return fillColorHover;}
 78             set { fillColorHover = value;}
 79         }
 80         /// <summary>
 81         /// 鼠标在按钮之上时,按钮呈现的边框色
 82         /// </summary>
 83         [Description("鼠标在按钮之上时,按钮呈现的边框色"),Category("Natrpan")]
 84         public Color BorderColorHover
 85         {
 86             get {return borderColorHover;}
 87             set { borderColorHover = value;}
 88         }
 89  
 90         protected override void OnPaint(PaintEventArgs e)
 91         {    
 92             try{
 93                     Graphics g = e.Graphics;            
 94                     for(int i= 0;i<this.Buttons.Count;i++)
 95                     {   //this.Focus() &&                 
 96                             ifthis.Buttons[i].Rectangle.Contains(PointToClient(MousePosition)) && this.Buttons[i].Style != ToolBarButtonStyle.Separator)//间隔线
 97                             {  
 98                                 g.FillRectangle(new SolidBrush(this.fillColorHover),this.Buttons[i].Rectangle);
 99                                 if(this.Buttons[i].ImageIndex != -1)
100                                 {
101                                     g.DrawImage(this.ImageList.Images[this.Buttons[i].ImageIndex],this.Buttons[i].Rectangle.X+1,this.Buttons[i].Rectangle.Y+1);
102                                 }
103                                 //画边框
104                                 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);
105                                 continue;
106                             }
107                         
108                         if(this.Buttons[i].ImageIndex != -1)  //没有图片时
109                         {   //    g.DrawImage(this.ImageList.Images[i],this.Buttons[i].Rectangle.X,this.Buttons[i].Rectangle.Y); 
110                             g.DrawImage(this.ImageList.Images[this.Buttons[i].ImageIndex],this.Buttons[i].Rectangle.X,this.Buttons[i].Rectangle.Y);
111                         }//画图                
112                         if(this.Buttons[i].Style == ToolBarButtonStyle.Separator)
113                         {    //填充一个区域(间隔线)            
114                             g.FillRectangle(new SolidBrush(SystemColors.ControlDark),this.Buttons[i].Rectangle.X,this.Buttons[i].Rectangle.Y,this.Buttons[i].Rectangle.Width / 4,this.Buttons[i].Rectangle.Height);
115                         }
116 
117                     }//for
118             }
119             catch(Exception err)
120             {
121                 DocCenter.formDocMain.Global_DocManagerLog.LogException(err);
122             }
123         
124         }  
125 
126         protected override void OnClick(EventArgs e)
127         {   
128             this.Refresh();
129             base.OnClick (e);
130         }
131         
132     
133         #region Component Designer generated code
134         /// <summary>
135         /// Required method for Designer support - do not modify
136         /// the contents of this method with the code editor.
137         /// </summary>
138         private void InitializeComponent()
139         {
140             components = new System.ComponentModel.Container();
141         }
142         #endregion
143     }
144 }
145