【发布时间】:2012-01-04 07:21:09
【问题描述】:
我在一个 winform 应用程序中有一个自定义标签。我跨线程更改标签的内容。 我打开一个后台线程来读取输入数据,并调用我的跨线程方法来使用以下代码更改标签内容:
// ... invoke a cross-thread method to reset progress label information
Set_ProgressInfo("Reading data from input data file ... inputData");
我的跨线程方法是:
public void Set_ProgressInfo(string text)
{
if (this.progressInfo.InvokeRequired)
{
this.progressInfo.BeginInvoke(new MethodInvoker(delegate()
{ Set_ProgressInfo(text); }));
}
else
{
this.progressInfo.Text = text;
this.progressInfo.Location = new System.Drawing.Point(55, 595);
this.progressInfo.ForeColor = System.Drawing.Color.AliceBlue;
this.progressInfo.Font = new System.Drawing.Font("Verdana", 10.0f,
System.Drawing.FontStyle.Bold);
// Auto size label to fit the text
// ... create a Graphics object for the label
using (var g_progressInfo = this.progressInfo.CreateGraphics())
{
// ... get the Size needed to accommodate the formatted text
Size preferredSize_progressInfo = g_progressInfo.MeasureString(
this.progressInfo.Text, this.progressInfo.Font).ToSize();
// ... pad the text and resize the label
this.progressInfo.ClientSize = new Size(
preferredSize_progressInfo.Width + (BSAGlobals.labelTextPadding),
preferredSize_progressInfo.Height + (BSAGlobals.labelTextPadding));
}
}
}
一切都很好,因为它应该,除了:
当我改变字体大小时
this.progressInfo.Font = new System.Drawing.Font("Verdana", 10.0f,
System.Drawing.FontStyle.Bold);
从 10.0f 到 8.0f,只有调用组件中字符串的“正在从输入数据文件读取数据...”部分
Set_ProgressInfo("Reading data from input data file ... inputData");
显示。由于某种原因,在较小的字体大小下无法正确计算大小。我在这里错过了什么吗?我已经为此苦苦挣扎了一段时间,根本看不出这是什么原因。任何帮助将不胜感激。谢谢。
【问题讨论】:
-
你没提,AutoSize=false?您是在设计器中还是在代码中更改字体大小?
-
我正在更改代码中的大小,但我没有在任何地方设置 AutoSize。我正在以编程方式做所有事情。
标签: c# winforms label font-size