重新计算 RichTextBox 控件的宽度时遇到问题
我正在创建这个程序,需要重新计算 RichTextBox 控件的宽度。问题是它总是重新计算错误。我使用正确的公式将点转换为英寸,然后将英寸转换为像素,然后将像素乘以字符数以获得宽度。下面是代码:
foreach (RichTextBox r in panel1.Controls)
{
if (r.DisplayRectangle.IntersectsWith(contextMenuStrip1.DisplayRectangle))
{
Graphics gfx = this.CreateGraphics();
float dpix = gfx.DpiX;
float dpiy = gfx.DpiY;
int numofchars = r.TextLength;
float point = r.Font.SizeInPoints;
float inches = point / 72;
float pixels = dpiy * inches;
float width = (pixels * numofchars);
r.Width = Convert.ToInt32(width);
}
}
I am creating this program that requires the recalculation of a RichTextBox control's width. The problem is that it keeps recalculating wrong. I used the correct formulas to convert point to inches, then inches to pixels and then pixels times number of characters to get the width. Here is the code below:
foreach (RichTextBox r in panel1.Controls)
{
if (r.DisplayRectangle.IntersectsWith(contextMenuStrip1.DisplayRectangle))
{
Graphics gfx = this.CreateGraphics();
float dpix = gfx.DpiX;
float dpiy = gfx.DpiY;
int numofchars = r.TextLength;
float point = r.Font.SizeInPoints;
float inches = point / 72;
float pixels = dpiy * inches;
float width = (pixels * numofchars);
r.Width = Convert.ToInt32(width);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过研究 Graphics.MeasureString 方法?
Have you tried looking into the Graphics.MeasureString Method?