Csharp/C Sharp/2D Graphics/Brush — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:41, 26 мая 2010
Содержание
Brushes.Black
using System;
using System.Drawing;
using System.Windows.Forms;
class HelloWorld: Form
{
public static void Main()
{
Application.Run(new HelloWorld());
}
public HelloWorld()
{
Text = "Hello World";
BackColor = Color.White;
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics graphics = pea.Graphics;
graphics.DrawString("Hello, Windows Forms!", Font,
Brushes.Black, 0, 0);
}
}
Brush Style
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace GDI_Basics
{
public class HatchBrushes : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public HatchBrushes()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(564, 390);
this.Resize += new System.EventHandler(this.HatchBrushes_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.HatchBrushes_Paint);
}
#endregion
private void HatchBrushes_Resize(object sender, System.EventArgs e)
{
this.Invalidate();
}
private void HatchBrushes_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
HatchBrush myBrush;
int y = 20;
int x = 20;
foreach (HatchStyle brushStyle in System.Enum.GetValues(typeof(HatchStyle)))
{
myBrush = new HatchBrush(brushStyle, Color.Blue, Color.LightYellow);
e.Graphics.FillRectangle(myBrush, x, y, 40, 20);
e.Graphics.DrawString(brushStyle.ToString(), new Font("Tahoma", 8),
Brushes.Black, 50 + x, y + 5);
y += 30;
if ((y + 30) > this.ClientSize.Height)
{
y = 20;
x += 180;
}
}
}
[STAThread]
static void Main()
{
Application.Run(new HatchBrushes());
}
}
}
Gradient brush demo
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
private System.ruponentModel.IContainer components;
public Form1(){
InitializeComponent();
}
#region Windows Form Designer generated code
private void InitializeComponent(){
this.ruponents = new System.ruponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.ruponents);
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(292, 260);
this.Name = "Form1";
this.Text = "LinearGradientBrush Demo";
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
#endregion
static void Main(){
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
this.BackColor = Color.FromArgb(255, 0, 0, 255);
}
private System.Windows.Forms.Timer timer1;
private float angle = 0;
private LinearGradientBrush GetBrush()
{
return new LinearGradientBrush(
new Rectangle( 20, 20, 200, 100),
Color.Orange,
Color.Yellow,
0.0F,
true);
}
private void Rotate( Graphics graphics, LinearGradientBrush brush )
{
brush.RotateTransform(angle);
brush.SetBlendTriangularShape(.5F);
graphics.FillRectangle(brush, brush.Rectangle);
}
private void Rotate(Graphics graphics)
{
angle += 5 % 360;
Rotate(graphics, GetBrush());
}
private void timer1_Tick(object sender, System.EventArgs e)
{
Rotate(CreateGraphics());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Rotate(e.Graphics);
}
}
Hatch Brush Styles
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D; // LinearGradientBrush
namespace HatchBrushStyles
{
public class HatchBrushStyles : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public HatchBrushStyles()
{
InitializeComponent();
this.Size = new Size(500, 150);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "HatchBrushStyles";
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new HatchBrushStyles());
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Font f = new Font(new FontFamily("Times New Roman"), 10);
Brush fb = new SolidBrush(Color.Black);
Color cb = Color.Red, cf =Color.White;
int wi = 30, hi = 25, rectNb = 14;
int x, y;
HatchBrush hb = null;
for(int i = 0; i < 53; i++)
{
x = (int)(i % rectNb);
y = (int)(i / rectNb);
hb = new HatchBrush((HatchStyle)i, cf, cb);
g.FillRectangle(hb, 2 + x*(5 + wi), 2 + y*(5 + hi), wi, hi);
}
fb.Dispose(); hb.Dispose(); g.Dispose();
}
}
}
Hexagon Gradient Brush
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class HexagonGradientBrush: Form
{
const float fSide = 50; // Side (also radius) of hexagon
public static void Main()
{
Application.Run(new HexagonGradientBrush());
}
public HexagonGradientBrush()
{
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)
{
float fHalf = fSide * (float) Math.Sin(Math.PI / 3);
PointF[] aptf = {new PointF( fSide, 0),
new PointF( fSide * 1.5f, 0),
new PointF( fSide, 0),
new PointF( fSide / 2, -fHalf),
new PointF(-fSide / 2, -fHalf),
new PointF(-fSide, 0),
new PointF(-fSide * 1.5f, 0),
new PointF(-fSide, 0),
new PointF(-fSide / 2, fHalf),
new PointF( fSide / 2, fHalf) };
PathGradientBrush pgbrush1 = new PathGradientBrush(aptf, WrapMode.Tile);
for (int i = 0; i < aptf.Length; i++)
{
aptf[i].X += fSide * 1.5f;
aptf[i].Y += fHalf;
}
PathGradientBrush pgbrush2 =
new PathGradientBrush(aptf, WrapMode.Tile);
grfx.FillRectangle(pgbrush1, 0, 0, cx, cy);
grfx.FillRectangle(pgbrush2, 0, 0, cx, cy);
}
}
illustrates filling shapes with a brush
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Example21_4 : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public Example21_4()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(400, 400);
this.Name = "Example21_4";
this.Text = "Example21_4";
this.Paint += new System.Windows.Forms.
PaintEventHandler(this.Example21_4_Paint);
}
static void Main()
{
Application.Run(new Example21_4());
}
private void Example21_4_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush brSolid = new SolidBrush(Color.Blue);
Brush brHatch = new HatchBrush(HatchStyle.HorizontalBrick,
Color.Red, Color.Yellow);
Brush brGradient = new LinearGradientBrush(new Rectangle(0, 0, 200, 200), Color.Black, Color.LightGray, 45, false);
g.FillRectangle(brGradient, 10, 10, 200, 200);
g.FillEllipse(brHatch, 200, 200, 150, 190);
g.FillPie(brSolid, 0, 0, 300, 300, 285, 75);
}
}
Solid brush demo
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class MainForm : System.Windows.Forms.Form
{
private System.ruponentModel.Container components;
public MainForm()
{
InitializeComponent();
BackColor = Color.LemonChiffon;
Text = "www.nfex.ru";
Size = new Size(200, 200);
CenterToScreen();
}
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.Text = "Form1";
this.Resize += new System.EventHandler(this.MainForm_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
}
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
private void MainForm_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("www.nfex.ru",
new Font("Times New Roman", 20),
new SolidBrush(Color.Black),
this.DisplayRectangle);
}
}
Use Brushes.Aquamarine to draw a Ellipse
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form{
private System.ruponentModel.Container components = null;
public Form1(){
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
}
protected override void Dispose( bool disposing ){
if( disposing ){
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, ClientRectangle);
g.FillEllipse(Brushes.Aquamarine, new Rectangle(60, 20, 50, 30));
}
private void InitializeComponent(){
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main() {
Application.Run(new Form1());
}
}
Use Brushes.BlueViolet ro draw a Polygon
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form{
private System.ruponentModel.Container components = null;
public Form1(){
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
}
protected override void Dispose( bool disposing ){
if( disposing ){
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, ClientRectangle);
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(){
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main() {
Application.Run(new Form1());
}
}
Use Brush to draw a Rectangle
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form{
private System.ruponentModel.Container components = null;
public Form1(){
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
}
protected override void Dispose( bool disposing ){
if( disposing ){
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
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));
}
private void InitializeComponent(){
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main() {
Application.Run(new Form1());
}
}