c#控件渐变色源代码
在winform 控件实际使用过程中,有时候我们为了好看,会使控件北京颜色显示为渐变设,
这个时候我们需要重写Paint 控件, 下面以panel 为例,核心方法:g.FillRectangle(b, this.ClientRectangle);
源代码如下:
using System.Drawing;
using System.Drawing.Drawing2D;
///
/// 渐变色
///
///
///
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Color FColor = Color.MintCream;
Color TColor = Color.PaleTurquoise;
//实例化刷子,第一个参数指示上色区域,第二个和第三个参数分别渐变颜色的开始和结束,第四个参数表示颜色的方向。
Brush b = new LinearGradientBrush(this.ClientRectangle, FColor, TColor, LinearGradientMode.Horizontal); //从左到右渐变色
// Brush b = new LinearGradientBrush(this.ClientRectangle, FColor, TColor, LinearGradientMode.Vertical); //从上到下渐变色
g.FillRectangle(b, this.ClientRectangle); //进行上色
}
效果如下:
原文链接:http://www.51jrft.com/jlmb/Others/23994.html