VSTO Outlook插件:MeasureString计算出错误的宽度

问题描述 投票:0回答:1

我想显示带有国际化标签的按钮。用英语,标签完全适合按钮的宽度。用德语不适合。为了动态计算我使用的宽度[...]

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)(在调试器中选中)。

.net vsto outlook-addin system.drawing.graphics
1个回答
0
投票

不需要使用基础的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";
   }
© www.soinside.com 2019 - 2024. All rights reserved.