Results 1 to 14 of 14

Thread: [RESOLVED] Working with shape controls & drawing shapes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    19

    Resolved [RESOLVED] Working with shape controls & drawing shapes

    Hi...as some may know, I'm still working on my MS Paint-like program. What I'm trying to do is allow the user to click to create the center of a circle, move the mouse to where the edge of the circle will be, and click to create the circle.

    I've done it with a rectangle, but I'm having trouble doing it with the circle. Here's my code for the rectangle. Shape1 is a rectangle. You can click and drag and Shape1 is essentially a preview of the rectangle that will be drawn, then when you release the mouse, the rectangle is drawn. Similar to what I want to do with a circle.

    Code:
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    CurrentX = X
    CurrentY = Y
    
    Shape1.Visible = True
    Shape1.Height = 0
    Shape1.Width = 0
    Shape1.Left = X
    Shape1.Top = Y
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
        If X > CurrentX And Y > CurrentY Then
            Shape1.Width = X - CurrentX
            Shape1.Height = Y - CurrentY
        End If
    End If
    End Sub
    
    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Shape1.Visible = False
    Me.Line (CurrentX, CurrentY)-(X, Y), selectedColor, B
    End Sub
    Also - Didn't want to start a thread about this cuz It's a pretty lame question, but how do you clear a Picturebox's picture?

  2. #2
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Working with shape controls & drawing shapes

    To clear the picturebox picture:

    Set Picture1.Picture = LoadPicture(vbNullString)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    19

    Re: Working with shape controls & drawing shapes

    This is the code I have for positioning the circle in the first place, if that helps anyone:

    Code:
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    CurrentX = X
    CurrentY = Y
    
    Shape1.Left = X - Shape1.Height / 2
    Shape1.Top = Y - Shape1.Width / 2
    End Sub
    Where you click is the center of the circle.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Working with shape controls & drawing shapes

    In your other post, we informed you on how to create a circle using the .Circle function. I dont' see that in your example.

    Circle Radius = is 1/2 of the shape's width
    Circle center X = shape's left + width\2
    Circle center Y = shape's top + height\2

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Working with shape controls & drawing shapes

    if you want the drawing to colour filled, add F at the end of the method,

    to clear a drawing like that, use picturebox.cls if you want to clear a single drawing you would need to know its coordinates, the redraw it to background colour, or else redraw all the other items

    for your circle try like
    vb Code:
    1. b = Shape1.BorderWidth
    2. w = Shape1.Width - b * 2
    3. l = Shape1.Left + b
    4. t = Shape1.Top + b
    5. H = Shape1.Height - b * 2 ' should be the same as w
    6. Me.Circle (l + w / 2, t + H / 2), w / 2, vbRed
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    19

    Re: Working with shape controls & drawing shapes

    I don't know if you guys understand, so I'll try to reword.

    I want the user to be able to click to position the center of the circle, and drag the mouse to then change the size (drag further from center = larger circle, drag closer to circle = smaller circle).

    The point being that they can see the circle as they make it. Would it not make sense to use a shape control for the preview of the circle, seeing as this is going to be on a drawing or picture? After releasing the mouse, I'd use the .circle to create a new circle in the same place and make the preview invisible.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Working with shape controls & drawing shapes

    Why would the circle code be any different than your rectangle code you posted in #1 above? The only thing that would be different is that instead of drawing a rectangle when Mouse_Up happens, you would draw a circle.

    Also in your post #1, you have no code to resize/stretch the rectangle. If you have that code, the logic for the circle would be very similar.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Working with shape controls & drawing shapes

    Also in your post #1, you have no code to resize/stretch the rectangle
    see his mousemove code

    the circle is much harder in fact, unless i am missing something,
    try like this
    vb Code:
    1. Dim myx As Integer, myy As Integer ' global variables
    2. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    3.     If Button = 1 And Shape1.Visible = True And Shape1.Shape = vbShapeCircle Then
    4.     If Shape1.Width < 20 Then Shape1.Width = 200: Shape1.Height = 200
    5.     Shape1.Left = X - Shape1.Width / 2
    6.     Shape1.Top = Y - Shape1.Height / 2 '+ Me.Height - Me.ScaleHeight
    7.     End If
    8. '    Shape1.Move 0, 0
    9.    myx = X: myy = Y
    10.    Debug.Print X, Y
    11. End Sub
    12.  
    13. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    14. Dim cx As Integer, cy As Integer, sx As Integer, sy As Integer
    15. If Button > 0 Then
    16.     cx = Shape1.Left + Shape1.Width / 2
    17.     cy = Shape1.Top + Shape1.Height / 2
    18.     On Error Resume Next
    19.         If X > myx Then
    20.             Shape1.Width = (Sqr((myx - X) ^ 2 + (myy - Y) ^ 2))
    21.             Else
    22.             Shape1.Width = (Sqr((X - myx) ^ 2 - (myy - Y) ^ 2))
    23.         End If
    24.     On Error GoTo 0
    25.     Shape1.Move myx - Shape1.Width / 2, myy - Shape1.Width / 2, Shape1.Width, Shape1.Width
    26. '    mymove = False
    27. Debug.Print "M ", Shape1.Left + Shape1.Width / 2, Shape1.Top + Shape1.Height / 2  'checking center remains constant
    28. End If
    29. End Sub
    30. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    31. Dim b As Integer, w As Integer, l As Integer, h As Integer, t As Integer
    32.      b = Shape1.BorderWidth
    33.       w = Shape1.Width - b * 2
    34.       l = Shape1.Left + b
    35.       t = Shape1.Top + b
    36.       H = Shape1.Height - b * 2 ' should be the same as w
    37.       Me.Circle (l + w / 2, t + H / 2), w / 2, vbRed
    38.       End Sub
    ,
    this is far from perfect code, note the on error resume next, just to make it work, if you test this, you will see the red drawing over the black shape, if it works as you want you can then set the shape to visible or not
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    19

    Re: Working with shape controls & drawing shapes

    I got it to work through some trial and error. Thanks guys

    Code:
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
    If Button = 1 Then
    CurrentX = X
    CurrentY = Y
    End If
    Shape1.Left = X - Shape1.Width / 2
    Shape1.Top = Y - Shape1.Height / 2
    Shape1.Height = 0
    Shape1.Width = 0
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
    Label1.Caption = X - CurrentX
    Label2.Caption = Y - CurrentY
    Label3.Caption = CurrentX & "," & CurrentY
    
    
    a = X - CurrentX
    b = Y - CurrentY
    
    c = a ^ 2 + b ^ 2
    c = Sqr(c)
    
    Shape1.Height = c * 2
    Shape1.Width = c * 2
    
    Shape1.Left = CurrentX - Shape1.Width / 2
    Shape1.Top = CurrentY - Shape1.Height / 2
    End If
    End Sub
    
    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
    a = X - CurrentX
    b = Y - CurrentY
    
    c = a ^ 2 + b ^ 2
    c = Sqr(c)
    
    Me.Circle (CurrentX, CurrentY), c
    End If
    End Sub

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Working with shape controls & drawing shapes

    I got it to work through some trial and error.
    i think that is how all code works
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    19

    Re: [RESOLVED] Working with shape controls & drawing shapes

    Having one last problem. For some reason my shapes are filling when I don't want them too. I've made sure to set picImg.fillstyle to "vbTransparent", but they still fill. Anything I could be forgetting?

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Working with shape controls & drawing shapes

    depending on the shape type, a line shape uses the "F" to fill so omit that, also check for the fill properties of the form
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    19

    Re: [RESOLVED] Working with shape controls & drawing shapes

    The picturebox's fillstyle is set to transparent. This is getting me a filled box :/

    Code:
    picImg.Line (picImg.CurrentX, picImg.CurrentY)-(x, y), selectedColour, B
    It's always filling in black, even if I change the picturebox's fillcolor and changin selectedColor..

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Working with shape controls & drawing shapes

    the constant for transparent is VbFSTransparent, which is value of 1
    if the fillstyle is not correctly set (invalid constant) to some value it will be black fill (vbFSSolid, value 0), unless you set the fillcolor to someother colour
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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