C#中怎样得到一个字符串的宽度和高度呢?有没有函数阿?

「已注销」 2010-08-06 05:24:11
如题,有没函数可以直接的倒字符串的宽度和高度呢?谢谢
...全文
235 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-08-06
  • 打赏
  • 举报
回复
像素
MeasureString 和MeasureCharacterRanges
string s="";
float w=0;
float h=0;
Graphics g=Graphics.FromHwnd(this.Handle);
Font myf=new Font( "宋体 ",15);
StringFormat sf=new StringFormat(StringFormat.GenericTypographic);
SizeF size=g.MeasureString(s,myf,1000,sf);
joeandlily 2010-08-06
  • 打赏
  • 举报
回复
MeasureString
兔子-顾问 2010-08-06
  • 打赏
  • 举报
回复
gdi下用

[DllImport("gdi32.dll", EntryPoint="GetTextMetrics")]
public static extern int GetTextMetrics (
int hdc,
ref TEXTMETRIC lpMetrics
);
兔子-顾问 2010-08-06
  • 打赏
  • 举报
回复
字符串的高度宽度要在某个GDI/GDI+环境下才有意义,比如当前窗体的:

using (Graphics g = this.CreateGraphics())
{
SizeF string_size = g.MeasureString("字符串", this.Font);
}
q107770540 2010-08-06
  • 打赏
  • 举报
回复

string s="aaaa";
int i=s.Length; //4

110,533

社区成员

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

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

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