Csharp/C Sharp/2D Graphics/PathGradientBrush — различия между версиями

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

Текущая версия на 11:39, 26 мая 2010

CenterColor

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        GraphicsPath gp = new GraphicsPath();
        gp.AddLine(10, 10, 110, 15);
        gp.AddLine(110, 15, 100, 96);
        gp.AddLine(100, 96, 15, 110);
        gp.CloseFigure();
        g.FillRectangle(Brushes.White, this.ClientRectangle);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        PathGradientBrush pgb = new PathGradientBrush(gp);
        pgb.CenterColor = Color.White;
        pgb.SurroundColors = new Color[] { Color.Blue };
        g.FillPath(pgb, gp);
        g.DrawPath(Pens.Black, gp);
        pgb.Dispose();
        gp.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Gradient Brush Ball

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class BouncingGradientBrushBall : Form {
    public static void Main() {
        Application.Run(new BouncingGradientBrushBall());
    }
    public BouncingGradientBrushBall() {
        ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs pea) {
        GraphicsPath path = new GraphicsPath();
        Rectangle rect = new Rectangle(10, 10, 30, 30);
        path.AddEllipse(rect);
        PathGradientBrush pgbrush = new PathGradientBrush(path);
        pgbrush.CenterPoint = new PointF((rect.Left + rect.Right) / 3,
                                         (rect.Top + rect.Bottom) / 3);
        pgbrush.CenterColor = Color.White;
        pgbrush.SurroundColors = new Color[] { Color.Red };
        pea.Graphics.FillRectangle(pgbrush, rect);
    }
}


Square Tile

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class SquareTile: Form
{
     const int iSide = 50;         // Side of square
   
     public static void Main()
     {
          Application.Run(new SquareTile());
     }
     public SquareTile()
     {
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }      
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Point[] apt = {new Point(0,     0),     new Point(iSide, 0), 
                         new Point(iSide, iSide), new Point(0,     iSide)};
   
          PathGradientBrush pgbrush =
                         new PathGradientBrush(apt, WrapMode.TileFlipXY);
   
          pgbrush.SurroundColors = new Color[] { Color.Red,  Color.Lime,
                                                 Color.Blue, Color.White};
   
          grfx.FillRectangle(pgbrush, 0, 0, cx, cy);
     }
}


Star Gradient Brush

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class StarGradientBrush: Form
{
     public static void Main()
     {
          Application.Run(new StarGradientBrush());
     }
     public StarGradientBrush()
     {
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }       
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Point[] apt = new Point[5];
          
          for (int i = 0; i < apt.Length; i++)
          {
               double dAngle = (i * 0.8 - 0.5) * Math.PI;
               apt[i] = new Point(
                         (int)(cx * (0.50 + 0.48 * Math.Cos(dAngle))),
                         (int)(cy * (0.50 + 0.48 * Math.Sin(dAngle))));
          }
          PathGradientBrush pgbrush = new PathGradientBrush(apt);
   
          pgbrush.CenterColor = Color.White;
          pgbrush.SurroundColors = new Color[1] { Color.Black };
   
          grfx.FillRectangle(pgbrush, 0, 0, cx, cy);
    }
}


Triangle Gradient Brush

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class TriangleGradientBrush: Form
{
     public static void Main()
     {
          Application.Run(new TriangleGradientBrush());
     }
     public TriangleGradientBrush()
     {
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }       
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Point[] apt  = { new Point(cx,  0),
                           new Point(cx, cy),
                           new Point(0,  cy) };
   
          PathGradientBrush pgbrush = new PathGradientBrush(apt);
   
          grfx.FillRectangle(pgbrush, 0, 0, cx, cy);
     }
}


Triangle Tile

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class TriangleTile: Form
{
     const int iSide = 50;         // Side of square for triangle
     MenuItem  miChecked;
   
     public static void Main()
     {
          Application.Run(new TriangleTile());
     }
     public TriangleTile()
     {
          ResizeRedraw = true; 
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&Wrap-Mode");
   
          foreach (WrapMode wm in Enum.GetValues(typeof(WrapMode)))
          {
               MenuItem mi = new MenuItem();
               mi.Text     = wm.ToString(); 
               mi.Click   += new EventHandler(MenuWrapModeOnClick);
               Menu.MenuItems[0].MenuItems.Add(mi);
          }
          miChecked = Menu.MenuItems[0].MenuItems[0];
          miChecked.Checked = true;
     }
     void MenuWrapModeOnClick(object obj, EventArgs ea)
     {
          miChecked.Checked = false;
          miChecked = (MenuItem) obj;
          miChecked.Checked = true;
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }        
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Point[] apt = { new Point(0,     0), 
                          new Point(iSide, 0), 
                          new Point(0,     iSide)};
   
          PathGradientBrush pgbrush = 
                    new PathGradientBrush(apt, (WrapMode) miChecked.Index);
   
          grfx.FillRectangle(pgbrush, 0, 0, cx, cy);
     }
}


Two-Triangle Tile

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class TwoTriangleTile : Form {
    const int iSide = 50;         // Side of square for triangle
    public static void Main() {
        Application.Run(new TwoTriangleTile());
    }
    public TwoTriangleTile() {
        ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs pea) {
        DoPage(pea.Graphics, ForeColor, ClientSize.Width, ClientSize.Height);
    }
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
        Point[] apt = { new Point(0, 0), new Point(iSide, 0), new Point(0, iSide) };
        PathGradientBrush pgbrush1 =
                       new PathGradientBrush(apt, WrapMode.TileFlipXY);
        apt = new Point[] {new Point(iSide, 0), new Point(iSide, iSide), 
                             new Point(0, iSide)};
        PathGradientBrush pgbrush2 =
                       new PathGradientBrush(apt, WrapMode.TileFlipXY);
        grfx.FillRectangle(pgbrush1, 0, 0, cx, cy);
        grfx.FillRectangle(pgbrush2, 0, 0, cx, cy);
    }
}