容器內控制項滑鼠右鍵按著不放,控制項位移的作法


原來位置

拖拉後位移


 Dim RecInitPos As MouseEventArgs = Nothing
    Private Sub Form1_Load(ByVal s As Object, ByVal e As EventArgs) Handles MyBase.Load
        '容器內控制項事件
        For Each objCtrl As Control In Panel1.Controls
            AddHandler objCtrl.MouseMove, AddressOf objCtrl_MouseMove
            AddHandler objCtrl.MouseDown, AddressOf objCtrl_MouseDown
        Next
    End Sub
    Private Sub objCtrl_MouseMove(ByVal s As Object, ByVal e As MouseEventArgs)
        'Ref. CType: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-TW&k=k(VB.CTYPE)&rd=true
        Dim NewCont As Control = CType(s, Control)
        With NewCont
            'Ref. Capture: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-TW&k=k(SYSTEM.WINDOWS.FORMS.CONTROL.CAPTURE)&rd=true
            If .Capture Then
                '控制項位移
                .Location = New Point(e.X + .Location.X - RecInitPos.X, e.Y + .Location.Y - RecInitPos.Y)
            End If
        End With
    End Sub
    Private Sub objCtrl_MouseDown(ByVal s As Object, ByVal e As MouseEventArgs)
        RecInitPos = e '滑鼠起始位置
    End Sub

沒有留言:

張貼留言