Results 1 to 7 of 7

Thread: [RESOLVED] Drawing Polygon, Runtime

  1. #1

    Thread Starter
    Member Mythrandil's Avatar
    Join Date
    Mar 2006
    Posts
    55

    Resolved [RESOLVED] Drawing Polygon, Runtime

    Hey all

    Im having some trouble with some seemingly simple code

    VB Code:
    1. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.  
    3. If Design Then
    4.         Points(UBound(Points)).X = X
    5.         Points(UBound(Points)).Y = Y
    6.        
    7.         Dim handle As Long
    8.         Dim temp1 As Long
    9.        
    10.         Picture1.Cls
    11.         handle = Picture1.hdc
    12.         temp1 = Polygon(handle, Points(0), (UBound(Points) + 1))
    13.        
    14.         ReDim Preserve Points(UBound(Points) + 1) As POINTAPI
    15.    
    16.     End If
    17.  
    18. 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

  2. #2
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Drawing Polygon, Runtime

    You don't really need API's to draw polygons. Try this

    VB Code:
    1. Private Type Point
    2.     X As Long
    3.     Y As Long
    4. End Type
    5. Dim Pnt() As Point
    6. Private Sub Form_Load()
    7. ReDim Preserve Pnt(0)
    8. Picture1.AutoRedraw = True
    9. Timer1.Interval = 1
    10. End Sub
    11. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    12.     ReDim Preserve Pnt(UBound(Pnt) + 1)
    13. If Button = 1 Then
    14.     Pnt(UBound(Pnt)).X = X
    15.     Pnt(UBound(Pnt)).Y = Y
    16. ElseIf Button = 2 Then
    17.     Pnt(UBound(Pnt)).X = Pnt(1).X
    18.     Pnt(UBound(Pnt)).Y = Pnt(1).Y
    19. End If
    20. End Sub
    21.  
    22. Private Sub Timer1_Timer()
    23. If UBound(Pnt) < 1 Then Exit Sub
    24. Picture1.Cls
    25.  
    26. Picture1.Line (Pnt(1).X, Pnt(1).Y)-(Pnt(1).X, Pnt(1).Y)
    27.  
    28. For p = 2 To UBound(Pnt) Step 1
    29.     Picture1.Line -(Pnt(p).X, Pnt(p).Y)
    30. Next
    31. End Sub

  3. #3

    Thread Starter
    Member Mythrandil's Avatar
    Join Date
    Mar 2006
    Posts
    55

    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.

  4. #4
    Addicted Member
    Join Date
    Apr 2006
    Location
    USA
    Posts
    207

    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:
    1. Option Explicit
    2. Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As pointApi, ByVal nCount As Long) As Long
    3. Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As pointApi) As Long
    4.  
    5. Dim points() As pointApi
    6. Dim lastPos As pointApi
    7. Dim pointsCount As Integer
    8.  
    9. Private Type pointApi
    10.     x As Long
    11.     y As Long
    12. End Type
    13.  
    14.  
    15. Private Sub Picture1_DblClick()
    16.     'start a new polygon
    17.     pointsCount = 0
    18.    
    19.     'save the last polygon so it won't be cleared
    20.     Picture1.Picture = Picture1.Image
    21. End Sub
    22.  
    23. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    24.  
    25.     If Button = vbLeftButton Then 'this can be changed to Design=true
    26.         pointsCount = pointsCount + 1
    27.         ReDim Preserve points(pointsCount) As pointApi
    28.        
    29.         points(pointsCount).x = x
    30.         points(pointsCount).y = y
    31.                
    32.         Picture1.Cls
    33.        
    34.         If pointsCount > 1 Then
    35.             Call Polygon(Picture1.hdc, points(1), pointsCount)
    36.         Else
    37.             'first point
    38.             Call MoveToEx(Picture1.hdc, x, y, lastPos)
    39.             Picture1.PSet (x, y)
    40.         End If
    41.     End If
    42.  
    43. 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]

  5. #5

    Thread Starter
    Member Mythrandil's Avatar
    Join Date
    Mar 2006
    Posts
    55

    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 ^^

  6. #6
    Addicted Member
    Join Date
    Apr 2006
    Location
    USA
    Posts
    207

    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]

  7. #7

    Thread Starter
    Member Mythrandil's Avatar
    Join Date
    Mar 2006
    Posts
    55

    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 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
  •  



Click Here to Expand Forum to Full Width