2015-07-21 114 views
0

我有幾個帶有控件的WPF窗口。我想一個接一個地打印它們而不顯示它們。有沒有辦法做到這一點?WPF打印窗口不顯示它

這是印刷方法:

public void printItemList(string printerName) 
    { 
     printButton.Visibility = Visibility.Collapsed; 
     cancelButton.Visibility = Visibility.Collapsed; 
     this.Height = 1250; 
     this.Width = 815; 
     PrintDialog printDlg = new System.Windows.Controls.PrintDialog(); 
     printDlg.PrintQueue = new System.Printing.PrintQueue(new System.Printing.PrintServer(), printerName); 
     System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket); 

     //get scale of the print wrt to screen of WPF visual 
     double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth/this.ActualWidth, capabilities.PageImageableArea.ExtentHeight/
         this.ActualHeight); 

     //Transform the Visual to scale 
     this.LayoutTransform = new ScaleTransform(scale, scale); 

     //get the size of the printer page 
     Size sz = new Size(this.ActualWidth, this.ActualHeight); //(8.5 * 96.0, 11.0 * 96.0); 

     //update the layout of the visual to the printer page size. 
     this.Measure(sz); 
     this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); 

     //now print the visual to printer to fit on the one page. 
     printDlg.PrintVisual(this, String.Empty); 
    } 
+0

如同在應用程序運行時創建窗口的圖像(png/jpg/etc)一樣? –

+0

如何在顯示窗口時打印窗口?我想你正在創造一個形象? – user1859022

+0

我剛剛添加了我正在使用的打印方法。 – Steven

回答

0

移動窗外是關鍵,謝謝你的提示!

ItemListWindow il = new ItemListWindow(); 
il.Show(); 
var desktopWorkingArea = System.Windows.SystemParameters.WorkArea; 
il.Top = desktopWorkingArea.Bottom + 100; 
il.printItemList(printerComboBox.SelectedItem.ToString()); 
il.Close();