当前位置: 首页>>代码示例>>C#>>正文


C# Font.GetStringSize方法代码示例

本文整理汇总了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));
        }
开发者ID:CHiiLD,项目名称:net-toolkit,代码行数:59,代码来源:GraphicsExtensions.cs


注:本文中的System.Drawing.Font.GetStringSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。