Clipping Combinations

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class ClippingCombinations: Form
{
string strCaption = “CombineMode = “;
MenuItem miCombineMode;

public static void Main()
{
Application.Run(new ClippingCombinations());
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
public ClippingCombinations()
{
ResizeRedraw = true;

Menu = new MainMenu();
Menu.MenuItems.Add(“&CombineMode”);

EventHandler ehClick = new EventHandler(MenuCombineModeOnClick);

for (int i = 0; i < 6; i++) { MenuItem mi = new MenuItem("&" + (CombineMode)i); mi.Click += ehClick; mi.RadioCheck = true; Menu.MenuItems[0].MenuItems.Add(mi); } miCombineMode = Menu.MenuItems[0].MenuItems[0]; miCombineMode.Checked = true; } void MenuCombineModeOnClick(object obj, EventArgs ea) { miCombineMode.Checked = false; miCombineMode = (MenuItem) obj; miCombineMode.Checked = true; Text = strCaption + (CombineMode)miCombineMode.Index; Invalidate(); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, 2 * cx / 3, cy); grfx.SetClip(path); path.Reset(); path.AddEllipse(cx / 3, 0, 2 * cx / 3, cy); grfx.SetClip(path, (CombineMode)miCombineMode.Index); grfx.FillRectangle(Brushes.Red, 0, 0, cx, cy); } } [/csharp]

Write info about the clipping rectangle to the console window

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    String s = "text";
    Font f = new Font("Times New Roman", 12);
    SizeF sf = g.MeasureString(s, f, 400);
    RectangleF rf = new RectangleF(10, 10, sf.Width, sf.Height);

    g.DrawString(s, f, Brushes.Black, rf);
    f.Dispose();
    Console.WriteLine("Clipping region painted: " + e.ClipRectangle);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


draw something onto the drawing surface (with the clipping region applied)

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
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;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Rectangle rect = new Rectangle(10, 10, 80, 50);
    g.DrawRectangle(Pens.Black, rect);
    g.SetClip(rect);
    g.FillEllipse(Brushes.Blue, 20, 20, 100, 100);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


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); } } [/csharp]

Hatch Brush Styles

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Drawing2D; // LinearGradientBrush

namespace HatchBrushStyles
{
public class HatchBrushStyles : System.Windows.Forms.Form
{
private System.ComponentModel.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.components = new System.ComponentModel.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(); } } } [/csharp]

Brush Style


   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GDI_Basics
{
    public class HatchBrushes : System.Windows.Forms.Form
    {
        private System.ComponentModel.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());
        }
    }
}


           
          


illustrates filling shapes with a brush


   

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Example21_4 : System.Windows.Forms.Form
{
  private System.ComponentModel.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);
  }
}