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;
}
[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;
}