鼠标位置 + 数量与按钮相交

Mouse Location + Amount IntersectsWith Button

我的光标是不可见的,当用户滚动到一个按钮上时它会突出显示。问题是当用户在按钮之间时,他不知道光标在哪里。

在鼠标离开事件上我想让光标跳到最近的按钮(不是他刚离开的那个)

   Private Sub btnNumbers_Mouseleave(sender As System.Object, e As System.EventArgs) Handles btnAlef.MouseLeave, btnBkspc.MouseLeave, btnBack.MouseLeave, btnClearAll.MouseLeave, btnDeleteWord.MouseLeave, btnEditMenu.MouseLeave, btnUndo.MouseLeave, btnSpeak.MouseLeave, btnGimel.MouseLeave, btnZayin.MouseLeave, btnYud.MouseLeave, btnVav.MouseLeave, btnTzadik.MouseLeave, btnTuf.MouseLeave, btnTes.MouseLeave, btnSpace.MouseLeave, btnShin.MouseLeave, btnSamech.MouseLeave, btnReish.MouseLeave, btnQuestion.MouseLeave, btnPred5.MouseLeave, btnPred4.MouseLeave, btnPred3.MouseLeave, btnPred2.MouseLeave, btnPred1.MouseLeave, btnPeriod.MouseLeave, btnPercent.MouseLeave, btnOpenParen.MouseLeave, btnNun.MouseLeave, btnMem.MouseLeave, btnLetterPrediction2.MouseLeave, btnLetterPrediction1.MouseLeave, btnLamed.MouseLeave, btnKuf.MouseLeave, btnHey.MouseLeave, btnFey.MouseLeave, btnExclamation.MouseLeave, btnEnter.MouseLeave, btnEnderTzadik.MouseLeave, btnEnderNun.MouseLeave, btnEnderMem.MouseLeave, btnEnderFey.MouseLeave, btnEnderChaf.MouseLeave, btnDollar.MouseLeave, btnDaled.MouseLeave, btnCloseParen.MouseLeave, btnChes.MouseLeave, btnChaf.MouseLeave, btnBkspc.MouseLeave, btnBeis.MouseLeave, btnAyin.MouseLeave, btnApostrophe.MouseLeave, btn9.MouseLeave, btn8.MouseLeave, btn7.MouseLeave, btn6.MouseLeave, btn5.MouseLeave, btn4.MouseLeave, btn3.MouseLeave, btn2.MouseLeave, btn1.MouseLeave, btn0.MouseLeave, btnSavedPhrases5.MouseLeave, btnSavedPhrases4.MouseLeave, btnSavedPhrases3.MouseLeave, btnSavedPhrases2.MouseLeave, btnSavedPhrases1.MouseLeave, btnSettings.MouseLeave, btnPhrases.MouseLeave, btnNumbers.MouseLeave, btnMinimize.MouseLeave, btnHebrew.MouseLeave, btnExit.MouseLeave, btnCopy.MouseLeave, btnRightWord.MouseLeave, btnRightChar.MouseLeave, btnLeftWord.MouseLeave, btnLeftChar.MouseLeave, btnHome.MouseLeave, btnEnd.MouseLeave, btnT8.MouseLeave, btnT7.MouseLeave, btnT6.MouseLeave, btnT5.MouseLeave, btnT4.MouseLeave, btnT3.MouseLeave, btnT2.MouseLeave, btnT1.MouseLeave
    Dim btn As Button = DirectCast(sender, Button)
    btn.FlatStyle = FlatStyle.Standard
    Dim GetCursorPos = Cursor.Position
    If FormSettings.chbxBorderHover.Checked = True Then
        For Each c As Control In Me.Controls.OfType(Of Button)()
            If GetCursorPos.IntersectsWith(c.Bounds) Then
                Cursor.Position = c.Location
                Exit For
            End If
        Next
    End If

End Sub

这是我目前所拥有的,但是intercectsWith不适用于绘图点

有什么方法可以让我检查最近的按钮是什么吗?

Dim minDist As Double = Double.MaxVal
Dim closestCtrl as Control = Nothing
For Each c As Control In Me.Controls.OfType(Of Button)()
    If c Is sender Then Continue For  ' Skip the one we came from

    Dim dist = ComputeDistance(Cursor.Position, c.Bounds)
    If dist < minDist Then
        minDist = dist
        closestCtrl = c
    End If       
Next

