打印本文 打印本文 关闭窗口 关闭窗口
VB.net基础:简单的自定义控件MyPictureBox
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2152  更新时间:2009/4/23 19:00:53  文章录入:mintao  责任编辑:mintao
End Sub

这样的组合就能达到目标了。

然后我们再写“拖动”图片的功能。显然,这个功能只有当ViewMode=TrueSize的时候才有用。这个功能分三部分,首先是MouseDown事件:
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button <> MouseButtons.Left Or ViewMode <> VMode.TrueSize Then Exit Sub
        Moving = True
        Me.Cursor = System.Windows.Forms.Cursors.Hand
        MousePos = Me.MousePosition
        OldScroll = Me.AutoScrollPosition
    End Sub

这里Moving变量用来表示用户是否正在拖动图片,类型是Boolean。没定义?到前面去定义一下不就行了。还有什么MousePos,OldScroll,它们俩的类型是Point。快去定义一下。当用用户按下鼠标左键的时候,记录下鼠标位置和图片当的前滚动位置。

然后是MouseMove事件:

等等。在用户移动鼠标的时候,又要不停的计算图片框的滚动位置。为了避免再次“霸道”起来,我们又要拖进来一个Timer。设置Interval值为1。这个Timer是这样的:
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        MousePosNow = Me.MousePosition
        Dim DeltaPos As Point = New Point((MousePos.X - MousePosNow.X), (MousePos.Y - MousePosNow.Y))
        Me.AutoScrollPosition = New Point(-OldScroll.X + DeltaPos.X, -OldScroll.Y + DeltaPos.Y)
        Timer2.Stop()
    End Sub

就四句话,应该不太难看懂吧。
所以MouseMove就这样:
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If Moving Then
            Timer2.Start()
        End If
    End Sub

第三部分就是MouseUp事件了,就两句话:
    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Moving = False
        Me.Cursor = System.Windows.Forms.Cursors.Default
    End Sub

这样,MyPictureBox就写好了。





……没错,是写好了。运行一下看看。





不能运行?废话。我叫你再建一个WinForm程序,把MyPictureBox拖进去看看!

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口