Text region clips shape : Region Clip « 2D Graphics « C# / C Sharp






Text region clips shape

Text region clips shape

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

public class Form1 : Form
{
    private System.Windows.Forms.CheckBox chkClipping;
  public Form1() {
        InitializeComponent();
  }

  private void checkBox1_CheckedChanged(object sender, EventArgs e)
  {
    Invalidate();
  }  
  private void Form1_Paint(object sender, PaintEventArgs e)
  {
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    Rectangle rectangle = new Rectangle(10, 10, 250, 50);
    GraphicsPath path = new GraphicsPath();
    path.AddEllipse(rectangle);
    e.Graphics.DrawPath(Pens.Red, path);
    
    Region clippingRegion = new Region(path);
    if (chkClipping.Checked) e.Graphics.Clip = clippingRegion;

    e.Graphics.DrawString("www.java2s.com",new Font("Verdana", 36, FontStyle.Bold), Brushes.Black, 10, 10);

    clippingRegion.Dispose();
    path.Dispose();

  }

  private void InitializeComponent()
  {
    this.chkClipping = new System.Windows.Forms.CheckBox();
    this.SuspendLayout();
    // 
    // chkClipping
    // 
    this.chkClipping.AutoSize = true;
    this.chkClipping.Checked = true;
    this.chkClipping.CheckState = System.Windows.Forms.CheckState.Checked;
    this.chkClipping.Location = new System.Drawing.Point(12, 300);
    this.chkClipping.Name = "chkClipping";
    this.chkClipping.Size = new System.Drawing.Size(80, 17);
    this.chkClipping.TabIndex = 0;
    this.chkClipping.Text = "Use Clipping";
    this.chkClipping.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(292, 329);
    this.Controls.Add(this.chkClipping);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Clipping";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    this.ResumeLayout(false);
    this.PerformLayout();

  }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}


           
       








Related examples in the same category

1.Clip many shapesClip many shapes