鼠标按下或基于鼠标按钮更新控制光标

Update Control Cursor on Mouse Down or based on Mouse Button

当鼠标左键 = 鼠标左键时,我无法在鼠标移动事件中更改鼠标光标。

在 'Text Control Coordinates' 文本框的 gif 图像中,您可以看到我正在更新此文本框,其中包含游标应更新到的内容的条目。这是底部框中的最后一个条目。光标设置为 'SizeAll',但是当我使用鼠标左键移动控件时,我有代码将控件光标设置为 'Hand'。文本框显示已正确到达将 Cursor 更新为 'Hand' 的逻辑。问题是光标仅在我释放鼠标左键后更新。它会一直这样做,直到我再次开始移动鼠标并且 MouseMove 事件再次接管。

Private Sub ClsTextObj_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    ' Get object under cursor when user moved mouse - MouseMove
    ' We are only going to perform the WORK if the object is TypeOf clsTextObj
    If TypeOf sender Is clsTextObj Then
        Dim txt_clsText_ctrl_tmp = DirectCast(sender, clsTextObj)

        'Declare Bool to determine if Left Mouse Button is being used
        Dim IsMouseLeftButton = e.Button = Windows.Forms.MouseButtons.Left

        Dim mCursor As Cursor
        Dim MouseCursor = "Logic Never Reached"

        'Set mCursor var to the hand cursor when the LEFT Mouse Button is being used
        If IsMouseLeftButton Then
            MouseCursor = "Hand"
            mCursor = Cursors.Hand
        Else
            MouseCursor = "SizeAll"
            mCursor = Cursors.SizeAll
        End If

        'START Determine Mouse Cursor - Find where the mouse cursor is within the control
        Dim MouseIsInLeftEdge As Boolean
        Dim MouseIsInRightEdge As Boolean
        Dim MouseIsInTopEdge As Boolean
        Dim MouseIsInBottomEdge As Boolean

        MouseIsInLeftEdge = Math.Abs(e.X) <= 9
        MouseIsInRightEdge = Math.Abs(e.X - txt_clsText_ctrl_tmp.Width) <= 9
        MouseIsInTopEdge = Math.Abs(e.Y) <= 9
        MouseIsInBottomEdge = Math.Abs(e.Y - txt_clsText_ctrl_tmp.Height) <= 9

        If MouseIsInLeftEdge Then
            If MouseIsInTopEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNWSE
            ElseIf MouseIsInBottomEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNESW
            Else
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeWE
            End If
        ElseIf MouseIsInRightEdge Then
            If MouseIsInTopEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNESW
            ElseIf MouseIsInBottomEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNWSE
            Else
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeWE
            End If
        ElseIf (MouseIsInTopEdge Or MouseIsInBottomEdge) Then
            txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNS
        Else
            txt_clsText_ctrl_tmp.Appearance.Cursor = mCursor
            'txt_clsText_ctrl_tmp.Appearance.Cursor = Cursor.SizeAll
        End If
        'END Determining Mouse Cursor

        'Capture Mouse Down Clicks while moving mouse cursor.  Mouse Move overrides Mouse Down basically and so we capture mouse down buttons while moving here
        'If e.Button = Windows.Forms.MouseButtons.Left Then
        If IsMouseLeftButton Then
            txt_clsText_ctrl.Appearance.Cursor = Cursors.Hand
            txt_clsText_ctrl.Location = New Point(txt_clsText_ctrl.Location.X + (e.X - initialClickLocation.X), txt_clsText_ctrl.Location.Y + (e.Y - initialClickLocation.Y))
        End If

        'Output mouse details
        ClsTextObj1.Value = "X: " & txt_clsText_ctrl_tmp.Location.X + e.X _
                          & ",Y: " & txt_clsText_ctrl_tmp.Location.Y + e.Y

        'Build User output info
        builder.Clear()
        builder.Append("Control Name: " & txt_clsText_ctrl_tmp.Name).AppendLine()
        builder.Append("e.X " & e.X & ", e.Y: " & e.Y).AppendLine()
        builder.Append("e.Point Location: " & e.Location.ToString()).AppendLine()
        builder.Append("txt_clsText_ctrl Point Location: " & txt_clsText_ctrl_tmp.Location.ToString()).AppendLine()
        builder.Append("Form Location: {X=" & txt_clsText_ctrl_tmp.Location.X + e.X)
        builder.Append(",Y=" & txt_clsText_ctrl_tmp.Location.Y + e.Y & "}").AppendLine()
        builder.Append("initial Ctrl Location: " & initialCtrlLocation.ToString()).AppendLine()
        builder.Append("initial Click Location: " & initialClickLocation.ToString()).AppendLine()
        builder.Append("Calc Point: " & "X: " & (e.X - initialClickLocation.X))
        builder.Append(",Y: " & (e.Y - initialClickLocation.Y)).AppendLine()
        builder.Append("New Label Pos: " & "X: " & txt_clsText_ctrl_tmp.Location.X + (e.X - initialClickLocation.X))
        builder.Append(",Y: " & txt_clsText_ctrl_tmp.Location.Y + (e.Y - initialClickLocation.Y)).AppendLine()
        builder.Append("Mouse Button: " & e.Button.ToString()).AppendLine()
        builder.Append("Left Edge: " & MouseIsInLeftEdge).AppendLine()
        builder.Append("Right Edge: " & MouseIsInRightEdge).AppendLine()
        builder.Append("Top Edge: " & MouseIsInTopEdge).AppendLine()
        builder.Append("Bottom Edge: " & MouseIsInBottomEdge).AppendLine()
        builder.Append("Cursor: " & appendMouseCursor)

        ClsTextObj2.Value = builder.ToString()

    End If
