protected static void DrawText(Graphics g, Rectangle rect, string text, ContentAlignmentEx alignment, bool multiline, Font font, Color color, Color shadow, ShadowMask mask) { if (multiline == false) { var rc = alignment.CalcTextRect(g.MeasureString(text, font), rect); if (mask != ShadowMask.None) { g.DrawShadow(text, font, shadow, rc.X, rc.Y, mask); } using (var brush = new SolidBrush(color)) { g.DrawString(text, font, brush, rc.X, rc.Y); } } else { var texts = g.GetMultilineText(text, font, rect.Width); var textSize = g.CalcMultilineTextSize(texts, font); var rc = alignment.CalcTextRect(textSize, rect); using (var brush = new SolidBrush(color)) { var top = rc.Top; foreach (var line in texts) { var size = g.MeasureString(String.IsNullOrEmpty(line) ? " " : line, font); var rcLine = alignment.CalcTextRect(size, rc.Left, top, rc.Width, size.Height); if (mask != ShadowMask.None) { g.DrawShadow(text, font, shadow, rcLine.X, rcLine.Y, mask); } g.DrawString(line, font, brush, rcLine.X, rcLine.Y); top += rcLine.Height; } } } }
//-------------------------------------------------------------------------------- // Calc //-------------------------------------------------------------------------------- /// <summary> /// /// </summary> /// <param name="textAlign"></param> /// <param name="sx"></param> /// <param name="sy"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static RectangleF CalcTextRect(this ContentAlignmentEx textAlign, float sx, float sy, float x, float y, float width, float height) { if (textAlign.IsCenter()) { x += (width - sx) / 2; } else if (textAlign.IsRight()) { x += width - sx; } if (textAlign.IsMiddle()) { y += (height - sy) / 2; } else if (textAlign.IsBottom()) { y += height - sy; } return(new RectangleF(x, y, sx, sy)); }
/// <summary> /// /// </summary> /// <param name="alignment"></param> /// <returns></returns> public static bool IsLeft(this ContentAlignmentEx alignment) { return((alignment == ContentAlignmentEx.TopLeft) || (alignment == ContentAlignmentEx.MiddleLeft) || (alignment == ContentAlignmentEx.BottomLeft)); }
/// <summary> /// /// </summary> /// <param name="alignment"></param> /// <returns></returns> public static bool IsBottom(this ContentAlignmentEx alignment) { return((alignment == ContentAlignmentEx.BottomLeft) || (alignment == ContentAlignmentEx.BottomCenter) || (alignment == ContentAlignmentEx.BottomRight)); }
/// <summary> /// /// </summary> /// <param name="alignment"></param> /// <returns></returns> public static bool IsMiddle(this ContentAlignmentEx alignment) { return((alignment == ContentAlignmentEx.MiddleLeft) || (alignment == ContentAlignmentEx.MiddleCenter) || (alignment == ContentAlignmentEx.MiddleRight)); }
//-------------------------------------------------------------------------------- // Alignment //-------------------------------------------------------------------------------- /// <summary> /// /// </summary> /// <param name="alignment"></param> /// <returns></returns> public static bool IsTop(this ContentAlignmentEx alignment) { return((alignment == ContentAlignmentEx.TopLeft) || (alignment == ContentAlignmentEx.TopCenter) || (alignment == ContentAlignmentEx.TopRight)); }
/// <summary> /// /// </summary> /// <param name="textAlign"></param> /// <param name="sizeText"></param> /// <param name="rect"></param> /// <returns></returns> public static RectangleF CalcTextRect(this ContentAlignmentEx textAlign, SizeF sizeText, RectangleF rect) { return(textAlign.CalcTextRect(sizeText.Width, sizeText.Height, rect.X, rect.Y, rect.Width, rect.Height)); }
/// <summary> /// /// </summary> /// <param name="textAlign"></param> /// <param name="sizeText"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static RectangleF CalcTextRect(this ContentAlignmentEx textAlign, SizeF sizeText, float x, float y, float width, float height) { return(textAlign.CalcTextRect(sizeText.Width, sizeText.Height, x, y, width, height)); }
/// <summary> /// /// </summary> /// <param name="g"></param> /// <param name="text"></param> /// <param name="font"></param> /// <param name="color"></param> /// <param name="rect"></param> /// <param name="textAlign"></param> public static void DrawText(this Graphics g, string text, Font font, Color color, RectangleF rect, ContentAlignmentEx textAlign) { DrawText(g, text, font, color, rect.Left, rect.Top, rect.Width, rect.Height, textAlign); }
public static void DrawText(this Graphics g, string text, Font font, Color color, float x, float y, float width, float height, ContentAlignmentEx textAlign) { var rect = textAlign.CalcTextRect(g.MeasureString(text, font), x, y, width, height); using (var brush = new SolidBrush(color)) { g.DrawString(text, font, brush, rect.X, rect.Y); } }
public static void DrawShadow(this Graphics g, string text, Font font, Color color, RectangleF rect, ContentAlignmentEx textAlign, ShadowMask mask) { var rc = textAlign.CalcTextRect(g.MeasureString(text, font), rect); DrawShadow(g, text, font, color, rc.X, rc.Y, mask); }
/// <summary> /// /// </summary> /// <param name="alignment"></param> /// <returns></returns> public static bool IsCenter(this ContentAlignmentEx alignment) { return((alignment == ContentAlignmentEx.TopCenter) || (alignment == ContentAlignmentEx.MiddleCenter) || (alignment == ContentAlignmentEx.BottomCenter)); }
/// <summary> /// /// </summary> /// <param name="textAlign"></param> /// <param name="sx"></param> /// <param name="sy"></param> /// <param name="rect"></param> /// <returns></returns> public static RectangleF CalcTextRect(this ContentAlignmentEx textAlign, float sx, float sy, RectangleF rect) { return(textAlign.CalcTextRect(sx, sy, rect.X, rect.Y, rect.Width, rect.Height)); }
/// <summary> /// /// </summary> /// <param name="alignment"></param> /// <returns></returns> public static bool IsRight(this ContentAlignmentEx alignment) { return((alignment == ContentAlignmentEx.TopRight) || (alignment == ContentAlignmentEx.MiddleRight) || (alignment == ContentAlignmentEx.BottomRight)); }