MeasureString总是认为空白适合

时间:2016-12-01 12:10:07

标签: c# .net winforms gdi+ gdi

我正在尝试对字符串进行一些命中测试(我想从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的值应该设置为它可以适合的大小的字符数(我给它一个基于我试图测试的点的大小)。

这个工作正常,直到该区域足够大以容纳'test'字符串。此时charFitted从3('tes')跳到8('test')。它基本上总是包括所有空格,无论它给出的空间如何。

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

我已经包含了一个演示此

的测试应用程序

enter image description here enter image description here

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

2 个答案:

答案 0 :(得分:2)

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

将你的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);
    }

答案 1 :(得分:0)

我真的不喜欢这个解决方案,但到目前为止它是我唯一的解决方案。它基于@FSDaniel的解决方案,但我注意到了Graphics.MeasureString&amp;的结果。即使你将相同的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);
    }