简体   繁体   中英

Having Trouble Recalculate Width Of RichTextBox Control

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);


            }
        }

您是否尝试过查看Graphics.MeasureString方法?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM