心如止水

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

LinearGradientBrush指的是一个线性的坡度类,用它和GradientStop类可以为一个矩形区域填充渐变的颜色。

例如:

<!-- This rectangle is painted with a diagonal linear gradient. --> <Rectangle Width="200" Height="100"> <Rectangle.Fill> <LinearGradientBrush StartPoint="0,0" EndPoint="1,1"> <GradientStop Color="Yellow" Offset="0.0" /> <GradientStop Color="Red" Offset="0.25" /> <GradientStop Color="Blue" Offset="0.75" /> <GradientStop Color="LimeGreen" Offset="1.0" /> </LinearGradientBrush> </Rectangle.Fill> </Rectangle>注意,(0,0)指的是矩形的左上角,(1,1)指的是矩形的右下角。所以颜色的渐变效果如下:GradientStop.jpg对应的c#代码如下:
Rectangle diagonalFillRectangle = new Rectangle();
diagonalFillRectangle.Width = 200;
diagonalFillRectangle.Height = 100;
// Create a diagonal linear gradient with four stops.  
LinearGradientBrush myLinearGradientBrush =
    new LinearGradientBrush();
myLinearGradientBrush.StartPoint = new Point(0,0);
myLinearGradientBrush.EndPoint = new Point(1,1);
myLinearGradientBrush.GradientStops.Add(
    new GradientStop(Colors.Yellow, 0.0));
myLinearGradientBrush.GradientStops.Add(
    new GradientStop(Colors.Red, 0.25));               
myLinearGradientBrush.GradientStops.Add(
    new GradientStop(Colors.Blue, 0.75));       
myLinearGradientBrush.GradientStops.Add(
    new GradientStop(Colors.LimeGreen, 1.0));
 
// Use the brush to paint the rectangle.
diagonalFillRectangle.Fill = myLinearGradientBrush;
同理,如果要创建水平方向的颜色渐变,设置StartPoint为(0,0.5),EndPoint为(1,0.5),如果要创建竖直方向的颜色渐变,设置StartPoint为(0.5,0),EndPoint为(0.5,1)。

posted on 2008-10-13 17:20  cutebear  阅读(1381)  评论(1编辑  收藏  举报