根据等宽字体的大小设置WPF RichTextBox的宽度和高度
我正在尝试调整 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;
I am trying to fit a WPF RichTextBox to exactly accommodate a grid of characters in a particular monospace font. I am currently using FormattedText to determine the width and height of my RichTextBox, but the measurements it is providing me with are too small--specifically two characters in width too small.
Is there a better way to perform this task? This does not seem to be an appropriate way to determine the size of my control.
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有一个非常基本的
RichTextBox
,其中仅包含标准的FlowDocument
,那么您必须考虑RichTextBox
使用的所有额外布局。Document.PagePadding
属性默认为{5,0,5,0}
。所以你必须将宽度加 10。此外,
RichTextBox
的BorderThickness
默认为{1, 1, 1, 1}
。所以你必须将宽度加 2。因此,您想要的代码可能如下所示:
如果您这样做,您仍然会偏离 1 个字符,但这有点误导。即使文本只有一点点宽而不仅仅是整个字符宽度,
RichTextBox
也会换行。如果将宽度加 2 即可。我无法弄清楚额外的 2 到底是从哪里来的。我从来没有尝试过让宽度变得如此精确。我之前遇到过PagePadding
和BorderThickness
问题,但我就是找不到额外的 2。它可能只是您所坚持的一个常量,但我怀疑它。它一定在某个地方。关于生成
StandardizationLine
的小技巧,您可以这样做来清理循环。
Assuming you have a very basic
RichTextBox
with just the standardFlowDocument
inside it then you have to take into account all of the extra layoutRichTextBox
uses.The
Document.PagePadding
property defaults to{5,0,5,0}
. So you will have to add 10 to the width.Additionally the
RichTextBox
has aBorderThickness
that defaults to{1, 1, 1, 1}
. So you will have to add 2 to the width.So the code you want could look like:
If you do that you will still be off by 1 character, but that's a bit misleading. The
RichTextBox
will wrap even if the text is just a tiny bit to wide not just a whole character width. If you add 2 to the width it works out. I can't figure out exactly where that extra 2 is coming from. I've never tried to get the width to be so exact. I have run into thePagePadding
andBorderThickness
issues before, but I just can't find the extra 2. It might just be a constant you are stuck with, but I doubt it. It's got to be somewhere.A small tip on producing the
StandardizationLine
you can doto clean up the loop.
我找到了富文本框高度问题的解决方案。我已将其修改为一般用途。
在您的应用程序中创建以下结构....
在您的类中为表单创建以下私有变量(无论您需要计算富文本高度)
将以下方法添加到您的表单类
您可能需要修改上述方法以使其按照您的要求工作...
确保将 Rtf 字符串作为参数发送到方法而不是普通文本,并确保将可用的宽度和高度分配给方法中的 Richtextbox 变量...
您可以根据您的要求使用 WordWrap...
I found a solution for the Rich text box height issues.. i have modified it a for general use..
Create following structs in your application....
Create following private variables in your class for form (where ever you need to calculate rich text height)
Add following method to your Class for form
You may need to modify above method to make it work as per your requirement...
Make sure to send Rtf string as parameter to method not normal text and also make sure to assign available width and height to the Richtextbox variable in the method...
You can play with WordWrap depending on your requirement...