简体   繁体   English

使用PrintDocument在Winform C#中打印所有控件

[英]Printing all controls in a winform c# using PrintDocument

I got a windows form which has more than one page of mainly labels and textboxes, I'm trying to keep the font that i have in the winform already, so far I'm able to print the first page, but when i try to add the rest of the controls it does all sort of weird stuff this is the part of my code where i'm putting everything to print but not all the controls in the panel show in the print preview. 我有一个Windows表单,其中有多个页面,主要是标签和文本框,我正在尝试保留Winform中已经拥有的字体,到目前为止,我已经能够打印第一页了,但是当我尝试添加其余控件,它会执行各种奇怪的操作,这是我的代码的一部分,我在其中打印所有内容,但并非面板中的所有控件都在打印预览中显示。 So i found out that the controls in the panel are not in order and what i need to do is create the number of printing pages first then put the controls in those printing pages. 因此,我发现面板中的控件顺序不正确,我需要做的是首先创建打印页数,然后将控件放入这些打印页中。 any help on trying to create the print pages first to add the controls to it. 首先尝试创建打印页面以向其添加控件的任何帮助。 it will always be 4 print pages. 它将始终是4个打印页面。

    int mainCount = 0;
    public void printStuff(System.Drawing.Printing.PrintPageEventArgs e)
    {            
        Font printFont = new Font("Arial", 9);
        int dgX = dataGridView1.Left;
        int dgY = dataGridView1.Top += 22;
        double linesPerPage = 0;
        float yPos = 0;
        int count = 0;

        float leftMargin = e.MarginBounds.Left;
        float topMargin = e.MarginBounds.Top;
        float bottomMargin = e.MarginBounds.Bottom;
        StringFormat str = new StringFormat();

        linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
        Control ctrl;

        while ((count < linesPerPage) && (panel1.Controls.Count != mainCount))           
        {
            ctrl = panel1.Controls[mainCount];
            yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
            mainCount++;
            count++;
            if (ctrl is Label)
            {
                e.Graphics.DrawString(ctrl.Text, printFont, Brushes.Black, ctrl.Left + 5, ctrl.Top + 40);
            }
            else if (ctrl is TextBox)
            {
                e.Graphics.DrawString(ctrl.Text, printFont, Brushes.Black, ctrl.Left + 5, ctrl.Top + 40);
                e.Graphics.DrawRectangle(Pens.Black, ctrl.Left, ctrl.Top + 40, ctrl.Width, ctrl.Height);
            }
        }
        if (count > linesPerPage)
        {
            e.HasMorePages = true;
        }
        else
        {
            e.HasMorePages = false;
        }            
    }

    //Print
    private void exportFileToolStripMenuItem_Click(object sender, EventArgs e)
    {            
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        printStuff(e);
    }

It seems to me that the problem is that on subsequent pages you are not subtracting the "page offset" form the control Top positions when printing. 在我看来,问题在于,在随后的页面上,您没有在打印时从控件的“顶部”位置中减去“页面偏移”。 You are essentially trying to use the screen coordinates of the controls when placing them on the printed page which obviously only works correctly for the first page. 当您将控件放置在打印的页面上时,您实质上是在尝试使用它们的屏幕坐标,这显然仅对第一页有效。 On subsequent pages you need to map the screen coordinates by subtracting a quantity which is the equivalent of the "total-printed-surface-so-far".. 在随后的页面上,您需要通过减去等于“到目前为止的总打印表面”数量来映射屏幕坐标。

You will want to modify this line for instance: 例如,您将需要修改以下行:

e.Graphics.DrawString(ctrl.Text, printFont, Brushes.Black, ctrl.Left + 5, ctrl.Top + 40);

to something like this: 像这样:

e.Graphics.DrawString(ctrl.Text, printFont, Brushes.Black, ctrl.Left + 5, ctrl.Top + 40 - pageOffset);

where the pageOffset is a variable that should be computed for each page, based on the Height of the printable area: pageOffset = currentPageNumber * heightOfPrintableArea so you will also need to maintain a variable for the number of pages printed, similar to the mainCount 其中pageOffset是应根据可打印区域的高度为每个页面计算的变量: pageOffset = currentPageNumber * heightOfPrintableArea因此您还需要为打印的页面数维护一个变量,类似于mainCount

Of course, the same would apply to the other branch of your if statement: 当然,这同样适用于if语句的另一个分支:

e.Graphics.DrawString(ctrl.Text, printFont, Brushes.Black, ctrl.Left + 5, ctrl.Top + 40 - pageOffset);
e.Graphics.DrawRectangle(Pens.Black, ctrl.Left, ctrl.Top + 40 - pageOffset, ctrl.Width, ctrl.Height);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM