类似例子……需要注意的地方是从ToolBar继承的自定义控件不能直接override它的OnPaint函数,必须在它的construct函数设置它的外观由用户绘制,定义代码如下:this.SetStyle(ControlStyles.UserPaint,true);

解决方案 »

  1.   

    需要捕获Windows传递的消息,别人的代码可以参考一下
    class MyTextBox : System.Windows.Forms.TextBox  {  [DllImport("user32.dll")]  static extern IntPtr GetWindowDC(IntPtr hWnd);  [DllImport("user32.dll")]  static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);  const int WM_NCPAINT = 0x0085; 
    protected override void OnPaint(PaintEventArgs e)  {  base.OnPaint (e);  //Debug.WriteLine("WM_NCPAINT: hWnd = "+Handle.ToString()+"WParam = "+m.WParam.ToString());  IntPtr hDC = GetWindowDC(Handle);  if (hDC.ToInt32() == 0)  {  Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());  return;  }  Graphics g = Graphics.FromHdc(hDC);  Brush b = Brushes.Red;  Pen p = new Pen(b,5);  Rectangle r = new  Rectangle(0,0,this.Size.Width,this.Size.Height);  Debug.WriteLine("WM_NCPAINT: "+r.Top+","+r.Left+" -"+r.Bottom+","+r.Right);  g.DrawRectangle(p,r); 
    ReleaseDC(Handle,hDC);  }  protected override void WndProc(ref Message m)  {  switch(m.Msg)  {  case WM_NCPAINT:  {  base.WndProc(ref m);  Debug.WriteLine("WM_NCPAINT: hWnd = "+m.HWnd.ToString()+"WParam ="+m.WParam.ToString());  //Region rgn = Region.FromHrgn(m.WParam); 
    IntPtr hDC = GetWindowDC(m.HWnd);  if (hDC.ToInt32() == 0)  {  Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());  break;  }  Graphics g = Graphics.FromHdc(hDC);  Brush b = Brushes.Red;  Pen p = new Pen(b,5);  Rectangle r = new  Rectangle(0,0,this.Size.Width,this.Size.Height);  Debug.WriteLine("WM_NCPAINT: "+r.Top+","+r.Left+" -"+r.Bottom+","+r.Right);  g.DrawRectangle(p,r);  m.Result = IntPtr.Zero;  ReleaseDC(m.HWnd,hDC);  return;  }  }  base.WndProc(ref m);  }  }
      

  2.   

    看看这个例子需要注意的地方是从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
    }
      

  3.   

    CSTerry能否留下你的QQ,我的QQ是359149453