本文整理汇总了C#中System.Drawing.Font.GetStringSize方法的典型用法代码示例。如果您正苦于以下问题:C# Font.GetStringSize方法的具体用法?C# Font.GetStringSize怎么用?C# Font.GetStringSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Font
的用法示例。
在下文中一共展示了Font.GetStringSize方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DrawString
/// <summary>
/// Draw a string with the given content alignment
/// </summary>
/// <param name="g"></param>
/// <param name="str">String to draw</param>
/// <param name="font">Font to use to draw this string</param>
/// <param name="brush">Brush to use to draw this string</param>
/// <param name="rect">Rectangle with the string (<b>no cutting!</b>)</param>
/// <param name="alignment">The alignment of string to the given rectangle</param>
public static void DrawString(this Graphics g, String str, Font font, Brush brush, RectangleF rect, ContentAlignment alignment)
{
SizeF size = font.GetStringSize(str);
float x = rect.X;
float y = rect.Y;
switch (alignment)
{
case ContentAlignment.BottomCenter:
x = GetAlignment(Alignment.Center, rect.X, rect.Width, size.Width);
y = GetAlignment(Alignment.RightBottom, rect.Y, rect.Height, size.Height);
break;
case ContentAlignment.BottomLeft:
x = GetAlignment(Alignment.LeftTop, rect.X, rect.Width, size.Width);
y = GetAlignment(Alignment.RightBottom, rect.Y, rect.Height, size.Height);
break;
case ContentAlignment.BottomRight:
x = GetAlignment(Alignment.RightBottom, rect.X, rect.Width, size.Width);
y = GetAlignment(Alignment.RightBottom, rect.Y, rect.Height, size.Height);
break;
case ContentAlignment.MiddleCenter:
x = GetAlignment(Alignment.Center, rect.X, rect.Width, size.Width);
y = GetAlignment(Alignment.Center, rect.Y, rect.Height, size.Height);
break;
case ContentAlignment.MiddleLeft:
x = GetAlignment(Alignment.LeftTop, rect.X, rect.Width, size.Width);
y = GetAlignment(Alignment.Center, rect.Y, rect.Height, size.Height);
break;
case ContentAlignment.MiddleRight:
x = GetAlignment(Alignment.RightBottom, rect.X, rect.Width, size.Width);
y = GetAlignment(Alignment.Center, rect.Y, rect.Height, size.Height);
break;
case ContentAlignment.TopCenter:
x = GetAlignment(Alignment.Center, rect.X, rect.Width, size.Width);
y = GetAlignment(Alignment.LeftTop, rect.Y, rect.Height, size.Height);
break;
case ContentAlignment.TopLeft:
x = GetAlignment(Alignment.LeftTop, rect.X, rect.Width, size.Width);
y = GetAlignment(Alignment.LeftTop, rect.Y, rect.Height, size.Height);
break;
case ContentAlignment.TopRight:
x = GetAlignment(Alignment.RightBottom, rect.X, rect.Width, size.Width);
y = GetAlignment(Alignment.LeftTop, rect.Y, rect.Height, size.Height);
break;
default:
throw new NotImplementedException();
}
g.DrawString(str, font, brush, new PointF(x, y));
}