在PrintDocument的宽度Graphics.DrawString中心 [英] Graphics.DrawString center in printdocument width

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

问题描述

我试图集中在一个PrintDocument的字符串。
我已经做了一个形象以下和它的作品,但似乎并没有工作,一个字符串是一样的。

I'm attempting to center a string on a printdocument. I've done the following with an image and it works but doesn't seem to work the same with a string.

下面是代码,我用于图像居中

Here is the code that I used to center the image

e.Graphics.DrawImage(logo, (e.MarginBounds.Width / 2) - (logo.Width / 2), height);



我试图中心的文字是正在一个TabControl从卡口

The text i'm trying to center is being supply from a Tab in a TabControl

 using (var sf = new StringFormat())
 {
       height = logo.Height + 15;
       sf.LineAlignment = StringAlignment.Center;
       sf.Alignment = StringAlignment.Center;
       e.Graphics.DrawString(tabData.Text, new Font(this.Font.Name, 10),
            new SolidBrush(tabData.ForeColor),
            (e.MarginBounds.Width / 2) - (txtData.Width / 2), height, sf);
  }



我也试过以下并用string_size.Width / 2的地方txtData的.WIDTH

I have also tried below and used string_size.Width /2 in place of txtData.Width

SizeF string_size = e.Graphics.MeasureString(tabData.Text, tabData.Font);

修改

EDIT

目前完整的代码

        float height = 0;
        tabData.Text = "Date Range: 02/02/2010 - 08/09/2013"; //set just for testing
        using (var logo = Properties.Resources.title)
        {
            e.Graphics.DrawImage(logo, e.PageBounds.Left + (e.MarginBounds.Width / 2) - (logo.Width / 2), height);
            height = logo.Height + 15;
        }

        using (var sf = new StringFormat())
        {

            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString(tabData.Text, new Font(this.Font.Name, 10), new SolidBrush(tabData.ForeColor), e.PageBounds.Left + (e.PageBounds.Width / 2), height, sf);
        }



不明白为什么我要使用PageBounds和MarginBounds的混合物中心的图像,然后用文字将与两个都MarginBounds或两个PageBounds中心

Don't understand why I have to use a mixture of PageBounds and MarginBounds to center the Image then with the text it will center with either both MarginBounds or both PageBounds

推荐答案

我下面的作品。您可能需要使用 PageBounds 如果你的利润率并不统一。

The following works for me. You may need to use PageBounds if your margins are not uniform.

    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        int w = e.MarginBounds.Width / 2;
        int x = e.MarginBounds.Left;
        int y = e.MarginBounds.Top;
        Font printFont = new Font("Arial", 10);
        Bitmap logo = System.Drawing.SystemIcons.WinLogo.ToBitmap();

        int height = 100 + y;
        string tabDataText = "Hello World";
        var tabDataForeColor = Color.Blue;
        var txtDataWidth = e.Graphics.MeasureString(tabDataText, printFont).Width;

        e.Graphics.DrawImage(logo,
            e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (logo.Width / 2),
            e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (logo.Height));

        using (var sf = new StringFormat())
        {
            height += logo.Height + 15;
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString(tabDataText, new Font(this.Font.Name, 10),
                 new SolidBrush(tabDataForeColor),
                 e.MarginBounds.Left + (e.MarginBounds.Width / 2),
                 e.MarginBounds.Top + (e.MarginBounds.Height / 2) + (logo.Height / 2) + 15,
                 sf);
        }

        e.HasMorePages = false;
    }

修改响应

输出使用新代码。你是说这是你想要的吗?

Output using your new code. Are you saying this is what you want?

或者,你想要这个?

Or are you wanting this?

保证金是一个矩形它位于内页。这可能是那些利润率是不对称的,所以如果你想要绝对的中心,你应该引用 PageBounds

The margin is a rectangle that sits inside the page. It is possible that those margins are asymmetrical so if you want the absolute center you should reference PageBounds.

此外,你的文字居中对齐,这样使得绘制文本字符串的中间,而不是左上喜欢的参考点标志

Additionally, your text is center aligned so that makes the reference point of drawing the text in the middle of the String instead of top left like the logo.

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

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