문자열에 대한 적중 테스트를하려고합니다. (x 오프셋에서 char 인덱스를 가져 오려고합니다.)하지만 측정 문자열로 문제가 발생합니다. MeasureString은 항상 공백이 적합하다고 생각합니다.
이
은 본질적으로 내가 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 설정을 장난 해봤지만 아무것도 t의 도움을 보인다는 ...이
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()를 사용하십시오. –
나는 그것을 시도했지만 각각의 char에 대해 characterRange를 만들 필요가 있고, 32 개 이상의 문자 범위가 설정되면 OverflowException을 던진다. – Sprotty
젠장, 정말이야! 32 개 이상이'OverflowException'을 던집니다. –