protected int CalcGdiPreferredHeight(Graphics g)
{
IntPtr hdc = g.GetHdc();
int height = 0;
if (hdc != IntPtr.Zero)
{
IntPtr hFont = this.Font.ToHfont();
IntPtr oldFont = NativeMethods.SelectObject(hdc, hFont);
RECT rect = new RECT();
int width = this.Width - this.Padding.Right;
if (this.Image != null)
{
width -= 16 + this.Padding.Left;
}
rect.right = width;
NativeMethods.DrawText(hdc, this.Text, this.Text.Length, ref rect, DrawTextFlags.DT_CALCRECT | this.DrawTextFlags);
height = rect.bottom - rect.top;
NativeMethods.SelectObject(hdc, oldFont);
NativeMethods.DeleteObject(hFont);
}
else
{
height = this.CalcGdiPlusPreferredHeight(g);
}
g.ReleaseHdc(hdc);
return height;
}