十万火急!!! VB 如何上下或左右循环移动 和利用键盘的方向键控制Shape移动
如何让Shape上下或左右循环移动和利用键盘的方向键控制Shape移动键盘的方向键包括(左上左下右上右下)会是45度的知道的话请帮帮忙...
如何让Shape上下或左右循环移动 和利用键盘的方向键控制Shape移动
键盘的方向键 包括(左上 左下 右上 右下)会是45度的
知道的话 请帮帮忙 展开
键盘的方向键 包括(左上 左下 右上 右下)会是45度的
知道的话 请帮帮忙 展开
3个回答
展开全部
'37 ←
'38 ↑
'39 →
'40 ↓
Const Num = 10 '每次移动的数值
Dim z As Boolean, s As Boolean, y As Boolean, x As Boolean '左上右下键的情况
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37: z = True '←
Case 38: s = True '↑
Case 39: y = True '→
Case 40: x = True '↓
End Select
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37: z = False '←
Case 38: s = False '↑
Case 39: y = False '→
Case 40: x = False '↓
End Select
End Sub
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 50
End Sub
Private Sub Timer1_Timer()
If z = True Or s = True Or y = True Or x = True Then '如果有一个按下
If z = True And s = False And y = False And x = False Then Shape1.Left = Shape1.Left - Num '左
If z = False And s = True And y = False And x = False Then Shape1.Top = Shape1.Top - Num '上
If z = False And s = False And y = True And x = False Then Shape1.Left = Shape1.Left + Num '右
If z = False And s = False And y = False And x = True Then Shape1.Top = Shape1.Top + Num '下
If z = True And s = True And y = False And x = False Then Shape1.Left = Shape1.Left - Num: Shape1.Top = Shape1.Top - Num '左上
If z = True And s = False And y = False And x = True Then Shape1.Left = Shape1.Left - Num: Shape1.Top = Shape1.Top + Num '左下
If z = False And s = True And y = True And x = False Then Shape1.Left = Shape1.Left + Num: Shape1.Top = Shape1.Top - Num '右上
If z = False And s = False And y = True And x = True Then Shape1.Left = Shape1.Left + Num: Shape1.Top = Shape1.Top + Num '右下
End If
End Sub
'38 ↑
'39 →
'40 ↓
Const Num = 10 '每次移动的数值
Dim z As Boolean, s As Boolean, y As Boolean, x As Boolean '左上右下键的情况
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37: z = True '←
Case 38: s = True '↑
Case 39: y = True '→
Case 40: x = True '↓
End Select
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37: z = False '←
Case 38: s = False '↑
Case 39: y = False '→
Case 40: x = False '↓
End Select
End Sub
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 50
End Sub
Private Sub Timer1_Timer()
If z = True Or s = True Or y = True Or x = True Then '如果有一个按下
If z = True And s = False And y = False And x = False Then Shape1.Left = Shape1.Left - Num '左
If z = False And s = True And y = False And x = False Then Shape1.Top = Shape1.Top - Num '上
If z = False And s = False And y = True And x = False Then Shape1.Left = Shape1.Left + Num '右
If z = False And s = False And y = False And x = True Then Shape1.Top = Shape1.Top + Num '下
If z = True And s = True And y = False And x = False Then Shape1.Left = Shape1.Left - Num: Shape1.Top = Shape1.Top - Num '左上
If z = True And s = False And y = False And x = True Then Shape1.Left = Shape1.Left - Num: Shape1.Top = Shape1.Top + Num '左下
If z = False And s = True And y = True And x = False Then Shape1.Left = Shape1.Left + Num: Shape1.Top = Shape1.Top - Num '右上
If z = False And s = False And y = True And x = True Then Shape1.Left = Shape1.Left + Num: Shape1.Top = Shape1.Top + Num '右下
End If
End Sub
展开全部
Dim Counter As Integer '移动计数
Const moveCounter As Integer = 30 '总移动次数
Dim Wsteps As Long '横向步长
Dim Hsteps As Long '纵向步长
Private Sub Form_Resize() '只要变更窗体大小,就开始一次移动过程
tmrTest.Interval = 50
tmrTest.Enabled = True
shpBall.Move 0, Me.ScaleHeight - shpBall.Height '移动shp到窗体左下角,做初始准备
Wsteps = Int((Me.ScaleWidth - shpBall.Width) / moveCounter) '计算步长
Hsteps = -Int((Me.ScaleHeight - shpBall.Height) / moveCounter)
End Sub
Private Sub tmrTest_Timer()
Counter = Counter + 1 '计数工作
shpBall.Move 0 + Counter * Wsteps, Me.ScaleHeight - shpBall.Height + Counter * Hsteps '按步长和计数开始移动
If Counter = moveCounter Then '如果到总次数位置,就结束Timer动作
tmrTest.Enabled = False
Counter = 0
shpBall.Move Me.ScaleWidth - shpBall.Width, 0 '这样做,是避免最后步长有零头,造成shp没有真正的贴到窗体的右上
End If
End Sub
Const moveCounter As Integer = 30 '总移动次数
Dim Wsteps As Long '横向步长
Dim Hsteps As Long '纵向步长
Private Sub Form_Resize() '只要变更窗体大小,就开始一次移动过程
tmrTest.Interval = 50
tmrTest.Enabled = True
shpBall.Move 0, Me.ScaleHeight - shpBall.Height '移动shp到窗体左下角,做初始准备
Wsteps = Int((Me.ScaleWidth - shpBall.Width) / moveCounter) '计算步长
Hsteps = -Int((Me.ScaleHeight - shpBall.Height) / moveCounter)
End Sub
Private Sub tmrTest_Timer()
Counter = Counter + 1 '计数工作
shpBall.Move 0 + Counter * Wsteps, Me.ScaleHeight - shpBall.Height + Counter * Hsteps '按步长和计数开始移动
If Counter = moveCounter Then '如果到总次数位置,就结束Timer动作
tmrTest.Enabled = False
Counter = 0
shpBall.Move Me.ScaleWidth - shpBall.Width, 0 '这样做,是避免最后步长有零头,造成shp没有真正的贴到窗体的右上
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
回答者: yfdyh000 - 高级魔法师 六级 4-18 07:57
回答者: kanvic119 - 试用期 一级 4-15 22:07
以上两个都没有成功.不小得是不是我的VB有问题!
回答者: kanvic119 - 试用期 一级 4-15 22:07
以上两个都没有成功.不小得是不是我的VB有问题!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询