Winform 需求要在label的文本结尾处放置一个(详细)按钮,文本长度还不固定

dahaig 2006-08-24 04:23:57
我怎么样获得文本的显示长度啊,英文和汉字还有符号的单个字符的显示宽度不一样啊
请高手帮忙
...全文
145 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
idda 2006-08-24
  • 打赏
  • 举报
回复
如果你是直接在屏幕上画就用渔翁的方法,测量文本的长度
label1.Text = "Show me the moneyShow me the money";
label1.AutoSize = true;
Font drawFont = label1.Font;
Graphics g = label1.CreateGraphics();

SizeF size =g.MeasureString(label1.Text,drawFont);
float lineWidth = size.Width;
MessageBox.Show(lineWidth.ToString());
但是如果你用label就不用那么麻烦了,而且你取出的文本长度也不lable的长度,直接得到label的label1.Width如下
Point location = new Point(label1.Left + label1.Width, label1.Top);
Button yourBut = new Button();
yourBut.Text = "详细";
this.Controls.Add(yourBut);
yourBut.Location = location;
如果想加上事件
yourBut.Click +=new EventHandler(yourBut_Click);

private void yourBut_Click(object sender, EventArgs e) {

}
Knight94 2006-08-24
  • 打赏
  • 举报
回复
通过Graphics.MeasureString方法,参看
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(
new Pen(Color.Red, 1),
0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(
measureString,
stringFont,
Brushes.Black,
new PointF(0, 0));

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