2012-12-31 40 views
7

可能重複:
How can I take a screenshot of a Winforms control/form in C#?打印可滾動的窗體。

我有一個窗戶,名字和圖片的列表形式。名單很長,所以有一個滾動面板。現在,我想打印此表單,但是我無法打印,因爲打印功能只打印「可見」部分,因爲向下滾動時會看到不可見部分。那麼,有沒有辦法一次打印整個表格?

+3

這不是一個確切的重複數據刪除,但問題是基本相同。這裏沒有簡單的解決方案。你想要打印的東西實際上並不存在(因爲它沒有被繪製)。打印前,您必須滾動表格並拍攝多張圖像。只是打印數據可能會更容易,而不用擔心完全匹配表單。 –

+1

@JonB - 好吧,這類問題可以幫助我們找到這個經常性問題的一般答案。可能的重複似乎並不適用。 「這不可能」是迄今爲止唯一的答案嗎? –

回答

3

查找在Visual Basic的PowerPack工具箱

打印表單控件要打印滾動形式的完整的客戶區試中...

1.In工具箱中,單擊Visual Basic中的PowerPack選項卡,然後將PrintForm組件拖到窗體上。

PrintForm組件將被添加到組件托盤中。

2.在屬性窗口中,將PrintAction屬性設置爲PrintToPrinter。

3.在相應的事件處理程序中添加以下代碼(例如,在打印按鈕的Click事件處理程序中)。

1.PrintForm1.Print(ME,PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

給這一個鏡頭,讓我知道它是如何工作的你。

+0

這真的很不舒服,以至於無法將表格的高度超過764.我嘗試了您的解決方案。它不適用於大型表單。你有沒有其他的方法來做到這一點形式更多的高度與滾動。謝謝 – Brune

2

這不完全是一個完整的答案,但是這裏有一段代碼需要一個窗體上的可滾動Panel控件的截圖(位圖)。最大的缺點是在截圖時屏幕閃爍。我已經在簡單的應用程序上對它進行了測試,所以它可能不適用於所有情況,但這可能是一個開始。

這裏是如何使用它:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); // create a scrollable panel1 component 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     TakeScreenshot(panel1, "C:\\mypanel.bmp"); 
    } 
} 

這裏是實用程序:

public static void TakeScreenshot(Panel panel, string filePath) 
    { 
     if (panel == null) 
      throw new ArgumentNullException("panel"); 

     if (filePath == null) 
      throw new ArgumentNullException("filePath"); 

     // get parent form (may not be a direct parent) 
     Form form = panel.FindForm(); 
     if (form == null) 
      throw new ArgumentException(null, "panel"); 

     // remember form position 
     int w = form.Width; 
     int h = form.Height; 
     int l = form.Left; 
     int t = form.Top; 

     // get panel virtual size 
     Rectangle display = panel.DisplayRectangle; 

     // get panel position relative to parent form 
     Point panelLocation = panel.PointToScreen(panel.Location); 
     Size panelPosition = new Size(panelLocation.X - form.Location.X, panelLocation.Y - form.Location.Y); 

     // resize form and move it outside the screen 
     int neededWidth = panelPosition.Width + display.Width; 
     int neededHeight = panelPosition.Height + display.Height; 
     form.SetBounds(0, -neededHeight, neededWidth, neededHeight, BoundsSpecified.All); 

     // resize panel (useless if panel has a dock) 
     int pw = panel.Width; 
     int ph = panel.Height; 
     panel.SetBounds(0, 0, display.Width, display.Height, BoundsSpecified.Size); 

     // render the panel on a bitmap 
     try 
     { 
      Bitmap bmp = new Bitmap(display.Width, display.Height); 
      panel.DrawToBitmap(bmp, display); 
      bmp.Save(filePath); 
     } 
     finally 
     { 
      // restore 
      panel.SetBounds(0, 0, pw, ph, BoundsSpecified.Size); 
      form.SetBounds(l, t, w, h, BoundsSpecified.All); 
     } 
    }