2013-03-07 48 views
0

我得到了一个窗体,其中有多个主要标签和文本框的页面,我试图保留我已经在winform中的字体,到目前为止我能够打印的第一页,但是当我尝试添加其余的控件时,它做了各种奇怪的东西,这是我的代码的一部分,我把所有的东西打印,但并非面板中的所有控件显示打印预览。所以我发现面板上的控件没有按顺序,我需要做的是先创建打印页面的数量,然后将控件放在这些打印页面中。首先尝试创建打印页面以将控件添加到它的任何帮助。它将始终是4个打印页面。打印时打印WinForform中的所有控件c#使用PrintDocument

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); 
    } 
+0

和究竟是这些确实奇怪的事情? – 2013-03-07 16:53:28

+0

在第一页之后,它将整个页面上的其他控件打印出来,并且很多页面都是空白的。 – 2013-03-07 16:57:40

+0

该代码假定* mainCount *与应打印控件的页面有关。情况并非如此,控制的位置属性是重要的。还必须对页码进行调整。并且BeginPrint缺失将计数器重置为0.让表格不适合放在纸上也非常不寻常。 – 2013-03-07 17:51:47

回答

0

在我看来,问题是,在随后的页面你是不是减去“页偏移”形成的机顶位置。你基本上试图在把控件放在打印页面上时使用控件的屏幕坐标,这显然只适用于第一页。在随后的页面,你需要通过减去量这是“总印刷表面那么远”相当于屏幕坐标映射..

你将要修改这一行,例如:

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所以你还需要保持一个变量数字o ˚F页打印,类似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); 
+0

我会尝试,但我怎么会得到可打印区域的高度? – 2013-03-07 17:07:15

+0

您可以尝试使用e.MarginBounds.Height作为可打印区域的高度。看看是否产生你正在寻找的结果。正如我所说,使用另一个变量来跟踪你正在打印的页面,每次调用printStuff(..) – 2013-03-07 17:10:35