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.Cre开发者_JS百科ateGraphics();
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);
}
}
Have you tried looking into the Graphics.MeasureString Method?
精彩评论