...

   ' Compute distance between 2 points
   Private Function ComputeDistance(x1 as Integer, y1 as Integer, x2 as Integer, y2 as Integer) as Double
        Dim dx = (x1 - x2)
        Dim dy = (y1 - y2)
       Return Math.Sqrt(dx*dx + dy*dy)
    End Function

   ' Compute distance between a point and the closest corner of a rectangle
   ' I'm going to do this a lazy way. There's probably a better one
   Private Function ComputeDistance(pt as Point, rect as Rectangle) as Distance
        Dim dist as New List(of Double)
        dist.Add(ComputeDistance(pt.x, pt.y, rect.Left, rect.Top)
        dist.Add(ComputeDistance(pt.x, pt.y, rect.Right, rect.Top)
        dist.Add(ComputeDistance(pt.x, pt.y, rect.Left, rect.Bottom)
        dist.Add(ComputeDistance(pt.x, pt.y, rect.Right, rect.Bottom)
        Return dist.Min()
   End Function

我认为该界面会难以使用(尤其是当按钮不够大时),但这是另一种实现方式:

Private Sub btnNumbers_Mouseleave(sender As System.Object, e As System.EventArgs) Handles btnAlef.MouseLeave, btnBkspc.MouseLeave, btnBack.MouseLeave, btnClearAll.MouseLeave, btnDeleteWord.MouseLeave, btnEditMenu.MouseLeave, btnUndo.MouseLeave, btnSpeak.MouseLeave, btnGimel.MouseLeave, btnZayin.MouseLeave, btnYud.MouseLeave, btnVav.MouseLeave, btnTzadik.MouseLeave, btnTuf.MouseLeave, btnTes.MouseLeave, btnSpace.MouseLeave, btnShin.MouseLeave, btnSamech.MouseLeave, btnReish.MouseLeave, btnQuestion.MouseLeave, btnPred5.MouseLeave, btnPred4.MouseLeave, btnPred3.MouseLeave, btnPred2.MouseLeave, btnPred1.MouseLeave, btnPeriod.MouseLeave, btnPercent.MouseLeave, btnOpenParen.MouseLeave, btnNun.MouseLeave, btnMem.MouseLeave, btnLetterPrediction2.MouseLeave, btnLetterPrediction1.MouseLeave, btnLamed.MouseLeave, btnKuf.MouseLeave, btnHey.MouseLeave, btnFey.MouseLeave, btnExclamation.MouseLeave, btnEnter.MouseLeave, btnEnderTzadik.MouseLeave, btnEnderNun.MouseLeave, btnEnderMem.MouseLeave, btnEnderFey.MouseLeave, btnEnderChaf.MouseLeave, btnDollar.MouseLeave, btnDaled.MouseLeave, btnCloseParen.MouseLeave, btnChes.MouseLeave, btnChaf.MouseLeave, btnBkspc.MouseLeave, btnBeis.MouseLeave, btnAyin.MouseLeave, btnApostrophe.MouseLeave, btn9.MouseLeave, btn8.MouseLeave, btn7.MouseLeave, btn6.MouseLeave, btn5.MouseLeave, btn4.MouseLeave, btn3.MouseLeave, btn2.MouseLeave, btn1.MouseLeave, btn0.MouseLeave, btnSavedPhrases5.MouseLeave, btnSavedPhrases4.MouseLeave, btnSavedPhrases3.MouseLeave, btnSavedPhrases2.MouseLeave, btnSavedPhrases1.MouseLeave, btnSettings.MouseLeave, btnPhrases.MouseLeave, btnNumbers.MouseLeave, btnMinimize.MouseLeave, btnHebrew.MouseLeave, btnExit.MouseLeave, btnCopy.MouseLeave, btnRightWord.MouseLeave, btnRightChar.MouseLeave, btnLeftWord.MouseLeave, btnLeftChar.MouseLeave, btnHome.MouseLeave, btnEnd.MouseLeave, btnT8.MouseLeave, btnT7.MouseLeave, btnT6.MouseLeave, btnT5.MouseLeave, btnT4.MouseLeave, btnT3.MouseLeave, btnT2.MouseLeave, btnT1.MouseLeave
    Dim btn As Button = DirectCast(sender, Button)
    btn.FlatStyle = FlatStyle.Standard

    If FormSettings.chbxBorderHover.Checked = True Then
        Dim currentPos As Point = Me.PointToClient(Cursor.Position)
        Dim closestButton = (From x In Me.Controls.OfType(Of Button)()
                        Where x IsNot btn
                        Order By PointToButtonDistance(currentPos, x) Ascending).FirstOrDefault
        Cursor.Position = closestButton.PointToScreen(New Point(0, 0))
    End If
End Sub

Private Function PointToButtonDistance(ByVal pt As Point, ByVal btn As Button) As Long
    Dim center As New Point(btn.Location.X + btn.Width / 2, btn.Location.Y + btn.Height / 2)
    Dim dx As Integer = Math.Max(Math.Abs(pt.X - center.X) - btn.Width / 2, 0)
    Dim dy As Integer = Math.Max(Math.Abs(pt.Y - center.Y) - btn.Height / 2, 0)
    Return dx * dx + dy * dy
End Function