vb中,在picturebox控件中画圆的代码
要求,1、鼠标左键按下时的位置为圆心,拖拉的距离为半径画圆。2、鼠标可以选择此图形,并可以移动此图。...
要求,1、鼠标左键按下时的位置为圆心,拖拉的距离为半径画圆。
2、鼠标可以选择此图形,并可以移动此图。 展开
2、鼠标可以选择此图形,并可以移动此图。 展开
1个回答
展开全部
Option Explicit
Dim Dx, Dy As Integer 'mousedown
Dim Cx, Cy As Integer 'center of circle
Dim MouseDown As Boolean 'mouse is down
Dim isExists As Boolean 'circle exists
Dim Cr As Single 'radius
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If vbLeftButton = Button Then
MouseDown = True
Dx = X
Dy = Y
Else
'clear circle
Picture1.Cls
isExists = False
End If
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not MouseDown Then Exit Sub
If isExists Then
If Sqr((Abs(X - Cx)) ^ 2 + (Abs(Y - Cy)) ^ 2) > Cr Then
Exit Sub 'out of area
End If
'move circle
Dim Mx, My As Integer
Mx = X - Dx
My = Y - Dy
Cx = Cx + Mx
Cy = Cy + My
Dx = X
Dy = Y
Picture1.Cls
'draw new circle
Picture1.Circle (Cx, Cy), Cr
Else
Picture1.Cls
'draw new circle
Cr = Sqr((Abs(X - Dx)) ^ 2 + (Abs(Y - Dy)) ^ 2)
Picture1.Circle (Dx, Dy), Cr
Cx = Dx 'center
Cy = Dy
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If vbLeftButton = Button Then
'circle is ok
MouseDown = False
isExists = True
End If
End Sub
Dim Dx, Dy As Integer 'mousedown
Dim Cx, Cy As Integer 'center of circle
Dim MouseDown As Boolean 'mouse is down
Dim isExists As Boolean 'circle exists
Dim Cr As Single 'radius
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If vbLeftButton = Button Then
MouseDown = True
Dx = X
Dy = Y
Else
'clear circle
Picture1.Cls
isExists = False
End If
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not MouseDown Then Exit Sub
If isExists Then
If Sqr((Abs(X - Cx)) ^ 2 + (Abs(Y - Cy)) ^ 2) > Cr Then
Exit Sub 'out of area
End If
'move circle
Dim Mx, My As Integer
Mx = X - Dx
My = Y - Dy
Cx = Cx + Mx
Cy = Cy + My
Dx = X
Dy = Y
Picture1.Cls
'draw new circle
Picture1.Circle (Cx, Cy), Cr
Else
Picture1.Cls
'draw new circle
Cr = Sqr((Abs(X - Dx)) ^ 2 + (Abs(Y - Dy)) ^ 2)
Picture1.Circle (Dx, Dy), Cr
Cx = Dx 'center
Cy = Dy
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If vbLeftButton = Button Then
'circle is ok
MouseDown = False
isExists = True
End If
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询