public static Size GetTextSize(Graphics graphics, string text, Font font, ref Rectangle rc, APIsEnums.DrawTextFormatFlags drawFlags) { IntPtr hdc = IntPtr.Zero; if (graphics != null) { // Get device context from the graphics passed in hdc = graphics.GetHdc(); } else { // Get screen device context hdc = APIsGdi.GetDC(IntPtr.Zero); } IntPtr fontHandle = font.ToHfont(); IntPtr currentFontHandle = APIsGdi.SelectObject(hdc, fontHandle); APIsStructs.RECT rect = new APIsStructs.RECT(); rect.left = rc.Left; rect.right = rc.Right; rect.top = rc.Top; rect.bottom = rc.Bottom; APIsUser32.DrawText(hdc, text, text.Length, ref rect, drawFlags); APIsGdi.SelectObject(hdc, currentFontHandle); APIsGdi.DeleteObject(fontHandle); if (graphics != null) { graphics.ReleaseHdc(hdc); } else { APIsUser32.ReleaseDC(IntPtr.Zero, hdc); } return(new Size(rect.right - rect.left, rect.bottom - rect.top)); }
public extern static int DrawText(IntPtr hdc, string lpString, int nCount, ref APIsStructs.RECT lpRect, APIsEnums.DrawTextFormatFlags flags);