在窗体中计算要显示的字符串的长度

以计算Label中Text属性的字体长度(为piexl为单位)的示例代码如下:

// title  controls initial
Label label_Title_MSG = new Label();
label_Title_MSG.Name = "label_Title_MSG";
label_Title_MSG.Text = "选取要显示的潮位站:";
label_Title_MSG.Top = 5;
label_Title_MSG.Left = 10;
label_Title_MSG.Height = 12;
// 计算字符串的长度
Graphics g = this.m_mainForm.CreateGraphics();
SizeF vSizeF = g.MeasureString(label_Title_MSG.Text, label_Title_MSG.Font);
label_Title_MSG.Width = Convert.ToInt32(Math.Ceiling(vSizeF.Width));
label_Title_MSG.ForeColor = Color.Black;
// label_Title_MSG.BackColor = Color.FromArgb(255, 150, 150, 150);// alpha约大透明度越高
this.AddControls(label_Title_MSG);    

 

posted @ 2016-10-10 09:17  wenglabs  阅读(432)  评论(0编辑  收藏  举报