限制鼠标位置 和拖动标签

来源:互联网 发布:生物工程就业前景知乎 编辑:程序博客网 时间:2024/05/09 17:16

最近做项目有这样的要求
在PictureBox中画出坐标系统和曲线,
曲线的旁边要有标注
而且标注可以移动,当然是坐标范围内
以便调整到适当位置打印出来
查了一些资料
现在整理一下
对于移动标签
网上资料很多
主要思想是 MouseDown 的时候判断是否选中控件
MouseMove的时候给鼠标位置变量 赋当前鼠标的位置
MouseUp的时候放弃移动
    Dim whetherSelected As Boolean = False
    Dim mousePoint As New Point

    Private Sub label_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
        whetherSelected = True
        mousePoint.X = e.X
        mousePoint.Y = e.Y
    End Sub

    Private Sub label_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
        whetherSelected = False
    End Sub


    Private Sub label_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label5.MouseMove
        Dim label As Label
        label = CType(sender, Label)

        If (whetherSelected = True) Then
            mousePoint.X = e.X
            mousePoint.Y = e.Y
            Dim p As Point
            p = Control.MousePosition
            p.Offset(e.X - mousePoint.X, e.Y - mousePoint.Y)
            Dim c As Control
            c = sender
            c.Location = c.Parent.PointToClient(p)
        End If
VB版的在这边
http://www.china-askpro.com/msg22/qa48.shtml

对于限制鼠标位置
网上很多用API 的ClipCursor
但是.net 本身就有很方便的方法
参考http://msdn2.microsoft.com/en-us/library/system.windows.forms.cursor.clip.aspx
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2312551&SiteID=1

原创粉丝点击