Csharp/CSharp Tutorial/2D/LinearGradientBrush

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Brush Example for filling shapes

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
  class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, ClientRectangle);
      g.FillRectangle(Brushes.Red, new Rectangle(10, 10, 50, 50));
      Brush linearGradientBrush = new LinearGradientBrush(
        new Rectangle(10, 60, 50, 50), Color.Blue, Color.White, 45);
      g.FillRectangle(linearGradientBrush, new Rectangle(10, 60, 50, 50));
      linearGradientBrush.Dispose();
      g.FillEllipse(Brushes.Aquamarine, new Rectangle(60, 20, 50, 30));
      g.FillPie(Brushes.Chartreuse, new Rectangle(60, 60, 50, 50), 90, 210);
      g.FillPolygon(Brushes.BlueViolet, new Point[] {
                                new Point(110, 10),
                                new Point(150, 10),
                                new Point(160, 40),
                                new Point(120, 20),
                                new Point(120, 60),
      });
    }
  
    private void InitializeComponent()
    {
    }
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
  }

LinearGradientBrush and LinearGradientMode

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class LinearGradientBrushMode : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components;
  public LinearGradientBrushMode()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.LinearGradientBrushMode_Paint);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new LinearGradientBrushMode());
  }
  private void LinearGradientBrushMode_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    Rectangle r = new Rectangle(10, 10, 100, 100);
    LinearGradientBrush theBrush = null;
    int yOffSet = 10;
    Array obj = Enum.GetValues(typeof(LinearGradientMode));
    for(int x = 0; x < obj.Length; x++)
    {
      LinearGradientMode temp = (LinearGradientMode)obj.GetValue(x);
      theBrush = new LinearGradientBrush(r, Color.GreenYellow, Color.Blue, temp);      
      
      g.DrawString(temp.ToString(), new Font("Times New Roman", 10), new SolidBrush(Color.Black), 0, yOffSet);
      g. FillRectangle(theBrush, 150, yOffSet, 200, 50);
      yOffSet += 80;
    }
  }
}