Solved

making polygons

Posted on 1998-09-03
1
1,188 Views
Last Modified: 2012-05-04
Is there an easy way to draw polygons At runtime?
Like MSPaint polygon tool, if so how.
0
Comment
Question by:bradsoblesky
1 Comment
 
LVL 14

Accepted Solution

by:
waty earned 10 total points
ID: 1432993
Here is an article. You should adapt the declaration for use under Win32

How to Use FillPolygonRgn API to Fill Shape in Visual Basic
Last reviewed: June 21, 1995
Article ID: Q81470  
The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0


SUMMARY
Microsoft Visual Basic versions 2.0 and later for Windows include the Shape control which can be used for creating and filling six different geometric shapes. Alternatively, you can create a polygon region on a form or picture and fill it with a color, using the CreatePolygonRgn and FillRgn Windows API calls to draw and fill areas of the screen with color. Geometric shapes not provided with the Shape control, such as a triangle, can be created using this method.

More Information:

To draw a polygon on a form or picture control, you can use the Polygon API call; this will draw the edge of the polygon. You can then use CreatePolygonRgn to create an area that you can paint and use FillRgn to fill it with a color. Using these Windows API calls allows you to pick the points, the number of points, and to choose the color or brush to fill with.

The API calls used in the following example should be declared in the general Declarations section of your form. They are as follows:


API Call          Description
CreatePolygonRgn Creates a polygonal region


GetStockObject    Retrieves a handle to one of the predefined stock
                  pens, brushes, or fonts

FillRgn           Fills the region specified by the hRgn parameter
                  with the brush specified by the hBrush parameter

Polygon           Draws a polygon consisting of two or more points
                  connected by lines

Code Example
The following code example shows how to create a black triangle on a form. To change the program to create other shapes, add points to the array.


Run Visual Basic for Windows, or from the File menu, choose New Project (press ALT, F, N) if Visual Basic for Windows is already running. Form1 is created by default.

From the File menu, choose New Module (press ALT, F, M). Module1 is created by default.

Add the following code to the general declarations section of Module1 (in Visual Basic version 1.0 for Windows, add it to GLOBAL.BAS):

   Type Coord       ' This is the type structure for the x and y
      x As Integer  ' coordinates for the polygonal region.
      y As Integer
   End Type

   ' Enter each Declare statement as one, single line:
   Declare Function CreatePolygonRgn Lib "gdi" (lpPoints As Any,
      ByVal nCount As Integer, ByVal nPolyFillMode As Integer) As Integer
   Declare Function Polygon Lib "gdi" ByVal hDC As Integer,
      lpPoints As Any, ByVal nCount As Integer) As Integer
   Declare Function FillRgn Lib "gdi" (ByVal hDC As Integer,
      ByVal hRgn As Integer, ByVal hBrush As Integer) As Integer
   Declare Function GetStockObject Lib "gdi" (ByVal nIndex As Integer)
      As Integer
   Declare Function DeleteObject Lib "gdi" (ByVal hndobj As Integer)
      As Integer

   Global Const ALTERNATE = 1 ' ALTERNATE and WINDING are
   Global Const WINDING = 2   ' constants for FillMode.
   Global Const BLACKBRUSH = 4' Constant for brush type.


Add the following code to the Form_Click event for Form1:

   Sub Form_Click ()
      ' Dimension coordinate array.
      ReDim poly(1 To 3) As Coord
      ' Number of vertices in polygon.
      NumCoords% = 3
      ' Set scalemode to pixels to set up points of triangle.
      form1.scalemode = 3
      ' Assign values to points.
      poly(1).x = form1.scalewidth / 2
      poly(1).y = form1.scaleheight / 2
      poly(2).x = form1.scalewidth / 4
      poly(2).y = 3 * form1.scaleheight / 4
      poly(3).x = 3 * form1.scalewidth / 4
      poly(3).y = 3 * form1.scaleheight / 4
      ' Sets background color to red for contrast.
      form1.backcolor = &HFF
      ' Polygon function creates unfilled polygon on screen.
      ' Remark FillRgn statement to see results.
      bool% = Polygon(form1.hdc, poly(1), NumCoords%)
      ' Gets stock black brush.
      hbrush% = GetStockObject(BLACKBRUSH)
      ' Creates region to fill with color.
      hrgn% = CreatePolygonRgn(poly(1), NumCoords%, ALTERNATE)
      ' If the creation of the region was successful then color.
      If hrgn% Then bool% = FillRgn(form1.hdc, hrgn%, hbrush%)
      ' Print out some information.
      Print "FillRgn Return : ";bool%
      Print "HRgn : "; hrgn%
      Print "Hbrush : "; hbrush%
      Trash% = DeleteObject(hrgn%)
   End Sub


Run the program.

You should initially see a blank form. Click the form; a red background with a black triangle on it should be displayed. You can try different numbers of vertices by adding elements to the poly array and updating NumCoords. Different colors and brushes can be substituted as desired.
NOTE: If you try to fill a region with coordinates beyond the visible form, the CreatePolygonRgn function call will return a zero, meaning it was unsuccessful. The FillRgn will not work if the CreatePolygonRgn function was unsuccessful. All you will see is the outline created by the Polygon function. You should make certain that the vertices are all within the viewable form.

 

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA to copy paste columns form one file to other 20 86
MS Access Search and Replace Using VBA 6 59
How to debug this code 7 56
SLMGR Switches Are Not Working On KMS Host 3 70
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now