End Sub

我在这里错过了什么?在使用鼠标左键时,我可以成功更新应用程序的其他部分,但鼠标光标除外。我什至尝试在控件上使用 Refresh() 和其他一些愚蠢的想法,这些想法似乎从来没有让光标在控件移动时变成一只手。

您甚至可以看到我正在从 MouseMove Sub 中根据控件的边缘成功更新鼠标光标。但是,当按下鼠标左键时,就好像鼠标光标的更新被暂停,直到我松开按钮。

Update 10.19.18: I used the Form Cursor instead:

If IsMouseLeftButton And IsMouseDown Then
        Me.Cursor = Cursors.Hand
        'txt_clsText_ctrl.Appearance.Cursor = Cursors.Hand
        txt_clsText_ctrl.Location = New Point(txt_clsText_ctrl.Location.X + (e.X - initialClickLocation.X), txt_clsText_ctrl.Location.Y + (e.Y - initialClickLocation.Y))
End If

由于我使用的是表单光标,所以我必须确保将其更新回默认值。我发现 MouseUp 对此工作得很好。至少我还没有遇到任何错误。

Private Sub ClsTextObj_MouseUp(sender As Object, e As MouseEventArgs)
    If TypeOf sender Is STORE.PDFBuilder.clsTextObj Then
        Dim txt_clsText_ctrl_tmp = DirectCast(sender, STORE.PDFBuilder.clsTextObj)

        IsMouseDown = False
        Me.Cursor = Cursors.Default

    End If
End Sub

Lastly, for any Normal .NET Control you can use this same code but instead of my having to use Control.Appearance.Cursor ---> you would instead use Control.Cursor

在 Jimi 的推动下,我改为使用 Form Cursor,因为我正在使用的 Infragistics Control 在出于某种原因使用鼠标按钮时无法更新鼠标光标。我改为使用表单光标。代码如下:

