public Size MeasureString(string text, int?maximumWidth, TextAlignment halign = TextAlignment.Left, VerticalTextAlignment valign = VerticalTextAlignment.Top, StringDrawingFlags flags = 0) { uint formatFlags = GDIConstants.DT_CALCRECT; switch (halign) { case TextAlignment.Left: formatFlags |= GDIConstants.DT_LEFT; break; case TextAlignment.Center: formatFlags |= GDIConstants.DT_CENTER; break; case TextAlignment.Right: formatFlags |= GDIConstants.DT_RIGHT; break; } switch (valign) { case VerticalTextAlignment.Top: break; // there is no DT_* constant for top alignment case VerticalTextAlignment.Center: formatFlags |= GDIConstants.DT_VCENTER; break; case VerticalTextAlignment.Bottom: formatFlags |= GDIConstants.DT_BOTTOM; break; } if (flags.HasFlag(StringDrawingFlags.AddPathEllipsis)) { formatFlags |= GDIConstants.DT_PATH_ELLIPSIS; } else if (flags.HasFlag(StringDrawingFlags.AddWordEllipsis)) { formatFlags |= GDIConstants.DT_WORD_ELLIPSIS; } if (flags.HasFlag(StringDrawingFlags.BreakOnWords)) { formatFlags |= GDIConstants.DT_WORDBREAK; } if (flags.HasFlag(StringDrawingFlags.ExpandTabCharacters)) { formatFlags |= GDIConstants.DT_EXPANDTABS; } if (flags.HasFlag(StringDrawingFlags.IgnoreAmpersands)) { formatFlags |= GDIConstants.DT_NOPREFIX; } Rect frame = new Rect(); frame.right = maximumWidth ?? int.MaxValue; NativeMethods.DrawText(Handle, text, -1, ref frame, formatFlags); return(new Size(frame.right, frame.bottom)); }
public int DrawText(string text, Rect frame, TextAlignment halign = TextAlignment.Left, VerticalTextAlignment valign = VerticalTextAlignment.Top, StringDrawingFlags flags = 0) { uint formatFlags = 0; switch (halign) { case TextAlignment.Left: formatFlags |= GDIConstants.DT_LEFT; break; case TextAlignment.Center: formatFlags |= GDIConstants.DT_CENTER; break; case TextAlignment.Right: formatFlags |= GDIConstants.DT_RIGHT; break; } switch (valign) { case VerticalTextAlignment.Top: break; // there is no DT_* value for top alignmrnt case VerticalTextAlignment.Center: formatFlags |= GDIConstants.DT_VCENTER; break; case VerticalTextAlignment.Bottom: formatFlags |= GDIConstants.DT_BOTTOM; break; } if (flags.HasFlag(StringDrawingFlags.AddPathEllipsis)) { formatFlags |= GDIConstants.DT_PATH_ELLIPSIS; } else if (flags.HasFlag(StringDrawingFlags.AddWordEllipsis)) { formatFlags |= GDIConstants.DT_WORD_ELLIPSIS; } if (flags.HasFlag(StringDrawingFlags.BreakOnWords)) { formatFlags |= GDIConstants.DT_WORDBREAK; } if (flags.HasFlag(StringDrawingFlags.ExpandTabCharacters)) { formatFlags |= GDIConstants.DT_EXPANDTABS; } if (flags.HasFlag(StringDrawingFlags.IgnoreAmpersands)) { formatFlags |= GDIConstants.DT_NOPREFIX; } return(NativeMethods.DrawText(Handle, text, -1, ref frame, formatFlags)); }
private static int GetGDIFlags(TextAlignment halign, VerticalTextAlignment valign, StringDrawingFlags flags) { int formatFlags = 0; switch (halign) { case TextAlignment.Left: formatFlags |= GDIConstants.DT_LEFT; break; case TextAlignment.Center: formatFlags |= GDIConstants.DT_CENTER; break; case TextAlignment.Right: formatFlags |= GDIConstants.DT_RIGHT; break; } switch (valign) { case VerticalTextAlignment.Top: break; // there is no DT_* value for top alignmrnt case VerticalTextAlignment.Center: formatFlags |= GDIConstants.DT_VCENTER; break; case VerticalTextAlignment.Bottom: formatFlags |= GDIConstants.DT_BOTTOM; break; } if (flags.HasFlag(StringDrawingFlags.AddPathEllipsis)) { formatFlags |= GDIConstants.DT_PATH_ELLIPSIS; } else if (flags.HasFlag(StringDrawingFlags.AddWordEllipsis)) { formatFlags |= GDIConstants.DT_WORD_ELLIPSIS; } if (flags.HasFlag(StringDrawingFlags.BreakOnWords)) { formatFlags |= GDIConstants.DT_WORDBREAK; } if (flags.HasFlag(StringDrawingFlags.ExpandTabCharacters)) { formatFlags |= GDIConstants.DT_EXPANDTABS; } if (flags.HasFlag(StringDrawingFlags.IgnoreAmpersands)) { formatFlags |= GDIConstants.DT_NOPREFIX; } return(formatFlags); }