2015-01-09 75 views
0

我有光標不可見,當用戶滾動按鈕時,它會突出顯示。問題是當用戶在按鈕之間時,他不知道他的光標在哪裏。鼠標位置+金額IntersectsWith按鈕

在鼠標離開事件中,我希望光標跳到最接近的按鈕(而不是一個,他只是左)

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不與繪圖工作點

有沒有什麼辦法讓我檢查最接近的按鈕是什麼?

+0

改變你的邏輯來測試邊界(Rect)是否包含這個點:'如果c.Bounds.Contains(Cursor.Position)'你可能必須先將光標轉換成座標首先 – Plutonix

+1

As你通過控件循環,你將不得不計算Cursor.Location和c.Bounds之間的距離並找到距離最近的那個。如果c.Bounds.Contains(Cursor.Position)那麼遊標已經在控件中,並且你沒有任何事情要做。 – dwilliss

+0

@dwilliss我該怎麼做? – Dman

回答

1

我認爲界面將很難使用(尤其是如果按鈕是不是足夠大),但這裏的另一種實現方式:

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 
0
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 
+0

那麼,現在忽略我的計算距離函數。看到Bradly在我輸入我的回覆時張貼了一個鏈接。但是找到_closes_按鈕的邏輯仍然是你想要的。 – dwilliss