通过拖动鼠标滚动图像

发布于 2024-11-01 16:44:28 字数 135 浏览 0 评论 0原文

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

I have the image that scroll when i used the horizontal and vertical scroll bars. But I want to scroll the image by dragging it like in Photoshop (using hand tool and exploring through the zoomed image). Is there any way to do in such way in Visual Basic 6.0? I have changed the default cursor of the mouse to the hand cursor. Now i just want to scroll by dragging the image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2024-11-08 16:44:28

很简单,您只需处理包含图像的控件的鼠标事件。我将使用我编写的应用程序中的生产代码逐步引导您完成此完全相同的功能。

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 事件,在其中进行实际的拖动(在图片框中移动图像)。在示例中,我选择简单地在容器窗体上移动整个图片框控件,而不是移动图片框内的图像。您可能需要更改此处的逻辑,具体取决于表单的布局和您的特定需求。例如,您说您有滚动条,在这种情况下,您需要在此处调整 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

看起来像这样,类似于您在 Adob​​e Acrobat 或 Mac OS 9 中找到的内容(可能最初是由像 Susan Kare 这样的神奇人绘制的;可能不属于公共领域):

光标 ;  

Simple, you just need to handle the mouse events for the control that contains your image. I'll walk you through step-by-step using production code from an app I wrote that implements this exact same feature.

Start with the MouseDown event. Here, you'll need to check which button is down (if you want to allow dragging with only the left button, both the left and right buttons, or just the right button), change the mouse cursor to a closed or clapsed hand (indicating that a drag is in progress), and set some member variables that keep track of the starting coordinates of the cursor. Example:

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

Then, you'll handle the MouseMove event, where you'll do the actual dragging (moving the image inside the picture box). In the example, I chose to simply move the entire picture box control around on the container Form, rather than moving the image inside of the picture box. You might need to change the logic here, depending on the layout of your form and your specific needs. For example, you say that you have scroll bars—in that case, you'll need to adjust the position of the X and Y scroll bars here.

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

And finally, you need to handle the MouseUp event, where you'll end the drag by resetting the cursor:

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

And of course, you'll need to add those member variables to the top of your Form class that keep track of the previous position of the cursor (in x and y coordinates). Something as simple as this will do:

Private xpos As Long
Private ypos As Long

The cursors looked something like this, similar to what you'd find in Adobe Acrobat or Mac OS 9 (probably originally drawn by someone magical like Susan Kare; may not be in the public domain):

   

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文