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.

解决方案 »

  1.   

    谢谢楼上!我的信箱是[email protected],麻烦您们在主题中写入123,我就明白是csdn的朋友帮忙了!
      

  2.   

    呵呵,他的问题难点在,如何在picturebox中选中已经画好的圆。在我们的理解上可把那个圆认定为一个对象,但是在实际操作的时候呢?鼠标去点选的时候,结果只是在PictureBox里面单击而已。虽然可以PictureBox的MouseDown里判断鼠标位置是否在圆内,但这样是不是要对PictureBox上画的所有Shape遍历一次?对于点中两个(甚至多个)Shape相交的部分呢?
      

  3.   


    作一个行列三维矩阵,将PictureBox划分为 Row * Col 块区域在绘制图形时在其涉及到的区域里放上图形对象的索引当单击事件发生时,首先找出单击的区域,在对这个区域里的对象进行遍历,这样可以缩小搜索范围,提高效率。
      

  4.   

    前几天发高烧,所以被迫休息了几天,回来看到大家的留言,很是感谢!不过大家好像误会了我的意思,用oop的想法画圆,处理圆也是可以的.我是这样想,图形是屏幕上单个点组成的,我觉得定义一个有显示,删除,和移动功能的point类,再由这个类派生出一个circle类,把point中的成员作为圆心.就能实现功能了,但是vb中怎样写这个类,我实在没有遇到过,不知道vb中这样行不行,希望大家能给我一个简单的例子,我是一个新手,正在努力中,见笑了各位.