繁体   English   中英

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

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

我有一个Windows表单,其中有多个页面,主要是标签和文本框,我正在尝试保留Winform中已经拥有的字体,到目前为止,我已经能够打印第一页了,但是当我尝试添加其余控件,它会执行各种奇怪的操作,这是我的代码的一部分,我在其中打印所有内容,但并非面板中的所有控件都在打印预览中显示。 因此,我发现面板中的控件顺序不正确,我需要做的是首先创建打印页数,然后将控件放入这些打印页中。 首先尝试创建打印页面以向其添加控件的任何帮助。 它将始终是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);
    }

在我看来,问题在于,在随后的页面上,您没有在打印时从控件的“顶部”位置中减去“页面偏移”。 当您将控件放置在打印的页面上时,您实质上是在尝试使用它们的屏幕坐标,这显然仅对第一页有效。 在随后的页面上,您需要通过减去等于“到目前为止的总打印表面”数量来映射屏幕坐标。

例如,您将需要修改以下行:

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

像这样:

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

其中pageOffset是应根据可打印区域的高度为每个页面计算的变量: pageOffset = currentPageNumber * heightOfPrintableArea因此您还需要为打印的页面数维护一个变量,类似于mainCount

当然,这同样适用于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