MeasureString 始终认为空格适合

Posted

技术标签:

【中文标题】MeasureString 始终认为空格适合【英文标题】:MeasureString always thinks whitespace will fit 【发布时间】:2017-04-16 01:44:00 【问题描述】:

我正在尝试对字符串进行一些命中测试(我想从 x 偏移量获取 char 索引),但我遇到了度量字符串的问题。

这基本上是我正在使用的代码

     StringFormat sf = new StringFormat(StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap | StringFormatFlags.LineLimit);
     e.Graphics.MeasureString("test    string", this.Font, new SizeF(xHitTestPosition, this.Font.Height), sf, out charFitted, out linesFilled);

charFitted 的值应该设置为它可以容纳在它给定的大小内的字符数(我根据我要测试的点给它一个大小)。

这可以正常工作,直到该区域足够大以容纳“测试”字符串。此时 charFitted 从 3 ('tes') 跳到 8 ('test')。它基本上总是包含所有的空白,不管它已经给了多少空间。

我尝试过使用 StringFormat 设置,但似乎没有任何帮助...

我在下面添加了一个测试应用程序来演示这一点

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2

    public partial class Form1 : Form
    
        public Form1()
        
            InitializeComponent();
        

        private void Form1_Load(object sender, EventArgs e)
        
            trackBar1.Maximum = this.ClientRectangle.Width;
        

        protected override void OnPaint(PaintEventArgs e)
        
            string sample = "abc                       def";
            int charFitted, linesFilled;

            e.Graphics.DrawString(sample, this.Font, Brushes.Black, PointF.Empty);
            e.Graphics.DrawLine(Pens.Red, trackBar1.Value, 0, trackBar1.Value, 100);

            StringFormat sf = new StringFormat(StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap | StringFormatFlags.LineLimit);
            sf.Trimming = StringTrimming.Character;
            e.Graphics.MeasureString(sample, this.Font, new SizeF(trackBar1.Value, this.Font.Height), sf, out charFitted, out linesFilled);
            textBox1.Text = "[" + sample.Substring(0, charFitted) + "]";

            base.OnPaint(e);
        

        private void trackBar1_Scroll(object sender, EventArgs e)
        
            Invalidate();
        

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        
            if (disposing && (components != null))
            
                components.Dispose();
            
            base.Dispose(disposing);
        

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        
            this.trackBar1 = new System.Windows.Forms.TrackBar();
            this.textBox1 = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
            this.SuspendLayout();
            // 
            // trackBar1
            // 
            this.trackBar1.Location = new System.Drawing.Point(13, 184);
            this.trackBar1.Name = "trackBar1";
            this.trackBar1.Size = new System.Drawing.Size(259, 45);
            this.trackBar1.TabIndex = 0;
            this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(22, 229);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(237, 20);
            this.textBox1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.trackBar1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        

        #endregion

        private System.Windows.Forms.TrackBar trackBar1;
        private System.Windows.Forms.TextBox textBox1;
    

【问题讨论】:

它不是用于命中测试,而是使用 Graphics.MeasureCharacterRanges()。 我试过了,但我需要为每个字符创建一个 characterRange,如果设置了超过 32 个字符范围,它会抛出 OverflowException。 该死的,真的!超过 32 次投掷 OverflowException. 也许您可以用下划线替换所有空格来进行命中测试... 我不确定您是否可以假设空格和下划线在所有字体中占用相同的空间。我目前最好的解决方案是按原样运行代码,然后如果位置末尾有空格,我将开始删除它们测量字符串,直到它适合。还是不理想! 【参考方案1】:

问题是“abc”和“abc”的长度是一样的。由于您不打印尾随空格,因此图形表示是相同的。 我会在末尾添加一个字符,然后测量字符串并删除添加字符的长度。

将您的 OnPaint 切换到此,它似乎可以工作:

    protected override void OnPaint(PaintEventArgs e) 
        string sample = "abc                       def";

        e.Graphics.DrawString(sample, this.Font, Brushes.Black, PointF.Empty);
        e.Graphics.DrawLine(Pens.Red, trackBar1.Value, 0, trackBar1.Value, 100);

        StringFormat sf = new StringFormat(StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap | StringFormatFlags.LineLimit);
        sf.Trimming = StringTrimming.Character;

        var underscoreWidth = e.Graphics.MeasureString("_", this.Font).Width;

        for (int i = 0; i < sample.Length; i++) 
            var s = sample.Substring(0, i + 1) + "_";
            var size = e.Graphics.MeasureString(s, this.Font).Width - underscoreWidth;
            if (size > trackBar1.Value) 
                if (s.Length > 0) 
                    var ok = s.Substring(0, s.Length - 2);
                    textBox1.Text = "[" + ok + "]";
                    base.OnPaint(e);
                    return;
                
            
        

        textBox1.Text = "[" + sample + "]";
        base.OnPaint(e);
    

【讨论】:

【参考方案2】:

我真的不喜欢这个解决方案,但到目前为止,它是我拥有的唯一可行的解​​决方案。它基于@FSDaniel 的解决方案,但我注意到即使您将相同的 StringFormat 传递给它们,Graphics.MeasureString 和 Graphics.DrawString 的结果也会漂移。

使用设置了 TextFormatFlags.TextBoxControl 标志的 TextRenderer,我设法实现了唯一一致的测量。

这是一个令人讨厌的解决方案,可以通过寻找结果来稍微改进(性能方面),即如果字符串太大尝试字符串的 3/4,则从字符串中间开始,如果太小尝试 5 /8 等,直到你有结果。

这远不是最好的解决方案,所以如果有人有更好的解决方案,请发布!

    protected override void OnPaint(PaintEventArgs e)
    
        string sample = "abc                       defXXXXXXXXXXXXiiiiiiiX";

        TextFormatFlags flags = TextFormatFlags.NoPadding | TextFormatFlags.TextBoxControl | TextFormatFlags.SingleLine | TextFormatFlags.NoPrefix;

        TextRenderer.DrawText(e.Graphics, sample, this.Font, Point.Empty, Color.Black, flags);
        e.Graphics.DrawLine(Pens.Red, trackBar1.Value, 0, trackBar1.Value, 100);

        string measuredString = sample;
        for (int i = 0; i < sample.Length; i++)
        
            Size size = TextRenderer.MeasureText(e.Graphics, sample.Substring(0, i+1), this.Font, new Size(10000000, 1000000), flags);
            if (size.Width > trackBar1.Value)
            
                textBox1.Text = "[" + sample.Substring(0,i+1) + "]";
                break;
            
        

        base.OnPaint(e);
    

【讨论】:

以上是关于MeasureString 始终认为空格适合的主要内容,如果未能解决你的问题,请参考以下文章

C# WinForm,Graphics.MeasureString 计算不准确的坑

GDI+ - 为啥 MeasureString 函数使用 Courier 字体失败?

AUTOCAD——Measregeom测量工具

HM-10 iBeacon:为什么没有回答AT + MEAS命令?

div 始终位于页面顶部,随机图像自动适合该 div

ExtJs - 使文本区域始终适合父面板