|
Posted on
2009-04-06 22:01
Leo.W
阅读( 977)
评论()
编辑
收藏
举报
 MeasureTextWidth
[DllImport("gdi32")]
private static extern bool GetTextExtentPoint32(IntPtr hdc, string lpString, int cbString, out Size lpSize);
[DllImport("gdi32")]
private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32")]
private static extern IntPtr DeleteObject(IntPtr hDC);
static Size MeasureTextWidth(Graphics graphics, string text, Font textFont)
 {
IntPtr hdc = graphics.GetHdc();
IntPtr hFont = textFont.ToHfont();
SelectObject(hdc, hFont);
Size size = new Size();
GetTextExtentPoint32(hdc, text, text.Length, out size);
graphics.ReleaseHdc();
DeleteObject(hFont);
return size;
}
|