Dessine une chaîne sur un contrôle, puis correspondent clics de souris aux positions de caractères sur la forme. C'est en fait plus facile qu'il n'y paraît (ceci est adapté de la documentation standard sur MeasureCharacterRanges, ce qui simplifie la tâche entrante). L'exemple est dessiné sur un formulaire, il serait assez simple d'en faire un contrôle utilisateur.
Dans cet exemple, si vous cliquez sur une lettre, une boîte de message apparaît et vous indique la lettre sur laquelle vous venez de cliquer.
Espérons que ça aide.
P.S. S'il vous plaît pardonner "numéros magiques" par ex. "Connaître" il y aura 25 éléments dans le tableau, c'est juste un échantillon après tout :)
Public Class Form1
Const LETTERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private letterRects(25) As System.Drawing.RectangleF
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Dim index As Integer = -1
Dim mouseP As Point = Me.PointToClient(MousePosition)
For i As Integer = 0 To 25
If letterRects(i).Contains(mouseP.X, mouseP.Y) Then
index = i
Exit For
End If
Next
If index >= 0 Then
MessageBox.Show("Letter = " + LETTERS(index).ToString())
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' Set up string.
Dim stringFont As New Font("Times New Roman", 16.0F)
' Set character ranges
Dim characterRanges(26) As CharacterRange
For i As Integer = 0 To 25
characterRanges(i) = New CharacterRange(i, 1)
Next
' Create rectangle for layout, measurements below are not exact, these are "magic numbers"
Dim x As Single = 50.0F
Dim y As Single = 50.0F
Dim width As Single = 400.0F
Dim height As Single = 40.0F
Dim layoutRect As New RectangleF(x, y, width, height)
' Set string format.
Dim stringFormat As New StringFormat
stringFormat.FormatFlags = StringFormatFlags.FitBlackBox
stringFormat.SetMeasurableCharacterRanges(characterRanges)
' Draw string to screen.
e.Graphics.DrawString(letters, stringFont, Brushes.Black, _
x, y, stringFormat)
Dim stringRegions() As [Region]
' Measure two ranges in string.
stringRegions = e.Graphics.MeasureCharacterRanges(letters, _
stringFont, layoutRect, stringFormat)
For i As Integer = 0 To 25
letterRects(i) = stringRegions(i).GetBounds(e.Graphics)
Next
End Sub
End Class
Juste ce dont j'avais besoin, bravo pour ça! – anonym0use