[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?
Re: Working with shape controls & drawing shapes
To clear the picturebox picture:
Set Picture1.Picture = LoadPicture(vbNullString)
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.
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
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:
b = Shape1.BorderWidth
w = Shape1.Width - b * 2
l = Shape1.Left + b
t = Shape1.Top + b
H = Shape1.Height - b * 2 ' should be the same as w
Me.Circle (l + w / 2, t + H / 2), w / 2, vbRed
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.
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.
Re: Working with shape controls & drawing shapes
Quote:
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:
Dim myx As Integer, myy As Integer ' global variables
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 And Shape1.Visible = True And Shape1.Shape = vbShapeCircle Then
If Shape1.Width < 20 Then Shape1.Width = 200: Shape1.Height = 200
Shape1.Left = X - Shape1.Width / 2
Shape1.Top = Y - Shape1.Height / 2 '+ Me.Height - Me.ScaleHeight
End If
' Shape1.Move 0, 0
myx = X: myy = Y
Debug.Print X, Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim cx As Integer, cy As Integer, sx As Integer, sy As Integer
If Button > 0 Then
cx = Shape1.Left + Shape1.Width / 2
cy = Shape1.Top + Shape1.Height / 2
On Error Resume Next
If X > myx Then
Shape1.Width = (Sqr((myx - X) ^ 2 + (myy - Y) ^ 2))
Else
Shape1.Width = (Sqr((X - myx) ^ 2 - (myy - Y) ^ 2))
End If
On Error GoTo 0
Shape1.Move myx - Shape1.Width / 2, myy - Shape1.Width / 2, Shape1.Width, Shape1.Width
' mymove = False
Debug.Print "M ", Shape1.Left + Shape1.Width / 2, Shape1.Top + Shape1.Height / 2 'checking center remains constant
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim b As Integer, w As Integer, l As Integer, h As Integer, t As Integer
b = Shape1.BorderWidth
w = Shape1.Width - b * 2
l = Shape1.Left + b
t = Shape1.Top + b
H = Shape1.Height - b * 2 ' should be the same as w
Me.Circle (l + w / 2, t + H / 2), w / 2, vbRed
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
Re: Working with shape controls & drawing shapes
I got it to work through some trial and error. Thanks guys :bigyello:
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
Re: [RESOLVED] Working with shape controls & drawing shapes
Quote:
I got it to work through some trial and error.
i think that is how all code works
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?
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
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..
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