2012-07-16 20 views

回答

0

你需要做的是覆盖现有控制的绘画或创建自己的控制。就我个人而言,我会覆盖Panel控件的绘画,这将成为窗口整个底部的面板。

渐变面板是一个常见的要求,并有一个博客文章here(代码如下)。

显然你不想绘制完整的渐变,但它展示了绘画如何在控件上工作。

您既可以使用整个面板的较小子集绘制渐变,也可以使用类似Graphics.DrawPath的东西绘制渐变,并以正确的颜色绘制直线。


博客邮编(避免死链接):

using System; 
using System.ComponentModel; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Windows.Forms; 

namespace PostXING.Controls 
{ 
    /// <summary> 
    /// GradientPanel is just like a regular panel except it optionally 
    /// shows a gradient. 
    /// </summary> 
    [ToolboxBitmap(typeof(Panel))] 
    public class GradientPanel : Panel 
    { 
     /// <summary> 
     /// Property GradientColor (Color) 
     /// </summary> 
     private Color _gradientColor; 
     public Color GradientColor { 
      get { return this._gradientColor;} 
      set { this._gradientColor = value;} 
     } 

     /// <summary> 
     /// Property Rotation (float) 
     /// </summary> 
     private float _rotation; 
     public float Rotation { 
      get { return this._rotation;} 
      set { this._rotation = value;} 
     } 

     protected override void OnPaint(PaintEventArgs e) { 
         if(e.ClipRectangle.IsEmpty) return; //why draw if non-visible? 

      using(LinearGradientBrush lgb = new 
          LinearGradientBrush(this.ClientRectangle, 
         this.BackColor, 
         this.GradientColor, 
         this.Rotation)){ 
       e.Graphics.FillRectangle(lgb, this.ClientRectangle); 
      } 

         base.OnPaint (e); //right, want anything handled to be drawn too. 
     } 
    } 
} 
相关问题