c# - 重新计算 RichTextBox 控件的宽度时遇到问题

标签 c# .net visual-studio-2010 richtextbox dpi

我正在创建这个需要重新计算 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);


            }
        }

最佳答案

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

关于c# - 重新计算 RichTextBox 控件的宽度时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5201298/

相关文章:

c# - 通过 TcpClient [C#] 的 HTTP(S) POST/GET 请求

c# - 如何在Windows Phone 7 中释放应用程序缓存?

visual-studio-2010 - 异常 HRESULT : 0x800700C1 when executing c# application referencing native c++ lib with boost

c++ - Visual Studio 中的富用户界面应用程序

c# - 从列表中 ID 所在的集合中选择全部

c# - 禁用保存文件对话框的文件名框

c# - 如何在 C# 中合并 2 个 List<T> 并从中删除重复值

c# - 无法引用 Microsoft.Device 命名空间

.net - 我应该从哪里下载CorFlags.exe?

c# - 如何创建临时文件来缓存库的启动工作?