public static Size GetTextSize(Graphics graphics, string text, Font font, ref Rectangle rc, 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 = WindowsAPI.GetDC(IntPtr.Zero); } IntPtr fontHandle = font.ToHfont(); IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle); Win32.RECT rect = new Win32.RECT(); rect.left = rc.Left; rect.right = rc.Right; rect.top = rc.Top; rect.bottom = rc.Bottom; WindowsAPI.DrawText(hdc, text, text.Length, ref rect, (int)drawFlags); WindowsAPI.SelectObject(hdc, currentFontHandle); WindowsAPI.DeleteObject(fontHandle); if ( graphics != null ) graphics.ReleaseHdc(hdc); else WindowsAPI.ReleaseDC(IntPtr.Zero, hdc); return new Size(rect.right - rect.left, rect.bottom - rect.top); }
private DrawTextFormatFlags GetTextFormatFlags() { DrawTextFormatFlags flags = 0; if (!_useMnemonic) { flags |= DrawTextFormatFlags.DT_NOPREFIX; } if (_wordWrap) { flags |= DrawTextFormatFlags.DT_WORDBREAK; } if (_endEllipsis) { flags |= DrawTextFormatFlags.DT_END_ELLIPSIS | DrawTextFormatFlags.DT_SINGLELINE; } if (_textAlign != ContentAlignment.TopLeft) { flags |= DrawTextFormatFlags.DT_SINGLELINE; } switch (_textAlign) { case ContentAlignment.TopCenter: flags |= DrawTextFormatFlags.DT_CENTER; break; case ContentAlignment.TopRight: flags |= DrawTextFormatFlags.DT_RIGHT; break; case ContentAlignment.MiddleLeft: flags |= DrawTextFormatFlags.DT_VCENTER; break; case ContentAlignment.MiddleCenter: flags |= DrawTextFormatFlags.DT_CENTER | DrawTextFormatFlags.DT_VCENTER; break; case ContentAlignment.MiddleRight: flags |= DrawTextFormatFlags.DT_VCENTER | DrawTextFormatFlags.DT_RIGHT; break; case ContentAlignment.BottomLeft: flags |= DrawTextFormatFlags.DT_BOTTOM; break; case ContentAlignment.BottomCenter: flags |= DrawTextFormatFlags.DT_BOTTOM | DrawTextFormatFlags.DT_CENTER; break; case ContentAlignment.BottomRight: flags |= DrawTextFormatFlags.DT_BOTTOM | DrawTextFormatFlags.DT_RIGHT; break; } return(flags); }
public static Size GetTextSize( Graphics g, string text, Font font, ref Rectangle rc, DrawTextFormatFlags drawFlags ) { IntPtr hdc = GetGraphicsHDC( g ); IntPtr hFont = font.ToHfont(); IntPtr hOldFont = WindowsAPI.SelectObject( hdc, hFont ); Win32.RECT rect = new Win32.RECT(); rect.left = rc.Left; rect.right = rc.Right; rect.top = rc.Top; rect.bottom = rc.Bottom; WindowsAPI.DrawText( hdc, text, text.Length, ref rect, (int)drawFlags ); WindowsAPI.SelectObject( hdc, hOldFont ); WindowsAPI.DeleteObject( hFont ); ReleaseGraphicHDC( g, hdc ); return ( Size )rect; }
public static Size GetTextSize(Graphics graphics, string text, Font font, ref Rectangle rc, 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 = WindowsAPI.GetDC(IntPtr.Zero); } IntPtr fontHandle = font.ToHfont(); IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle); Win32.RECT rect = new Win32.RECT(); rect.left = rc.Left; rect.right = rc.Right; rect.top = rc.Top; rect.bottom = rc.Bottom; WindowsAPI.DrawText(hdc, text, text.Length, ref rect, (int)drawFlags); WindowsAPI.SelectObject(hdc, currentFontHandle); WindowsAPI.DeleteObject(fontHandle); if (graphics != null) { graphics.ReleaseHdc(hdc); } else { WindowsAPI.ReleaseDC(IntPtr.Zero, hdc); } return(new Size(rect.right - rect.left, rect.bottom - rect.top)); }
public static Size GetTextSize(Graphics graphics, string text, Font font, ref Rectangle rc, DrawTextFormatFlags drawFlags) { IntPtr hdc = graphics.GetHdc(); IntPtr fontHandle = font.ToHfont(); IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle); RECT rect = new RECT(); rect.left = rc.Left; rect.right = rc.Right; rect.top = rc.Top; rect.bottom = rc.Bottom; WindowsAPI.DrawText(hdc, text, text.Length, ref rect, (int) drawFlags); WindowsAPI.SelectObject(hdc, currentFontHandle); WindowsAPI.DeleteObject(fontHandle); graphics.ReleaseHdc(hdc); return new Size(rect.right - rect.left, rect.bottom - rect.top); }
public int DrawText(Graphics g, string text, Font font, Color color, Rectangle rc, StringFormat format) { int height; RectangleF rcClip = g.ClipBounds; RECT rect = new RECT(rc.Left, rc.Top, rc.Right, rc.Bottom); IntPtr hdc = g.GetHdc(); try { IntPtr clipRgn = Win32Declarations.CreateRectRgn(0, 0, 0, 0); if (Win32Declarations.GetClipRgn(hdc, clipRgn) != 1) { Win32Declarations.DeleteObject(clipRgn); clipRgn = IntPtr.Zero; } Win32Declarations.IntersectClipRect(hdc, (int)rcClip.Left, (int)rcClip.Top, (int)rcClip.Right, (int)rcClip.Bottom); IntPtr hFont = _fontCache.GetHFont(font); IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont); int textColor = Win32Declarations.ColorToRGB(color); int oldColor = Win32Declarations.SetTextColor(hdc, textColor); BackgroundMode oldMode = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT); DrawTextFormatFlags flags = 0; if ((format.FormatFlags & StringFormatFlags.NoWrap) != 0) { flags |= DrawTextFormatFlags.DT_SINGLELINE; } else { flags |= DrawTextFormatFlags.DT_WORDBREAK; } if (format.Alignment == StringAlignment.Center) { flags |= DrawTextFormatFlags.DT_CENTER; } else if (format.Alignment == StringAlignment.Far) { flags |= DrawTextFormatFlags.DT_RIGHT; } if (format.LineAlignment == StringAlignment.Center) { flags |= DrawTextFormatFlags.DT_VCENTER; } if (format.Trimming == StringTrimming.EllipsisCharacter) { flags |= DrawTextFormatFlags.DT_END_ELLIPSIS; } if (format.HotkeyPrefix == HotkeyPrefix.None) { flags |= DrawTextFormatFlags.DT_NOPREFIX; } height = Win32Declarations.DrawText(hdc, text, text.Length, ref rect, flags); Win32Declarations.SelectClipRgn(hdc, clipRgn); Win32Declarations.DeleteObject(clipRgn); Win32Declarations.SetBkMode(hdc, oldMode); Win32Declarations.SetTextColor(hdc, oldColor); Win32Declarations.SelectObject(hdc, oldFont); } finally { g.ReleaseHdc(hdc); } return(height); }
public extern static int DrawText(IntPtr hdc, string lpString, int nCount, ref RECT lpRect, DrawTextFormatFlags flags);
/// <summary> /// Renders some text to a graphics device. /// </summary> /// <param name="graphics">Graphics device to render the text into.</param> /// <param name="text">Text to render.</param> /// <param name="rect">Bounding rectangle for the text to fit into.</param> /// <param name="font">Font in which the text is rendered.</param> /// <param name="color">Text color.</param> /// <param name="dtf">Formatting flags.</param> public static void DrawText(Graphics graphics, string text, Rectangle rect, Font font, Color color, DrawTextFormatFlags dtf) { IntPtr hdc = graphics.GetHdc(); try { // Font IntPtr hFont = _fontCache.GetHFont(font); IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont); // Bounding rectangle RECT rc = new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom); // Color int textColor = Win32Declarations.ColorToRGB(color); int oldColor = Win32Declarations.SetTextColor(hdc, textColor); BackgroundMode oldMode = Win32Declarations.SetBkMode(hdc, BackgroundMode.TRANSPARENT); // Render the text Win32Declarations.DrawText(hdc, text, text.Length, ref rc, dtf); // Do deinit Win32Declarations.SetBkMode(hdc, oldMode); Win32Declarations.SetTextColor(hdc, oldColor); Win32Declarations.SelectObject(hdc, oldFont); } finally { graphics.ReleaseHdc(hdc); } }
/// <summary> /// Calculates the rectangle needed for rendering the text string. /// </summary> /// <param name="control">Control to which the text will be rendered. /// It's used for getting the device context and setting the initial text bounds /// (the latter is ignored in the single-line case).</param> /// <param name="text">Text to be rendered.</param> /// <param name="font">Font in which the text will be rendered.</param> /// <param name="dtf">Additional parameters that control rendering of the text</param> /// <returns>Size of the text's bounding rectangle.</returns> /// <remarks>The initial size is the control size.</remarks> public static Size GetTextSize(Control control, string text, Font font, DrawTextFormatFlags dtf) { return(GetTextSize(control, text, font, new Size(Int32.MaxValue, control.Height), dtf)); }
/// <summary> /// Calculates the rectangle needed for rendering the text string. /// </summary> /// <param name="control">Control to which the text will be rendered. /// It's used for getting the device context and setting the initial text bounds /// (the latter is ignored in the single-line case).</param> /// <param name="text">Text to be rendered.</param> /// <param name="font">Font in which the text will be rendered.</param> /// <param name="bounds">Initial size that should be expanded to fit the text.</param> /// <param name="dtf">Additional parameters that control rendering of the text.</param> /// <returns>Size of the text's bounding rectangle.</returns> public static Size GetTextSize(Control control, string text, Font font, Size bounds, DrawTextFormatFlags dtf) { using (Graphics g = control.CreateGraphics()) { IntPtr hdc = g.GetHdc(); try { IntPtr hFont = _fontCache.GetHFont(font); IntPtr oldFont = Win32Declarations.SelectObject(hdc, hFont); RECT rc = new RECT(0, 0, bounds.Width, bounds.Height); Win32Declarations.DrawText(hdc, text, text.Length, ref rc, dtf | DrawTextFormatFlags.DT_CALCRECT); int height = rc.bottom - rc.top; //height += 1; Size sz = new Size(rc.right - rc.left, height); Win32Declarations.SelectObject(hdc, oldFont); return(sz); } finally { g.ReleaseHdc(hdc); } } }
public static int DrawText(IntPtr hdc, string lpString, int nCount, ref Rectangle lpRect, DrawTextFormatFlags uFormat) { return(User32Methods.DrawText(hdc, lpString, nCount, ref lpRect, (uint)uFormat)); }
public static Size GetTextSize(Graphics g, string text, Font font, ref Rectangle rc, DrawTextFormatFlags drawFlags) { IntPtr hdc = GetGraphicsHDC(g); IntPtr hFont = font.ToHfont(); IntPtr hOldFont = WindowsAPI.SelectObject(hdc, hFont); NWin32.RECT rect = new NWin32.RECT(); rect.left = rc.Left; rect.right = rc.Right; rect.top = rc.Top; rect.bottom = rc.Bottom; WindowsAPI.DrawText(hdc, text, text.Length, ref rect, (int)drawFlags); WindowsAPI.SelectObject(hdc, hOldFont); WindowsAPI.DeleteObject(hFont); ReleaseGraphicHDC(g, hdc); return(( Size )rect); }