我通过为插入和覆盖模式创建插入符号来扩展富文本框。问题是测量的宽度太宽。这在较大的字体大小上最为明显。
返回的高度SelectionFont.Height
是正确的。没有类似的 Width 属性吗?
如何ch
准确测量指定字符的宽度?谁能提供一个例子?
下面是转换为 C# 的代码,下面是 VB.NET 中的代码:
using System;
public class RichTextBoxCaret : RichTextBox
{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private extern static void CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private extern static void ShowCaret(IntPtr hWnd);
private bool mInsertKeyState = true;
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Insert)
{
mInsertKeyState = !mInsertKeyState;
}
this.DrawCaret();
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
this.DrawCaret();
}
public void DrawCaret()
{
Size sz = new Size(0, 0);
if (this.SelectionFont != null)
{
if (!mInsertKeyState && this.SelectionStart < this.TextLength)
{
using (Graphics g = this.CreateGraphics)
{
using (Font f = new Font(this.SelectionFont.FontFamily, this.SelectionFont.Size, this.SelectionFont.Style, GraphicsUnit.Pixel, Convert.ToByte(0), false))
{
char ch = this.Text[this.SelectionStart];
sz.Width = Convert.ToInt32(g.MeasureString(ch, f).Width * this.ZoomFactor);
}
}
}
sz.Height = Convert.ToInt32(this.SelectionFont.Height * this.ZoomFactor);
}
if (!sz.IsEmpty)
{
CreateCaret(this.Handle, IntPtr.Zero, sz.Width, sz.Height);
ShowCaret(this.Handle);
}
}
}
对于 VB.NET:
Public Class RichTextBoxCaret
Inherits RichTextBox
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Sub CreateCaret(ByVal hWnd As IntPtr, ByVal hBitmap As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer)
End Sub
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Sub ShowCaret(ByVal hWnd As IntPtr)
End Sub
Private mInsertKeyState As Boolean = True
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
If e.KeyCode = Keys.Insert Then
mInsertKeyState = Not mInsertKeyState
End If
Me.DrawCaret()
End Sub
Protected Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(mevent)
Me.DrawCaret()
End Sub
Public Sub DrawCaret()
Dim sz As New Size(0, 0)
If Me.SelectionFont IsNot Nothing Then
If Not mInsertKeyState AndAlso Me.SelectionStart < Me.TextLength Then
Using g As Graphics = Me.CreateGraphics
Using f As New Font(Me.SelectionFont.FontFamily, Me.SelectionFont.Size, Me.SelectionFont.Style, GraphicsUnit.Pixel, CByte(0), False)
Dim ch As Char = Me.Text.Chars(Me.SelectionStart)
sz.Width = CInt(g.MeasureString(ch, f).Width * Me.ZoomFactor)
End Using
End Using
End If
sz.Height = CInt(Me.SelectionFont.Height * Me.ZoomFactor)
End If
If Not sz.IsEmpty Then
CreateCaret(Me.Handle, IntPtr.Zero, sz.Width, sz.Height)
ShowCaret(Me.Handle)
End If
End Sub
End Class