C#Winform中打印預覽時設置橫向打印


因項目需要,在做一個Screen的打印時,因為Screen中的DataGridView的表格很長,需要橫向排列才能完整的打印,因此設置PrintDocument.PrinterSettings.DefaultPageSettings.Landscape = true。但是在預覽的時候仍然是縱向排列。傷腦筋。。。
花了一些時間做其他的設置均未有效。用Reflector查看PrintController的PrintLoop方法,PrintDocument的打印相關的方法被調用的順序是這樣的:
QueryPageSettings
StartPage
PrintPage
EndPage
目前只處理了StartPage和PrintPage。
於是加上了QueryPageSettings事件的處理,發現每次執行打印時QueryPageSettingsEventArgs.PageSettings.Landscape仍然為false,雖然前面設置了DefaultpageSettings.Landscape=true。
於是在此事件中將其指定為true,再次預覽時,終於可以橫向看到排列整齊的報表了。
private  void  button2_Click( object  sender, EventArgs e)
{
     PrintPreviewDialog pd =  new  PrintPreviewDialog();
     pd.Document =  new  PrintDocument();
     pd.Document.PrintPage +=  new  PrintPageEventHandler(Document_PrintPage);
     pd.Document.QueryPageSettings +=  new  QueryPageSettingsEventHandler(Document_QueryPageSettings);
     pd.Document.BeginPrint +=  new  PrintEventHandler(Document_BeginPrint);
     if  (pd.ShowDialog( this ) == DialogResult.OK)
     {
     }
}
  
void  Document_QueryPageSettings( object  sender, QueryPageSettingsEventArgs e)
{
     e.PageSettings.Landscape =  true ;
     int  index = -1;
     for  ( int  i=0;i<e.PageSettings.PrinterSettings.PaperSizes.Count;i++)
     {
         if  (e.PageSettings.PrinterSettings.PaperSizes[i].PaperName==  "A4" )
         {
             index=i;
             break ;
         }
     }
     if  (index != -1)
     {
         e.PageSettings.PaperSize = e.PageSettings.PrinterSettings.PaperSizes[index];
     }
}
  
void  Document_BeginPrint( object  sender, PrintEventArgs e)
{
}
  
void  Document_PrintPage( object  sender, PrintPageEventArgs e)
{
     using  (Bitmap bit =  new  Bitmap( this .listBox.Width,  this .listBox.Height))
     {
         this .listBox.DrawToBitmap(bit,  this .listBox.ClientRectangle);
         e.Graphics.DrawImage(bit,  new  Point(0, 0));
     }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM