How could I draw a Polygon in Visual Basic?

J

Thread Starter

Javier Garcia

I have been playing with Visual Basic trying to find out why many people say that Visual Basic could be use as HMI. My first steps were oriented to the graphics area and I tried drawing the most common shapes used in any HMI (Circles, Rectangles, polygons). I expected to see in Visual Basic a friendly graphic editor to do that (as almost any HMI), but I haven't found any way to create Polygons, even triangles to change on them backcolor, styles, etc. Personalized valves, motors, general symbols involved on HMI applicatios alwas require shapes as polygons. How you guys are dealing with this?

Any advice would be appreciated.

Javier Garcia
Harper International
 
Y

Yosef Feigenbaum

This may not be a complete solution to the problem but you could always use the Windows API (gdi32) to call the polygon function. There are probably a bunch of sites on the internet where you could find examples of how to create a polygon from VB code.

Good Luck

(8{)} ( .)
yosi(AT)ludan.co.il
 
Process layout screens like you describe are not very easy to create in VB.

However, very often in HMI applications it will work fine with standard Windows controls.
 
In the past, I have drawn the icon or symbol in a graphic editor (e.g. MS Paint) and used the Image Control to display it on a form. Unfortunately, if I need to toggle colors of a symbol (e.g. from red to green to signify that it's ON) I needed a different file for that symbol. It worked fine for the application, but it didn't have a lot if different images.

-Todd
 
K

Koen Verschueren

First declare the Polygon function and the POINTAPI type (in a Form or Module):

Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long

Type POINTAPI
x As Long
y As Long
End Type

Then write the following code (For instance under Command button click):

Dim point(3) As POINTAPI
Dim handle As Long
Dim temp1 As Long

Form1.Cls
handle = Form1.hDC

point(0).x = 0
point(0).y = 0
point(1).x = 20
point(1).y = 20
point(2).x = 20
point(2).y = 10
point(3).x = 10
point(3).y = 20
temp1 = Polygon(handle, punt(0), 4)

This will draw a polygon with 4 point on form1.
You can also use a picture to draw on. Change the handle to Picture1.hDC
You can add more point by increasing the point() variable.
 
Visual Basic allows you to draw rectangles, filled rectangles and lines using the line method. You can also do oval and filled oval using the circle method. You can draw a polygon using multiple line commands. Those commands are not supported anymore in VB.NET, so you will have to redo this part of code if you ever decide to port it to .NET.

What you cannot do natively is draw a filled polygon (like a filled triangle). In order to do a filled triangle in VB, you the Win32 API 'Polygon' function.

Under VB.NET, it is completely different. Although you can still use Win32 API with unmanaged .NET, you have access to Graphics.DrawPolygon to draw a polygon.
 
Top