注册 登录
编程论坛 VB6论坛

返回初始位置

没得选择 发布于 2014-04-17 12:03, 789 次点击
Dim startright As String
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 1
End Sub

Private Sub Form_Load()
Timer1.Enabled = False
startright = Text1.Left
End Sub

Private Sub HScroll1_Change()
Timer1.Interval = 10 * HScroll1.Value / 10
End Sub

Private Sub Timer1_Timer()
Text1.Left = Text1.Left + 20
If Text1.Left < 0 Then
Text1.Left = startright
End If
End Sub
问一下,这个代码为什么用“Text1.Left = Text1.Left - 20
”就能实现Text1.Left = startright,,而Text1.Left = Text1.Left + 20,就不能实现text1.left=startright
我做的是一个自己会移动的text1。在这里,先谢谢了!
11 回复
#2
lowxiong2014-04-17 12:41
关键是“If Text1.Left < 0 Then”这一句,你认为如果不断地Text1.Left = Text1.Left + 20,Text1.Left的值还有可能小于0吗?如果Text1.Left永远不能小于0,那么Text1.Left 就永远不能等于startright了。
#3
没得选择2014-04-17 15:22
回复 2 楼 lowxiong
只想着语法出问题,没想过这个,你这样一说就明白了。十分感谢!
#4
没得选择2014-04-17 15:25
回复 2 楼 lowxiong
那要怎么改,才能text1.left=startleft
#5
wp2319572014-04-17 15:49
这个好弄吧  左侧 你想让他移动到哪里终止  右侧 你想让他移动到哪里终止  根据坐标自己换算吧
#6
没得选择2014-04-17 17:05
回复 5 楼 wp231957
我要他从初始位置移动到右侧,然后从右侧移动到初始位置,这样一直循环。要做到这样的效果,代码要怎么写?
#7
lowxiong2014-04-17 17:20
If Text1.Left > me.width Then
#8
没得选择2014-04-17 17:55
回复 7 楼 lowxiong
我想要text1从初始位置移到到右侧,再从右侧移动到初始位置,一直循环,又要加什么代码?
#9
owenlu19812014-04-18 22:21
移动前加判断
Dim LeftToRight as Boolean    '由左向右移

Private Sub Form_Load()
Timer1.Enabled = False
LeftToRight = True
End Sub

Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 1

End Sub

Private Sub Timer1_Timer()
If LeftToRight = True then
    If Text1.Left = Me.Width-Text1.Width then    'Text1的右边和窗体右边对齐
        Text1.Left = Text1.Left - 20    'Text1左移
        LeftToRight = False    ’左移
    ElseIf Text1.Left + 20 >= Me.Width-Text1.Width then    'Text1右移后的右边和窗体右边对齐
        Text1.Left = Me.Width-Text1.Width
        LeftToRight = False
    Else
        Text1.Left = Text1.Left + 20
    endif
else
    If Text1.Left = 0 then
        Text1.Left = Text1.Left + 20
        LeftToRight = True
    ElseIf Text1.Left - 20 <= 0 then
        Text1.Left = 0
        LeftToRight = True
    Else
        Text1.Left = Text1.Left - 20
    Endif
Endif

End Sub
#10
lowxiong2014-04-18 23:10
'代码这样不是更简单?还可以暂停,把步进值设为30,是因为在图形单位为緹时,30是2个像素,可以平滑移动
Dim mF As Integer, startPos As Integer
Private Sub Form_Load()
  mF = 30
  startPos = Text1.Left                                                 '初始位置
  Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
  Timer1.Enabled = Timer1.Enabled Xor True                              '暂停或继续
  Timer1.Interval = 1
End Sub

Private Sub Timer1_Timer()
  If Text1.Left >= Me.ScaleWidth - Text1.Width Then mF = -30            '达右边界立即左移
  If Text1.Left < startPos Then mF = 30                                 '达初始位置则右移
  Text1.Left = Text1.Left + mF
End Sub


[ 本帖最后由 lowxiong 于 2014-4-18 23:44 编辑 ]
#11
风吹过b2014-04-19 10:15
刚看到 这贴 。

10 楼的代码的确是简单,我想到的也就是这种更改 变化量的 方式。
这种代码简单,理解起来也比较容易,最大特点是 代码执行速度方面优化很好,速度。
#12
鸥翔鱼游2014-04-28 14:18
且学且珍惜,得认学习真正消化
1