注册 登录
编程论坛 VB6论坛

鼠标经过图片产生缩放变化

sclx88 发布于 2016-09-06 14:12, 1867 次点击
编译错误,而且我觉得这个代码是不是太繁杂了,有没有简化一点的,如果我有很多个图片框都需要这种效果,要怎么搞
程序代码:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Dim i

    If (X >= Picture1.Left) And (X <= Picture1.Left + Picture1.Width) And _
        (Y >= Picture1.Top) And (Y <= Picture1.Top + Picture1.Height) Then _
        Picture1.Height = 5628
        Picture1.Left = 0
        Picture1.Top = 0
        Picture1.Width = 8376
      
    ElseIf (X < Picture1.Left) And (X > Picture1.Left + Picture1.Width) And _
       (Y < Picture1.Top) And (Y > Picture1.Top + Picture1.Height) Then _
        Picture1.Left = Picture1.Left - 300
        Picture1.Top = Picture1.Top - 150
        Picture1.Width = Picture1.Width + 600
        Picture1.Height = Picture1.Height + 300
    End If
End Sub
7 回复
#2
xiangyue05102016-09-06 16:48
这个代码都复杂? 那你的VB可以不用学了。
初步看下来,没有明显的错误。建议把报错信息提供
#3
sclx882016-09-06 17:35
回复 2楼 xiangyue0510
编译错误
else 没有IF
 ElseIf (X < Picture1.Left) And (X > Picture1.Left + Picture1.Width) And _
       (Y < Picture1.Top) And (Y > Picture1.Top + Picture1.Height) Then _
这句哪里有问题
我看到有大神是这样写的,但是我仿造用不起
程序代码:
Dim s(3) As Single

 
Private Sub Form_Load()
With Image1
    .Stretch = True
    s(0) = .Left
    s(1) = .Top
    s(2) = .Width
    s(3) = .Height
End With
End Sub

 
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Image1
    If .Left <> s(0) Then .Move s(0), s(1), s(2), s(3)
End With
End Sub

 
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Image1
    If .Left = s(0) Then .Move .Left - 300, .Top - 150, .Width + 600, .Height + 300
End With
End Sub


[此贴子已经被作者于2016-9-6 17:40编辑过]

#4
风吹过b2016-09-06 18:02
你把 换行连接符 去掉,并且把下一行拼上去就会发现错误所在了。

with Picture1
    If (X >= .Left) And (X <= .Left + .Width) And _
        (Y >= .Top) And (Y <= .Top + .Height) Then
        .move 0, 0, 8376, 5628      
    ElseIf (X < .Left) And (X > .Left + .Width) And _
       (Y < .Top) And (Y > .Top + .Height) Then
        .move .Left - 300, .Top - 150, .Width + 600, .Height + 300
    End If
end with
#5
sclx882016-09-06 19:13
回复 4楼 风吹过b
问题解决了,但是实际效果点都不好用,鼠标指要晃一下才变,我要的效果是指针放上去放大,移开恢复原样,大神帮我看看下面的代码怎么搞一下,我比较喜欢这种简单明了的
程序代码:
Dim s(3) As Single

 
Private Sub Form_Load()
With Image1
    .Stretch = True
    s(0) = .Left
    s(1) = .Top
    s(2) = .Width
    s(3) = .Height
End With
End Sub

 
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Image1
    If .Left <> s(0) Then .Move s(0), s(1), s(2), s(3)
End With
End Sub

 
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Image1
    If .Left = s(0) Then .Move .Left - 300, .Top - 150, .Width + 600, .Height + 300
End With
End Sub
#6
风吹过b2016-09-07 09:15
试了这些代码,改成 Picture1 也没问题啊。

程序代码:

Option Explicit

Dim s(3) As Single

 
Private Sub Form_Load()
With Picture1
    '.Stretch = True
    s(0) = .Left
    s(1) = .Top
    s(2) = .Width
    s(3) = .Height
End With
End Sub

 
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Picture1
    If .Left <> s(0) Then .Move s(0), s(1), s(2), s(3)          '如果鼠标在窗体上移动时,图像还原大小
End With
End Sub

 
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Picture1
    If .Left = s(0) Then .Move .Left - 300, .Top - 150, .Width + 600, .Height + 300         '如果鼠标在图像内移动时,图像变大
End With
End Sub


如果你有多个 Picture1 ,那就使用控件数组。
同时有一个变量用来标记最后修改大小的那个控件是几号,用于改回去的。
#7
风吹过b2016-09-07 09:27
使用控件数组的办法:
代码里需要指定控件数组有几个元素,看注释。
程序代码:

Option Explicit

Const piccount = 4              '图像个数,0-4 ,共5个
Dim s(piccount, 3) As Single    '保存控件的原始大小数据,与控件数组下标对应
Dim picindex As Integer         '标志最后一个变大的框

Private Sub Form_Load()
Dim i As Long
For i = 0 To piccount
    With Picture1(i)
        '.Stretch = True        '根据图像修改框大小,仅适用于Image控件
        s(i, 0) = .Left
        s(i, 1) = .Top
        s(i, 2) = .Width
        s(i, 3) = .Height
    End With
Next i
End Sub

 
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Picture1(picindex)
    If .Left <> s(picindex, 0) Then .Move s(picindex, 0), s(picindex, 1), s(picindex, 2), s(picindex, 3)     '如果鼠标在窗体上移动时,图像还原大小
End With
End Sub

 
Private Sub Picture1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
With Picture1(Index)
    If .Left = s(Index, 0) Then        
        Picture1(picindex).Move s(picindex, 0), s(picindex, 1), s(picindex, 2), s(picindex, 3)     '前一个变小,防止图像放太近造成的干扰
        .Move .Left - 300, .Top - 150, .Width + 600, .Height + 300         '如果鼠标在图像内移动时,图像变大
        picindex = Index                                                       '保存前一个的下标
    End If
End With
End Sub


手动优化了一下代码的可视性

[此贴子已经被作者于2016-9-7 09:31编辑过]

#8
sclx882016-09-07 16:24
回复 7楼 风吹过b
谢谢大神,果然解决了
1