我想显示带有国际化标签的按钮。用英语,标签完全适合按钮的宽度。用德语不适合。为了动态计算我使用的宽度[...]
using (Graphics cg = this.CreateGraphics())
{
SizeF size = cg.MeasureString(OutlookAddIn4.Resources.Resources.ShowLoginText, this.showLogFile.Font);
this.showLogFile.Width = (int)size.Width+3;
}
[...]
对于OutlookAddIn4.Resources.Resources.ShowLoginText =“ Logfile anzeigen”中的字符串。在计算125个像素。我什至添加了3个像素。按钮的填充设置为0。为什么会计算错误的宽度? this.showLogFile.Font设置为正确的字体(Microsoft Sans Serif,12 pt)(在调试器中选中)。
不需要使用基础的Graphics
对象。
如果将按钮的AutoSize
属性设置为true,将AutoSizeMode
属性设置为GrowAndShrink
,并且将AutoEllipsis
属性设置为false,它将自动调整大小以适合文本。
但是如果您确实需要使用Graphics
对象:
using(Graphics cg = this.CreateGraphics())
{
SizeF size = cg.MeasureString("Your text goes here",this.button1.Font);
this.button1.Padding = 3;
this.button1.Width = (int)size.Width;
this.button1.Text = "Your text goes here";
}