MeasureString 总是认为空格适合

问题描述 投票:0回答:2

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

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

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;
    }
}
c# .net winforms gdi+ gdi
2个回答
2
投票

问题在于“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);
    }

0
投票

我真的不喜欢这个解决方案,但到目前为止它是我唯一有效的解决方案。它基于 @FSDaniel 的解决方案,但我注意到,即使您将相同的

Graphics.MeasureString
传递给它们,
StringFormat
和 Graphics.DrawString 的结果也会漂移。

我使用

TextRenderer
TextFormatFlags.TextBoxControl
标志集成功实现的唯一一致测量。

这是一个令人讨厌的解决方案,可以通过寻找结果来稍微改进(性能方面),即从字符串的中间开始,如果它太大,请尝试字符串的 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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.