Microsoft KB Archive/818444

= HOW TO: Use GDI+ Regions on Forms to Create Customized Shapes =

Article ID: 818444

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework Class Libraries 1.1
 * Microsoft .NET Framework Class Libraries 1.0

-



IN THIS TASK

 * SUMMARY
 * What Is GDI+Region?
 * Clipping by Using a Region
 * Step-by-Step Example
 * REFERENCES



SUMMARY
This step-by-step article describes how to use GDI+ regions to create forms that have customized shapes such as lines, polygons, and curves.

back to the top

What Is GDI+Region?
The Microsoft Windows GDI+Region class permits you to define a custom shape. You can use lines, polygons, and curves to make the shape.

The Region class describes an area of the display surface. The area can be any shape. The boundary of the area can be a combination of curved and straight lines. You can also create regions by using paths, the interiors of rectangles, or a combination of these. You can use regions in clipping operations and hit-testing operations. Hit-testing determines whether the mouse was clicked in a specified region of the screen. Clipping restricts drawing operations on a specified region.

back to the top

Clipping by Using a Region
One property of the Graphics class is the clipping Region. The Graphics class provides methods for drawing lines, curves, figures, images, and text. A Graphics object stores attributes of the display device and attributes of the items to be drawn. All drawing that a Graphics object does is restricted to the clipping Region of that Graphics object. You can set the clipping Region by calling the SetClip method. You can set this Region as Form to specify the whole form as the Region.

back to the top

Step-by-Step Example
The following example creates a Form that has a polygon shape. The code constructs a Region that is based on the GraphicsPath property. The GraphicsPath property is constructed by using an array of Points. The Region is passed to the SetClip method of a Graphics object, and then the constructed Region is set as the Region of the Form by using the set_Region method.  Start Visual Studio .NET. On the File menu, point to New, and then click New Project. In the New Project dialog box, click Visual J# Projects under Project Types, click Windows application under Templates, and then click OK.  In Form1.jsl, replace the existing code with the following sample code: import System.Drawing.*; import System.Drawing.Drawing2D.*; import System.Collections.*; import System.ComponentModel.*; import System.Windows.Forms.*; import System.Data.*;

public class Form1 extends System.Windows.Forms.Form {   private System.ComponentModel.Container components = null;

public Form1 {       InitializeComponent; }

/**    * Clean up any resources being used. */   protected void Dispose(boolean disposing) {       if (disposing) {           if (components != null) {               components.Dispose; }       }        super.Dispose(disposing); }

#region Windows Form Designer generated code /**    * Required method for Designer support - do not modify * the contents of this method with the code editor. */   private void InitializeComponent {       this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13)); this.set_ClientSize(new System.Drawing.Size(292, 273)); this.set_Name(&quot;Form1&quot;); this.set_Text(&quot;Form1&quot;); this.add_Paint( new System.Windows.Forms.PaintEventHandler(this.Form1_Paint) ); }   #endregion

/**    * The main entry point for the application. */   /** @attribute System.STAThread */ public static void main(String[] args) {       Application.Run(new Form1); }

private void Form1_Paint (Object sender, System.Windows.Forms.PaintEventArgs e)   { // Create a path that is made up of a single polygon. Point polyPoints[] = {               new Point(10, 10), new Point(150, 10), new Point(100, 75), new Point(100, 150) };       GraphicsPath path = new GraphicsPath; path.AddPolygon(polyPoints); // Construct a region that is based on the path. Region region = new Region(path); // Draw the outline of the region. Pen pen= new Pen(Color.FromArgb(255, Color.get_AliceBlue), 20); Graphics graphics = e.get_Graphics; graphics.DrawPath(pen, path); // Set the clipping region of the Graphics object. graphics.SetClip(region, CombineMode.Replace); this.set_Region(region); } }  On the Debug menu, click Start.

back to the top

