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

时间:2018-10-17 21:42:23

标签: .net vb.net mouse-cursor

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

enter image description here

在“文本控制坐标”文本框中的gif图像中,您可以看到我正在更新此文本框,其中输入了游标应更新到的内容。这是底部框中的最后一个条目。光标设置为“ SizeAll”,但是当我使用鼠标左键移动控件时,我有将控件光标设置为“手”的代码。文本框显示已正确达到将光标更新为“手形”的逻辑。问题是,只有在释放鼠标左键后,光标才会更新。直到我再次开始移动鼠标并且MouseMove事件再次接管为止。

select Convert(datetime,'''' + SUBSTRING(arc_billed_to_date,7,5) + '-' + SUBSTRING(arc_billed_to_date,4,2) + '-' + SUBSTRING(arc_billed_to_date,1,2)+ '''',105) as x from wkpol 

我在这里想念什么?除了鼠标光标以外,在使用“左鼠标”按钮时,我可以成功更新应用程序的其他部分。我什至尝试在控件上使用Refresh()以及其他一些愚蠢的想法,这些想法似乎永远不会在控件移动时变成光标。

您甚至可以看到我已经根据MouseMove子控件中控件的边缘成功更新了Mouse Cursor。但是,当按下鼠标左键时,就好像鼠标光标的更新处于暂停状态,直到我放开按钮为止。

  

更新10.19.18:   我改用了Form Cursor:

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

由于我使用的是Form Cursor,因此必须确保将其更新回默认值。我发现MouseUp可以正常工作。至少我还没有遇到任何错误。

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
  

最后,对于任何普通的.NET控件,您都可以使用相同的代码,但是不必使用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 --->而是使用Control.Appearance.Cursor

1 个答案:

答案 0 :(得分:0)

在Jimi的推动下,我改为使用Form Cursor作为正在使用的基础设施控件,而无论出于何种原因,在使用Mouse Button时都无法更新鼠标光标。我改用了Form Cursor。下面的代码:

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

有了一些额外的代码,我终于得到了我需要的东西: enter image description here