Hallo,
ich entwickle gerade eine komplett selbst gezeichnete Textbox. Sieht so weit auch schon sehr gut aus. Allerdings habe ich hierbei ein kleines Problem.
Ich zeichne den Text wie folgt:
Public Sub Text(ByVal g As Graphics, ByVal ClientRectangle As Rectangle, _
ByVal Text As String, ByVal TextAlign As ocl.AlignmentHorizontal, ByVal _
Font As Font, ByVal Color As Color)
Dim Pos As New Point(0, 0)
Dim flags As TextFormatFlags = TextFormatFlags.NoPadding
Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)
Dim SizeF As SizeF = g.MeasureString(Text, Font)
Dim Size As Size = TextRenderer.MeasureText(g, Text, Font, _
proposedSize, flags)
Select Case TextAlign
Case ocl.AlignmentHorizontal.Left
Pos.X = ClientRectangle.X
Case ocl.AlignmentHorizontal.Middle
Pos.X = CSng(ClientRectangle.X + ClientRectangle.Width / 2.0 - _
Size.Width / 2)
Case ocl.AlignmentHorizontal.Right
Pos.X = ClientRectangle.Right - Size.Width
End Select
Pos.Y = ClientRectangle.Y + CSng(ClientRectangle.Height / 2.0 - _
Size.Height / 2.0) 'Mitte
Dim rect As Rectangle = New Rectangle(Pos, Size)
TextRenderer.DrawText(g, Text, Font, Pos, Color.Black, flags)
End Sub und jetzt möchte ich einen Cursor darüber malen, der sich wenn ich die Tasten nach links und rechts bewege darüber gezeichnet wird:
Public Sub Cursor(ByVal g As Graphics, ByVal Rectangle As Rectangle, ByVal _
Color As Color, ByVal State As Boolean)
If State = False Then
'sichtbar aber transparent
Color = Color.FromArgb(128, Color.Gray)
End If
g.FillRectangle(New SolidBrush(Color), Rectangle)
End Sub Was mir Probleme bereitet ist die Ermittlung der genauen Cursoposition:
Private Function GetCursorCaret(ByVal g As Graphics, ByVal ClientRectangle _
As Rectangle, ByVal Font As Font) As Rectangle
Dim Location As Point
Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)
Dim flags As TextFormatFlags = TextFormatFlags.NoPadding
Location = ClientRectangle.Location
Dim X As Integer = 0
Dim T As String = ""
If SelectStart > 0 Then
T = Caption.Substring(0, SelectStart)
'X = CInt(g.MeasureString(T, Font).Width)
Location.X = TextRenderer.MeasureText(T, Font, proposedSize, _
flags).Width
Location.X = Location.X + X
End If
GetCursorCaret = New Rectangle(Location, New Size(3, _
ClientRectangle.Height))
End Function Der Cursor steht nie direkt hinter dem Buchstaben, sondern "optisch gesehen" an einer völlig falschen Position.
Weiß jemand Rat? |