【发布时间】:2010-04-02 21:02:00
【问题描述】:
我正在尝试调整 WPF RichTextBox 以完全适应特定等宽字体中的字符网格。我目前正在使用 FormattedText 来确定我的 RichTextBox 的宽度和高度,但是它为我提供的测量值太小了——特别是两个字符的宽度太小了。
有没有更好的方法来执行这项任务?这似乎不是确定控件大小的合适方法。
RichTextBox rtb;
rtb = new RichTextBox();
FontFamily fontFamily = new FontFamily("Consolas");
double fontSize = 16;
char standardizationCharacter = 'X';
String standardizationLine = "";
for(long loop = 0; loop < columns; loop ++) {
standardizationLine += standardizationCharacter;
}
standardizationLine += Environment.NewLine;
String standardizationString = "";
for(long loop = 0; loop < rows; loop ++) {
standardizationString += standardizationLine;
}
Typeface typeface = new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
FormattedText formattedText = new FormattedText(standardizationString, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black);
rtb.Width = formattedText.Width;
rtb.Height = formattedText.Height;
【问题讨论】:
-
您如何将文本写入
RichTextBox?
标签: c# wpf richtextbox monospace