如何将焦点移除到mousedown中的对象 [英] How I can remove the focus to an object in mousedown

查看:83
本文介绍了如何将焦点移除到mousedown中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试过按下鼠标按钮并将光标滑过控件,它会收到MouseDown事件。在进入另一个控件时不释放鼠标按钮,它会收到MouseDown事件。



类似于用手指滑过钢琴键的东西。

谢谢



简单



我尝试过:



Hi
I tried that pressing the mouse button and slide the cursor over a control, it receives the MouseDown event. Without releasing the mouse button when entering another control, it receives the MouseDown event.

Something similar to slide your finger across the keys of a piano.
Thank you

Exemple

What I have tried:

'Programmed by Lekim'
Option Strict On
Imports System.Runtime.InteropServices


Public Class Form1

    <DllImport("user32.dll")> _
    Private Shared Sub mouse_event(ByVal dwFlags As UInteger, _
                                   ByVal dx As UInteger, _
                                   ByVal dy As UInteger, _
                                   ByVal dwData As UInteger, _
                                   ByVal dwExtraInfo As Integer)
    End Sub


    Dim lblkey(5) As Label
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim locLBL As New Point(10, 10)
        Dim inc As Integer
        For I As Integer = 0 To 5
            lblkey(I) = New Label
            lblkey(I).Size = CType(New Point(20, 100), Drawing.Size)
            lblkey(I).BorderStyle = BorderStyle.FixedSingle
            lblkey(I).Location = New Point(locLBL.X + inc, locLBL.Y)
            Me.Controls.Add(lblkey(I))
            inc += 19
        Next
        For I As Integer = 0 To 5
            AddHandler lblkey(I).MouseDown, AddressOf lblkey_MouseDown
            AddHandler lblkey(I).MouseUp, AddressOf lblkey_MouseUp
            AddHandler lblkey(I).MouseMove, AddressOf lblkey_MouseMove

        Next

    End Sub
    Private Sub lblkey_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim Index As Integer = Array.IndexOf(lblkey, sender)
        Dim mPoint As New Point(Me.PointToClient(Cursor.Position).X, Me.PointToClient(Cursor.Position).Y)
        Dim X As Integer = mPoint.X

        If X < CInt(lblkey(Index).Left) Or
            X > (CInt(lblkey(Index).Left) + _
                 CInt(lblkey(Index).Width)) Then
            EventoUp()
            EventoDown()
        End If

    End Sub
    Private Sub lblkey_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim Index As Integer = Array.IndexOf(lblkey, sender)
        If Button.MouseButtons = MouseButtons.Left Then
            lblkey(Index).BackColor = Color.Azure
        End If
    End Sub
    Private Sub lblkey_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim Index As Integer = Array.IndexOf(lblkey, sender)
        lblkey(Index).BackColor = Color.Transparent
    End Sub

End Class
Module modEventMouse
    <DllImport("user32.dll")> _
    Public Sub mouse_event(ByVal dwFlags As UInteger, _
                                   ByVal dx As UInteger, _
                                   ByVal dy As UInteger, _
                                   ByVal dwData As UInteger, _
                                   ByVal dwExtraInfo As Integer)
    End Sub
    <Flags()> _
    Public Enum MouseEventFlags As UInteger
        MOUSEEVENTF_ABSOLUTE = &H8000
        MOUSEEVENTF_LEFTDOWN = &H2
        MOUSEEVENTF_LEFTUP = &H4
        MOUSEEVENTF_MIDDLEDOWN = &H20
        MOUSEEVENTF_MIDDLEUP = &H40
        MOUSEEVENTF_MOVE = &H1
        MOUSEEVENTF_RIGHTDOWN = &H8
        MOUSEEVENTF_RIGHTUP = &H10
        MOUSEEVENTF_XDOWN = &H80
        MOUSEEVENTF_XUP = &H100
        MOUSEEVENTF_WHEEL = &H800
        MOUSEEVENTF_HWHEEL = &H1000
    End Enum
    ''' <summary>
    ''' Simulate MouseDown the left mouse button
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub EventoDown()
        Call modEventMouse.mouse_event(MouseEventFlags.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    End Sub
    ''' <summary>
    ''' Simulate MouseUp the left mouse button
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub EventoUp()
        Call modEventMouse.mouse_event(MouseEventFlags.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
    End Sub


End Module

推荐答案

没有删除焦点这样的概念。你没有真正解释你想要达到的目标,但你需要理解焦点的概念。这始终是键盘焦点。整个桌面上只有一个控件拥有焦点,一次只有一个。要删除控件的焦点,您只需根据需要关注其他控件,调用 Control.Focus()



问题是:我试过按下鼠标按钮并将光标滑过控件,它会收到MouseDown事件。是完全错的。即使你没有在同一个类中定义,也没有办法调用一些。即使你从定义类中派生出你的类,它也无济于事。这实际上是重要的.NET事件傻瓜式功能。



而不是表达你(错误的)如何实现某些目标的想法,你应该更好地解释你的最终目的,从用户的角度看这种行为应该如何。



你不应该把焦点与不同的东西混在一起,比如选择。选择是一些控制状态的一部分;但只有整个控制可以集中,而不是它的一部分。此外,在任何鼠标按钮状态下,鼠标位置与聚焦无关。鼠标单击可能会或可能不会使控件集中。



代码的最大问题是使用P / Invoke,它完全破坏了您的平台兼容性并严重影响了可维护性你的产品您只需使用纯.NET FCL即可。



-SA
There is no such concept as "remove the focus". You did not really explain what you want to achieve, but you need to understand the concept of "focus". This is always a keyboard focus. There is only one control on the whole desktop which owns the focus, only one at a time. To remove the focus of the control, you simply need to focus some other control, according to your requirements, calling Control.Focus().

The problem is: your "I tried that pressing the mouse button and slide the cursor over a control, it receives the MouseDown event." is totally wrong. There is no a way to invoke some even you did not define in the same class. Even if you derive your class from the defining class, it cannot help. This is actually the important .NET event fool-proof feature.

Instead of expressing your (wrong) idea of how to achieve something, you should better explain your ultimate purpose, how the behavior should look from the user's perspective.

You should not mix up focus with different things, such as selection. The selection is the part of the state of some controls; but only the whole control can be focused, not a part of it. Also, mouse position, in any state of mouse buttons, has nothing to do with focusing. A mouse click may or may not leave the control focused.

The biggest problem of you code is using P/Invoke, which totally breaks your platform compatibility and strongly compromises maintainability of your product. All you do you can do with pure .NET FCL.

—SA


这篇关于如何将焦点移除到mousedown中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