MeasureString always thinks whitespace will fit

前端 未结 2 1495
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 22:22

I\'m trying to do some hit testing on strings (I want to get the char index from the x offset), but I\'m hitting issues with measure string.

This is essentially the cod

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-23 22:33

    I Really don't like this solution but so far its the only working solution I have. Its based on the solution from @FSDaniel, but I've noticed that the results from Graphics.MeasureString & Graphics.DrawString drift out even if you pass the same StringFormat to them.

    The only consistent measuring I've managed to achive using the TextRenderer with the TextFormatFlags.TextBoxControl flag set.

    This is a nasty solution which could be improved a little (performance wise) by hunting for a result, i.e. start in the middle of the string if its too big try 3/4 of the string, if thats to small try 5/8 etc until you have a result.

    This is far from the best solution, so if anyone has anything better, please post it!

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

提交回复
热议问题