你这样写有问题,比如宽1300,你判断不小于大于1200,不处理,然而又不大于3000,当然也不处理,就不变了,你其实只要先把它变方的,然后判断一个就行了。按你上面的大小1200-3000写了一个,测试通过。
Dim i As Integer
Private Sub Command1_Click()
If Command1.Width >= 3000 Or Command1.Width <= 1200 Then
If Command1.Width >= 3000 Then i = 1
If Command1.Width <= 1200 Then i = 0
End If
If i = 0 Then
Command1.Width = Command1.Width + 100
Command1.Height = Command1.Height + 100
Else
Command1.Width = Command1.Width - 100
Command1.Height = Command1.Height - 100
End If
Me.Caption = Command1.Width
End Sub
Private Sub Form_Load()
Command1.Width = 500
Command1.Height = 500
i = 0
End Sub
Private Sub Command1_Click()
Static fs As Boolean
Dim w As Integer, h As Integer
w = Command1.Width + IIf(fs, -100, 100)
h = Command1.Height + IIf(fs, -100, 100)
If fs = False Then
If w < Me.Width And h < Me.Height Then
Command1.Move (Me.Width - w) / 2, (Me.Height - h) / 2, w, h
Else
fs = Not fs
End If
Else
If w > 100 And h > 100 Then
Command1.Move (Me.Width - w) / 2, (Me.Height - h) / 2, w, h
Else
fs = Not fs
End If
End If
End sub
题主的代码忽略了一个问题,就是当按钮的尺寸在你要求的范围之内的时候,如何判断是正在变大,还是正在变小。所以加入一个符号参数K基本上就能完成要求了
Dim k
Private Sub Command1_Click()
If Command1.Height > 1200 And Command1.Height < 3000 Then
Command1.Height = Command1.Height + k * 100
Command1.Width = Command1.Height
Else
k = k * (-1)
Command1.Height = Command1.Height + k * 100
Command1.Width = Command1.Height
End If
End Sub
Private Sub Form_Load()
k = -1
Command1.Height = 1200
Command1.Width = 1200
End Sub
Private Sub Command1_Click()
Static n As Integer
If Command1.Width <= n Or Command1.Height <= n Then
Command1.Width = Command1.Width + 100
Command1.Height = Command1.Height + 100
If Command1.Width >= 3000 Or Command1.Height >= 3000 Then n = 1000
Else
Command1.Width = Command1.Width - 100
Command1.Height = Command1.Height - 100
If Command1.Width <= 1000 Or Command1.Height <= 1000 Then n = 3000
End If
End Sub