通过拖动鼠标滚动图像

时间:2011-04-15 06:28:42

标签: vb6 draggable mouseevent picturebox

当我使用水平和垂直滚动条时,我有滚动的图像。但我想像在Photoshop中一样拖动图像(使用手工工具并通过缩放图像进行探索)。在Visual Basic 6.0中有没有办法这样做?我已将鼠标的默认光标更改为手形光标。现在我只想通过拖动图像滚动。

1 个答案:

答案 0 :(得分:8)

很简单,您只需要处理包含图像的控件的鼠标事件。我会一步一步地使用我编写的应用程序中的生产代码来实现这一功能。

MouseDown事件开始。在这里,您需要检查哪个按钮处于关闭状态(如果您只允许使用左按钮,左右按钮或右按钮进行拖动),请将鼠标光标更改为关闭或折叠的手(指示拖动正在进行中),并设置一些跟踪光标起始坐标的成员变量。例如:

Private Sub picBox_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    ' When left mouse button is pressed down (initiating a drag)
    If Button = 1 Then
        ' Store the coordinates of the mouse cursor
        xpos = x
        ypos = y

        ' Change the cursor to hand grab icon
        picBox.MouseIcon = LoadPicture(App.Path & "\Resources\Cursors\grab.ico")
    End If
End Sub

然后,您将处理MouseMove事件,您将在其中执行实际拖动(在图片框内移动图像)。在这个例子中,我选择简单地在容器Form上移动整个图片框控件,而不是在图片框内移动图像。您可能需要在此处更改逻辑,具体取决于表单的布局和您的特定需求。例如,你说你有滚动条 - 在这种情况下,你需要在这里调整X和Y滚动条的位置。

Private Sub picBox_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    ' When left mouse button is being held down (drag)
    If Button = 1 Then
        ' Drag the picture box around the form
        picBox.Move x + (picBox.Left - xpos), y + (picBox.Top - ypos)
    End If
End Sub

最后,你需要处理MouseUp事件,你将通过重置光标结束拖动:

Private Sub picBox_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    ' Stop normal dragging
    If Button = 1 Then
        ' Set the cursor back to the unclapsed hand
        picBox.MouseIcon = LoadPicture(App.Path & "\Resources\Cursors\hand.ico")
    End If
End Sub

当然,您需要将这些成员变量添加到Form类的顶部,以跟踪光标的上一个位置(在x和y坐标中)。像这样简单的事情会做:

Private xpos As Long
Private ypos As Long

游标看起来像这样,类似于你在Adobe Acrobat或Mac OS 9中找到的(可能最初由像Susan Kare这样神奇的人绘制;可能不属于公共领域):

相关问题