Apr 30th, 2006, 04:30 AM
#1
Thread Starter
Member
[RESOLVED] Drawing Polygon, Runtime
Hey all
Im having some trouble with some seemingly simple code
VB Code:
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Design Then
Points(UBound(Points)).X = X
Points(UBound(Points)).Y = Y
Dim handle As Long
Dim temp1 As Long
Picture1.Cls
handle = Picture1.hdc
temp1 = Polygon(handle, Points(0), (UBound(Points) + 1))
ReDim Preserve Points(UBound(Points) + 1) As POINTAPI
End If
end sub
The code is fairly self explanatory but just incase: if the form is in design mode the user can click on the picture box area to form points of a polygon, after each click the polygon is redrawn.
Tracing the code shows no obvious errors, the array is fills up perfectly with coords and the polkygon api returns values of 1 which is non zero which suggests no errors. However nothing on the picture box is drawn!
Thanks in advance
Apr 30th, 2006, 07:34 AM
#2
Re: Drawing Polygon, Runtime
You don't really need API's to draw polygons. Try this
VB Code:
Private Type Point
X As Long
Y As Long
End Type
Dim Pnt() As Point
Private Sub Form_Load()
ReDim Preserve Pnt(0)
Picture1.AutoRedraw = True
Timer1.Interval = 1
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReDim Preserve Pnt(UBound(Pnt) + 1)
If Button = 1 Then
Pnt(UBound(Pnt)).X = X
Pnt(UBound(Pnt)).Y = Y
ElseIf Button = 2 Then
Pnt(UBound(Pnt)).X = Pnt(1).X
Pnt(UBound(Pnt)).Y = Pnt(1).Y
End If
End Sub
Private Sub Timer1_Timer()
If UBound(Pnt) < 1 Then Exit Sub
Picture1.Cls
Picture1.Line (Pnt(1).X, Pnt(1).Y)-(Pnt(1).X, Pnt(1).Y)
For p = 2 To UBound(Pnt) Step 1
Picture1.Line -(Pnt(p).X, Pnt(p).Y)
Next
End Sub
Apr 30th, 2006, 08:19 AM
#3
Thread Starter
Member
Re: Drawing Polygon, Runtime
Hey thanks for taking the time to reply.
It was actually a silly mistake!
My picture1 was scaled in twips so i was passing massive numbers to polygon which was being drawn off-screen. Changed to pixels and its working great.
Apr 30th, 2006, 08:49 AM
#4
Addicted Member
Re: Drawing Polygon, Runtime
Hi Mythrandil,
I'm glad you got it to work. Here's a slightly modified version that might help you anyways.
VB Code:
Option Explicit
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As pointApi, ByVal nCount As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As pointApi) As Long
Dim points() As pointApi
Dim lastPos As pointApi
Dim pointsCount As Integer
Private Type pointApi
x As Long
y As Long
End Type
Private Sub Picture1_DblClick()
'start a new polygon
pointsCount = 0
'save the last polygon so it won't be cleared
Picture1.Picture = Picture1.Image
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbLeftButton Then 'this can be changed to Design=true
pointsCount = pointsCount + 1
ReDim Preserve points(pointsCount) As pointApi
points(pointsCount).x = x
points(pointsCount).y = y
Picture1.Cls
If pointsCount > 1 Then
Call Polygon(Picture1.hdc, points(1), pointsCount)
Else
'first point
Call MoveToEx(Picture1.hdc, x, y, lastPos)
Picture1.PSet (x, y)
End If
End If
End Sub
Keith_VB6
If you have any further questions, just ask.
If this solves things, then please mark the thread resolved.
[Thread Tools] --> [Mark Thread Resolved]
Apr 30th, 2006, 09:40 AM
#5
Thread Starter
Member
Re: Drawing Polygon, Runtime
Thanks for the help,
I'll deffinetly implement some of your code, however rather than saving pictures i intend to have an array of all polygons so the user can choose to delete objects at their will, as its a simple matter of removing the points and repainting the picture box ^^
Apr 30th, 2006, 02:31 PM
#6
Addicted Member
Re: [RESOLVED] Drawing Polygon, Runtime
You're welcome. Since you were storing the points into an array, I figuired you were making more than just a basic paint program. Good luck.
Keith_VB6
If you have any further questions, just ask.
If this solves things, then please mark the thread resolved.
[Thread Tools] --> [Mark Thread Resolved]
Apr 30th, 2006, 04:14 PM
#7
Thread Starter
Member
Re: [RESOLVED] Drawing Polygon, Runtime
/edit *deleted*
ok i shudn't be let loose on computers whn im this stressed. was silly mistake again, defined points as integers instead of longs ~_~
sorry about bump
Attached Files
Last edited by Mythrandil; Apr 30th, 2006 at 04:23 PM .
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width