C# 测量信使字符宽度以填充列表框
我正在尝试编写一个方法,给出一个描述,一个标记将返回一个填充字符串,填充列表框中的一行。我原以为我现在的代码中可能有这种情况,但我注意到它高估了C# 测量信使字符宽度以填充列表框,c#,string,graphics,fonts,C#,String,Graphics,Fonts,我正在尝试编写一个方法,给出一个描述,一个标记将返回一个填充字符串,填充列表框中的一行。我原以为我现在的代码中可能有这种情况,但我注意到它高估了Graphics.MeasureString(…)中字符的宽度。经过一些挖掘,我发现MeasureString以PageUnits为单位返回其值,这是一个枚举 下面是我创建的一个SSCCE,以显示我的问题。我不确定如何将给定的PageUnit转换为像素。有什么想法吗 using System; using System.Collections.Gener
Graphics.MeasureString(…)
中字符的宽度。经过一些挖掘,我发现MeasureString
以PageUnits
为单位返回其值,这是一个枚举
下面是我创建的一个SSCCE,以显示我的问题。我不确定如何将给定的PageUnit
转换为像素。有什么想法吗
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 StringPadProj
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
theListBox.Items.Add(getPaddedStr("meow", "[Cat]"));
theListBox.Items.Add(getPaddedStr("woof", "[Dog]"));
theListBox.Items.Add(getPaddedStr("chirp", "[Bird]"));
theListBox.Items.Add(getPaddedStr("cricket", "[Cricket]"));
}
private String getPaddedStr(String desc, String tag)
{
int availSpacePx = theListBox.Width;
Console.Out.WriteLine("ListBoxW: " + availSpacePx);
Graphics e = theListBox.CreateGraphics();
e.PageUnit = GraphicsUnit.Pixel;
Font listBFont = theListBox.Font;
SizeF charSize = e.MeasureString("k", listBFont);
Console.Out.WriteLine("CharSize: " + charSize);
int numCharPerLine = (int)Math.Floor(availSpacePx / charSize.Width);
Console.Out.WriteLine("CharsPerLine: " + numCharPerLine);
int tagLen = tag.Length;
int spaceLeft = numCharPerLine - tagLen;
if (desc.Length > spaceLeft)
desc = desc.Substring(0, spaceLeft);
else if (desc.Length < spaceLeft)
while (desc.Length < spaceLeft)
desc += " ";
String itemText = desc + tag;
return itemText;
}
/// <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.theListBox = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// theListBox
//
this.theListBox.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.theListBox.FormattingEnabled = true;
this.theListBox.ItemHeight = 14;
this.theListBox.Location = new System.Drawing.Point(12, 12);
this.theListBox.Name = "theListBox";
this.theListBox.Size = new System.Drawing.Size(350, 172);
this.theListBox.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(374, 198);
this.Controls.Add(this.theListBox);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListBox theListBox;
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间字符串padproj
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
列表框.Items.Add(getPaddedStr(“喵”,“猫]”);
列表框.Items.Add(getPaddedStr(“woof”,“Dog]”);
添加(getPaddedStr(“chirp”,“Bird]”);
列表框.Items.Add(getPaddedStr(“cricket”,“cricket]”,“[cricket]”);
}
私有字符串getPaddedStr(字符串描述,字符串标记)
{
int availSpacePx=列表框的宽度;
Console.Out.WriteLine(“ListBoxW:+availSpacePx”);
Graphics e=listbox.CreateGraphics();
e、 PageUnit=GraphicsUnit.Pixel;
字体列表字体=列表框。字体;
SizeF charSize=e.MeasureString(“k”,列表字体);
Console.Out.WriteLine(“CharSize:+CharSize”);
int numCharPerLine=(int)数学地板(可用空间px/charSize.Width);
Console.Out.WriteLine(“CharsPerLine:+numCharPerLine”);
int tagLen=标记长度;
int spaceLeft=numCharPerLine-tagLen;
如果(描述长度>空格左)
desc=desc.Substring(0,空格左);
else if(描述长度<空格左)
while(描述长度<空格左)
desc+=“”;
字符串itemText=desc+标记;
返回itemText;
}
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
this.theListBox=new System.Windows.Forms.ListBox();
这个.SuspendLayout();
//
//登记箱
//
this.theListBox.Font=new System.Drawing.Font(“Courier new”,8.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((字节)(0));
this.theListBox.FormattingEnabled=true;
this.theListBox.ItemHeight=14;
this.theListBox.Location=新系统.图纸.点(12,12);
this.theListBox.Name=“theListBox”;
this.theListBox.Size=新系统.Drawing.Size(350172);
this.theListBox.TabIndex=0;
//
//表格1
//
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(374198);
this.Controls.Add(this.theListBox);
this.Name=“Form1”;
this.Text=“Form1”;
this.Load+=new System.EventHandler(this.Form1\u Load);
此选项为.resume布局(false);
}
#端区
private System.Windows.Forms.ListBox列表框;
}
}
编辑:
所以看来,MeasureString(…)
正在做它应该做的事情。据我所知,问题在于listbox.Width
值的单位。它们似乎不是用像素来衡量的。事实上,我找不到任何参考单位的确切用途的东西
所以现在的问题是,如何将Control.Width
的单位转换为/找到像素?编辑:新答案:
MSDN on一语道破:
MeasureString方法设计用于单个字符串
并在字符串前后包含少量额外空间
以允许悬垂图示符。此外,DrawString方法也会进行调整
glyph指向优化显示质量,并可能显示字符串
比测量报告的更窄。获取合适的度量标准
用于布局中的相邻字符串(例如,在实现
格式化文本),使用MeasureCharacterRanges方法或
采用StringFormat的MeasureString方法,并传递
泛型的。另外,请确保
图形是反别名
因此,将measure命令更改为类似这样的命令将获得适合于乘以的单个字符的度量值(当使用单空格字体时,就像您所做的那样):
嗯,好主意。但这会产生相同的结果假设
MeasureString(…)
未被破坏。可能ListBox.Width
不返回像素值。这是唯一有意义的东西。ListBox.Width是以像素为单位的,但也许使用ListBox.ClientRectangle.Width会更好?你能把我链接到s吗
SizeF charSize = e.MeasureString("k", listBFont,
PointF.Empty, StringFormat.GenericTypographic);