Graphics.DrawString()的中心文本输出 [英] Center text output from Graphics.DrawString()

查看:148
本文介绍了Graphics.DrawString()的中心文本输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NETCF(Windows Mobile) Graphics 类和 DrawString()方法来呈现一个字符到屏幕上。

I'm using the .NETCF (Windows Mobile) Graphics class and the DrawString() method to render a single character to the screen.

问题是我似乎无法正确地居中。无论我为字符串渲染位置的Y坐标设置了什么,它总是低于此值,文本大小越大,Y偏移量越大。

The problem is that I can't seem to get it centered properly. No matter what I set for the Y coordinate of the location of the string render, it always comes out lower than that and the larger the text size the greater the Y offset.

例如,在文本大小为12的情况下,偏移大约为4,但在32处偏移大约为10。

For example, at text size 12, the offset is about 4, but at 32 the offset is about 10.

我希望角色垂直占据大部分矩形被绘制并水平居中。这是我的基本代码。 这个引用了它正在绘制的用户控件。

I want the character to vertically take up most of the rectangle it's being drawn in and be centered horizontally. Here's my basic code. this is referencing the user control it's being drawn in.

Graphics g = this.CreateGraphics();

float padx = ((float)this.Size.Width) * (0.05F);
float pady = ((float)this.Size.Height) * (0.05F);

float width = ((float)this.Size.Width) - 2 * padx;
float height = ((float)this.Size.Height) - 2 * pady;

float emSize = height;

g.DrawString(letter, new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular),
            new SolidBrush(Color.Black), padx, pady);

是的,我知道我可以使用标签控件,并将其设置为居中,但我确实需要使用 Graphics 类手动执行此操作。

Yes, I know there is the label control that I could use instead and set the centering with that, but I actually do need to do this manually with the Graphics class.

推荐答案

<

    private void DrawLetter()
    {
        Graphics g = this.CreateGraphics();

        float width = ((float)this.ClientRectangle.Width);
        float height = ((float)this.ClientRectangle.Width);

        float emSize = height;

        Font font = new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular);

        font = FindBestFitFont(g, letter.ToString(), font, this.ClientRectangle.Size);

        SizeF size = g.MeasureString(letter.ToString(), font);
        g.DrawString(letter, font, new SolidBrush(Color.Black), (width-size.Width)/2, 0);

    }

    private Font FindBestFitFont(Graphics g, String text, Font font, Size proposedSize)
    {
        // Compute actual size, shrink if needed
        while (true)
        {
            SizeF size = g.MeasureString(text, font);

            // It fits, back out
            if (size.Height <= proposedSize.Height &&
                 size.Width <= proposedSize.Width) { return font; }

            // Try a smaller font (90% of old size)
            Font oldFont = font;
            font = new Font(font.Name, (float)(font.Size * .9), font.Style);
            oldFont.Dispose();
        }
    }

到目前为止,这项工作完美无瑕。

So far, this works flawlessly.

我会改变的唯一方法是将FindBestFitFont()调用移动到OnResize()事件,以便我不会在每次绘制字母时调用它。只有在控件大小发生变化时才需要调用它。我只是将它包含在函数中以获得完整性。

The only thing I would change is to move the FindBestFitFont() call to the OnResize() event so that I'm not calling it every time I draw a letter. It only needs to be called when the control size changes. I just included it in the function for completeness.

这篇关于Graphics.DrawString()的中心文本输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