How to use GDI+ regions on forms for customized shapes by using Visual Basic .NET or Visual Basic 2005 (823589)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

SUMMARY

This step-by-step article describes how to use GDI+ regions to create forms with customized shapes such as lines, polygons, and curves. GDI+ is the next step in the evolution of the Microsoft Graphics Device Interface (GDI).

back to the top

Information About the GDI+ Region Class

The common language runtime uses GDI+. With GDI+, you can create graphics, draw text, and make changes to graphical images as objects. As the advanced implementation of GDI, GDI+ is designed to improve performance and to be easier to use. You can use GDI+ to render graphical images on Microsoft Windows forms and on Microsoft Windows controls.

You can use the GDI+ Region class to define a custom shape. The shape can be made up of lines, of polygons, and of curves. 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. Regions can also be created from the interiors of rectangles, paths, or both. Regions are used in clipping and in hit-testing operations. Hit-testing determines if a user clicked in a specified region of the screen. Clipping restricts the drawing method to a specified region.

back to the top

Use the Clipping Region Property

One of the properties of the Graphics class is the clipping region. The Graphics class provides methods to use to draw lines, curves, figures, images, and text. The 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. To set the clipping region, call the SetClip method. You can set the clipping region as the region of the Form.

back to the top

Create a Form That Contains a Customized Shape

The following example creates a form that is shaped like a polygon. The code constructs a region that is based on the GraphicsPath class. GraphicsPath is constructed with an array of Point structures. The Region is passed to the SetClip method of a Graphics object, and the constructed region is set as the region of the form by using the Region method.
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. In the Project Types section, click Visual Basic Projects. In the Templates section, click Windows Application. By default, Form1 is created.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. On the View menu, click Code.
  5. Add the following statement at the beginning of the code:
    Imports System.Drawing.Drawing2D
  6. Add the following code to the Form1_Load event handler:
    ' Create a path that is made up of a single polygon.
    Dim polyPoints() As Point = {New Point(10, 10), New Point(120, 10), New Point(420, 275), New Point(200, 250)}
    Dim path As New GraphicsPath()
    path.AddPolygon(polyPoints)
    ' Construct a region based on the path.
    Dim region1 As New Region(path)
    ' Draw the outline of the region.
    Dim pen1 As New Pen(Color.FromArgb(255, Color.AliceBlue), 20)
    ' Create a Graphics object.
    Dim graphics1 As Graphics
    graphics1 = Me.CreateGraphics
    graphics1.DrawPath(pen1, path)
    ' Set the clipping region of the Graphics object.
    graphics1.SetClip(region1, CombineMode.Replace)
    Me.Region = region1
  7. On the Build menu, click Build Solution.
  8. On the Debug menu, click Start.
back to the top

REFERENCES

For more information about GDI+ graphics, visit the following Microsoft Developer Network (MSDN) Web site:back to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbWindowsForms kbHOWTOmaster kbhowto kbgdipimaging kbgdippath kbgraphic kbGDIPlus kbForms KB823589 kbAudDeveloper