Option Explicit
Dim centerx As Integer
Dim centery As Integer
Dim orix As Integer
Dim oriy As Integer
Dim radious As Double
Dim oldx As Integer
Dim oldy As Integer
Dim status As IntegerPrivate Sub Command1_Click()
status = 3
End SubPrivate Sub Command2_Click()
status = 0
End SubPrivate Sub Form_Load()
Me.AutoRedraw = True
status = 0
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If status = 3 Then
If IsCircleCenter(x, y) Then 'Move the circle
orix = x
oriy = y
status = 4
End If
If IsOnCircleEdge(x, y) Then 'modify the radious
orix = x
oriy = y
status = 5
End If
End If
If status = 0 Then
centerx = x
centery = y
status = 1
Screen.MousePointer = vbSizeAll
oldx = x
oldy = y
Me.ForeColor = vbRed
Me.DrawMode = 7
Line (centerx, centery)-(oldx, oldy)
End If
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If status = 1 Then
Me.DrawMode = 7
Me.ForeColor = vbRed
Line (centerx, centery)-(oldx, oldy)
Line (centerx, centery)-(x, y)
oldx = x
oldy = y
End If
If status = 3 Then
If IsOnCircleEdge(x, y) Or IsCircleCenter(x, y) Then
Screen.MousePointer = vbSizeAll
Else
Screen.MousePointer = vbDefault
End If
End If
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If status = 1 Then ' decide the center
radious = Math.Sqr((x - centerx) * (x - centerx) + (y - centery) * (y - centery))
Line (centerx, centery)-(oldx, oldy)
Call drawcircle(centerx, centery, radious)
Screen.MousePointer = vbDefault
status = 0
ElseIf status = 4 Then
If IsCircleCenter(x, y) Then
status = 3
Else
Call drawcircle(centerx, centery, radious)
centerx = centerx + x - orix
centery = centery + y - oriy
Call drawcircle(centerx, centery, radious)
status = 3
End If
ElseIf status = 5 Then
If IsOnCircleEdge(x, y) Then
status = 3
Else
Call drawcircle(centerx, centery, radious)
radious = Math.Sqr((x - centerx) * (x - centerx) + (y - centery) * (y - centery))
Call drawcircle(centerx, centery, radious)
status = 3
End If
End If
End Sub
Sub drawcircle(x As Integer, y As Integer, r As Double)
Dim y0 As Integer
y0 = r
Me.ForeColor = vbRed
Me.DrawMode = vbXorPen
Me.Circle (x, y), y0, vbRed
Me.Line (x, y)-(x, y)
End Sub
Function IsCircleCenter(x As Single, y As Single) As Boolean
Dim dx As Integer
Dim dy As Integer
IsCircleCenter = False
dx = Math.Abs(x - CurrentX)
dy = Math.Abs(y - CurrentY)
If (dx + dy) < 100 Then IsCircleCenter = True
End Function
Function IsOnCircleEdge(x As Single, y As Single) As Boolean
Dim dx As Single
Dim dy As Single
Dim dr As Integer
IsOnCircleEdge = False
dx = Math.Abs(Math.Abs(x - centerx))
dy = Math.Abs(Math.Abs(y - centery))
If Math.Sqr(Math.Abs((dx * dx + dy * dy - radious * radious))) < 400 Then IsOnCircleEdge = TrueEnd Function
1. Create a vb project
2. Add two buttons to the form,
3. Paste the above code to the code editor window
4. You can run it
Click command1 button will change to modify state (means you can move and resize the circle)
click command2 button can change to draw state (means you can draw a new circle)
5 YOu can use your mouse to resize the circle( on the edge of circle) or move the circle ( on the center point of the circle)
Because I only saved the last circle painted, so you only can resize and move the latest circle you draw.
Dim centerx As Integer
Dim centery As Integer
Dim orix As Integer
Dim oriy As Integer
Dim radious As Double
Dim oldx As Integer
Dim oldy As Integer
Dim status As IntegerPrivate Sub Command1_Click()
status = 3
End SubPrivate Sub Command2_Click()
status = 0
End SubPrivate Sub Form_Load()
Me.AutoRedraw = True
status = 0
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If status = 3 Then
If IsCircleCenter(x, y) Then 'Move the circle
orix = x
oriy = y
status = 4
End If
If IsOnCircleEdge(x, y) Then 'modify the radious
orix = x
oriy = y
status = 5
End If
End If
If status = 0 Then
centerx = x
centery = y
status = 1
Screen.MousePointer = vbSizeAll
oldx = x
oldy = y
Me.ForeColor = vbRed
Me.DrawMode = 7
Line (centerx, centery)-(oldx, oldy)
End If
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If status = 1 Then
Me.DrawMode = 7
Me.ForeColor = vbRed
Line (centerx, centery)-(oldx, oldy)
Line (centerx, centery)-(x, y)
oldx = x
oldy = y
End If
If status = 3 Then
If IsOnCircleEdge(x, y) Or IsCircleCenter(x, y) Then
Screen.MousePointer = vbSizeAll
Else
Screen.MousePointer = vbDefault
End If
End If
End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If status = 1 Then ' decide the center
radious = Math.Sqr((x - centerx) * (x - centerx) + (y - centery) * (y - centery))
Line (centerx, centery)-(oldx, oldy)
Call drawcircle(centerx, centery, radious)
Screen.MousePointer = vbDefault
status = 0
ElseIf status = 4 Then
If IsCircleCenter(x, y) Then
status = 3
Else
Call drawcircle(centerx, centery, radious)
centerx = centerx + x - orix
centery = centery + y - oriy
Call drawcircle(centerx, centery, radious)
status = 3
End If
ElseIf status = 5 Then
If IsOnCircleEdge(x, y) Then
status = 3
Else
Call drawcircle(centerx, centery, radious)
radious = Math.Sqr((x - centerx) * (x - centerx) + (y - centery) * (y - centery))
Call drawcircle(centerx, centery, radious)
status = 3
End If
End If
End Sub
Sub drawcircle(x As Integer, y As Integer, r As Double)
Dim y0 As Integer
y0 = r
Me.ForeColor = vbRed
Me.DrawMode = vbXorPen
Me.Circle (x, y), y0, vbRed
Me.Line (x, y)-(x, y)
End Sub
Function IsCircleCenter(x As Single, y As Single) As Boolean
Dim dx As Integer
Dim dy As Integer
IsCircleCenter = False
dx = Math.Abs(x - CurrentX)
dy = Math.Abs(y - CurrentY)
If (dx + dy) < 100 Then IsCircleCenter = True
End Function
Function IsOnCircleEdge(x As Single, y As Single) As Boolean
Dim dx As Single
Dim dy As Single
Dim dr As Integer
IsOnCircleEdge = False
dx = Math.Abs(Math.Abs(x - centerx))
dy = Math.Abs(Math.Abs(y - centery))
If Math.Sqr(Math.Abs((dx * dx + dy * dy - radious * radious))) < 400 Then IsOnCircleEdge = TrueEnd Function
1. Create a vb project
2. Add two buttons to the form,
3. Paste the above code to the code editor window
4. You can run it
Click command1 button will change to modify state (means you can move and resize the circle)
click command2 button can change to draw state (means you can draw a new circle)
5 YOu can use your mouse to resize the circle( on the edge of circle) or move the circle ( on the center point of the circle)
Because I only saved the last circle painted, so you only can resize and move the latest circle you draw.
作一个行列三维矩阵,将PictureBox划分为 Row * Col 块区域在绘制图形时在其涉及到的区域里放上图形对象的索引当单击事件发生时,首先找出单击的区域,在对这个区域里的对象进行遍历,这样可以缩小搜索范围,提高效率。