Beim der im nachfolgenden Code wird ein markierte Text ausgegeben.
Dabei bewegen sich einige Buchstaben bei dem rechts markierten Teilstring.
Ich habe dort schon mit verschiedenen Einstellungen einige Versuche gemacht.
Auch eine Ausgabe mir RectangleF hat hierbei nicht gehofen. Schlimmer wird es
wenn anstatt Single Integer benutzt wird. Habe aber auch hier keine bessere
Lösung gefunden.
Bei dem Test im unteren Beispiel eine PictureBox auf Form bringen und mit
der Maus drüber fahren.
Jürgen
Option Strict Off
Public Class Form1
Dim sStr As String = "Bitte Maus hin und her bewegen. Warum bewegen sich" & _
"beim selektierten Text einige Buchstaben."
Dim oFont As Font = New Font("Courier New", 13)
Dim strFormat As New StringFormat(StringFormat.GenericTypographic) With _
{.FormatFlags = StringFormatFlags.MeasureTrailingSpaces}
Dim iPos As Integer
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles _
Me.Load
Me.Size = New Size(1200, 120)
Me.Left = 100
PictureBox1.Location = New Point(0, 0)
PictureBox1.Size = New Size(Me.ClientRectangle.Width, _
Me.ClientRectangle.Height)
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
iPos = e.X \ CInt(Me.CreateGraphics.MeasureString("X", oFont, _
Integer.MaxValue, strFormat).Width)
If iPos > sStr.Length Then iPos = sStr.Length
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As _
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim gra As Graphics = e.Graphics
Dim iCharW As Single = Me.CreateGraphics.MeasureString("X", oFont, _
Integer.MaxValue, strFormat).Width
Dim iMitte As Integer = sStr.Length \ 2
Dim iCenter As Single = iCharW * iMitte
Dim iLeft As Integer
Dim iRight As Integer
'Zeile ausgeben
gra.DrawString(sStr, oFont, Brushes.Black, 0, 20, strFormat)
If iPos < iMitte Then
iLeft = iPos
iRight = iMitte
ElseIf iPos > iMitte Then
iLeft = iMitte
iRight = iPos
Else
Exit Sub
End If
'Text markieren
Dim iX As Single = iLeft * iCharW
Dim iWidth As Single = (iRight - iLeft) * iCharW
gra.FillRectangle(Brushes.Blue, iX, 20, iWidth, 25)
gra.DrawString(sStr.Substring(iLeft, iRight - iLeft), oFont, _
Brushes.White, iX, 20, strFormat)
End Sub
End Class _______________________________________________________________
Jeder Mensch ist anders. Darum nimm jeden Menschen so wie er ist. |