Private Sub ClsTextObj_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    ' Get object under cursor when user moved mouse - MouseMove
    ' We are only going to perform the WORK if the object is TypeOf clsTextObj
    If TypeOf sender Is clsTextObj Then
        Dim txt_clsText_ctrl_tmp = DirectCast(sender, clsTextObj)

        Dim IsMouseLeftButton = e.Button = Windows.Forms.MouseButtons.Left

        Dim mCursor As Cursor
        Dim MouseCursor = "Logic Never Reached"

        If IsMouseLeftButton And IsMouseDown Then
            MouseCursor = "Hand"    'Used to output info to screen
            mCursor = Cursors.Hand
        Else
            MouseCursor = "SizeAll" 'Used to output info to screen
            mCursor = Cursors.SizeAll
        End If

        'START Determine Mouse Cursor - Find where the mouse cursor is within the control
        Dim MouseIsInLeftEdge As Boolean
        Dim MouseIsInRightEdge As Boolean
        Dim MouseIsInTopEdge As Boolean
        Dim MouseIsInBottomEdge As Boolean

        MouseIsInLeftEdge = Math.Abs(e.X) <= 9
        MouseIsInRightEdge = Math.Abs(e.X - txt_clsText_ctrl_tmp.Width) <= 9
        MouseIsInTopEdge = Math.Abs(e.Y) <= 9
        MouseIsInBottomEdge = Math.Abs(e.Y - txt_clsText_ctrl_tmp.Height) <= 9

        If MouseIsInLeftEdge Then
            If MouseIsInTopEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNWSE
            ElseIf MouseIsInBottomEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNESW
            Else
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeWE
            End If
        ElseIf MouseIsInRightEdge Then
            If MouseIsInTopEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNESW
            ElseIf MouseIsInBottomEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNWSE
            Else
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeWE
            End If
        ElseIf (MouseIsInTopEdge Or MouseIsInBottomEdge) Then
            txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNS
        Else
            txt_clsText_ctrl_tmp.Appearance.Cursor = mCursor
        End If
        'END Determining Mouse Cursor

        'Capture Mouse Down Clicks while moving mouse cursour.  Mouse Move overrides Mouse Down basically and so we capture mouse down buttons while moving here
        'If e.Button = Windows.Forms.MouseButtons.Left Then
        If IsMouseLeftButton Then 'And IsMouseDown Then
            Me.Cursor = mCursor
            txt_clsText_ctrl.Location = New Point(txt_clsText_ctrl.Location.X + (e.X - initialClickLocation.X), txt_clsText_ctrl.Location.Y + (e.Y - initialClickLocation.Y))
        End If


        ClsTextObj1.Value = "X: " & txt_clsText_ctrl_tmp.Location.X + e.X _
                          & ",Y: " & txt_clsText_ctrl_tmp.Location.Y + e.Y

        'Build User output info
        builder.Clear()
        builder.Append("Control Name: " & txt_clsText_ctrl_tmp.Name).AppendLine()
        builder.Append("e.X " & e.X & ", e.Y: " & e.Y).AppendLine()
        builder.Append("e.Point Location: " & e.Location.ToString()).AppendLine()
        builder.Append("txt_clsText_ctrl Point Location: " & txt_clsText_ctrl_tmp.Location.ToString()).AppendLine()
        builder.Append("Form Location: {X=" & txt_clsText_ctrl_tmp.Location.X + e.X)
        builder.Append(",Y=" & txt_clsText_ctrl_tmp.Location.Y + e.Y & "}").AppendLine()
        builder.Append("initial Ctrl Location: " & initialCtrlLocation.ToString()).AppendLine()
        builder.Append("initial Click Location: " & initialClickLocation.ToString()).AppendLine()
        builder.Append("Calc Point: " & "X: " & (e.X - initialClickLocation.X))
        builder.Append(",Y: " & (e.Y - initialClickLocation.Y)).AppendLine()
        builder.Append("New Label Pos: " & "X: " & txt_clsText_ctrl_tmp.Location.X + (e.X - initialClickLocation.X))
        builder.Append(",Y: " & txt_clsText_ctrl_tmp.Location.Y + (e.Y - initialClickLocation.Y)).AppendLine()
        builder.Append("Mouse Button: " & e.Button.ToString()).AppendLine()
        builder.Append("Left Edge: " & MouseIsInLeftEdge).AppendLine()
        builder.Append("Right Edge: " & MouseIsInRightEdge).AppendLine()
        builder.Append("Top Edge: " & MouseIsInTopEdge).AppendLine()
        builder.Append("Bottom Edge: " & MouseIsInBottomEdge).AppendLine()
        builder.Append("Cursor: " & MouseCursor).AppendLine()
        builder.Append("Is Mouse Down: " & IsMouseDown).AppendLine()
        builder.Append("Is Mouse Left: " & IsMouseLeftButton)

        ClsTextObj2.Value = builder.ToString()

    End If

End Sub

Private Sub ClsTextObj_MouseDown(sender As Object, e As MouseEventArgs)
    ' Get object under cursor when user clicked MouseDown
    If TypeOf sender Is clsTextObj Then

        initialClickLocation = New Point(e.X, e.Y)

        txt_clsText_ctrl = DirectCast(sender, clsTextObj)

        IsMouseDown = True

        initialCtrlLocation = txt_clsText_ctrl.Location

        'ListView Stuff
        updateListView()

    End If
End Sub

Private Sub ClsTextObj_MouseUp(sender As Object, e As MouseEventArgs)

    IsMouseDown = False
    Me.Cursor = Cursors.Default

End Sub

通过一些额外的代码,我终于得到了我需要的东西: