需要使用函数嵌套完成你的要求,但vb堆栈空间比较小,嵌套不到6000次后会溢出,因此运行下面代码你要画个半径比较小的圆可完成你的要求,半径一大就溢出出错了。
Const vec = "-1,0,1,0,0,-1,0,1"
Dim a As Single
Dim b As Single
Dim r As Single
'增加一个半径暂存变量
Dim f As Boolean
'增加一个状态变量,false:画圆 true:填充
Dim d(3, 1) As Integer
'步进矢量,从常量vec中获取,用于判断指点周围颜色,共4个方向,即左右上下
Private Sub fillPic(X As Single, Y As Single, C As Long, FC As Long)
'填充图形,这是一个函数嵌套调用
Dim i As Integer, x1 As Single, y1 As Single
If picdraw.Point(X, Y) = C Then
picdraw.PSet (X, Y), FC
For i = 0 To 3
x1 = X + d(i, 0) * 15
y1 = Y + d(i, 1) * 15
fillPic x1, y1, C, FC
Next
End If
End Sub
Private Sub picdraw_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim C1 As Long, C2 As Long, r1 As Single, q() As String, i As Integer
If Button = 1 Then
If Not f Then
'处于画圆状态,记下圆心坐标
a = X
b = Y
Else
'处于填充状态
r1 = Sqr((X - a) ^ 2 + (Y - b) ^ 2)
'计算到圆心的距离
If r1 > r - 15 Then Exit Sub
'如果填充点击的点位置超过圆半径则不执行填充操作
C1 = picdraw.Point(X, Y)
'取鼠标点击位置的颜色
C2 = C1 Xor &HFFFFFF
q = Split(vec, ",")
For i = 0 To 3
'获取4个方向步进矢量
d(i, 0) = Val(q(i * 2))
d(i, 1) = Val(q(i * 2 + 1))
Next
fillPic X, Y, C1, vbRed
End If
End If
End Sub
Private Sub picdraw_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim i As Single
If Button = 1 Then
If Not f Then
'处于画圆状态
picdraw.Cls
r = Sqr((X - a) ^ 2 + (Y - b) ^ 2)
'计算半径
picdraw.Circle (a, b), r, vbBlack
End If
End If
End Sub
Private Sub picdraw_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then f = Not f
End Sub