【发布时间】: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. -
也许您可以用下划线替换所有空格来进行命中测试...
-
我不确定您是否可以假设空格和下划线在所有字体中占用相同的空间。我目前最好的解决方案是按原样运行代码,然后如果位置末尾有空格,我将开始删除它们测量字符串,直到它适合。还是不理想!