2014-08-28 61 views
1

我想仅打印来自C#应用程序的活动窗口。使用CopyFromScreen()不起作用,因为它需要一个屏幕截图,并且窗口可能会出现其他瞬态窗口部分隐藏。所以,我想PrintWindow():仅打印C中的活动窗口#

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) 
{ 
    e.Graphics.PageScale = 10.0f; 
    Graphics g = e.Graphics; 
    PrintWindow(this.Handle, g.GetHdc(), 0); 
} 

无论哪个PageScale我用这产生了窗口的一个小邮票大小的打印输出。 任何想法?

编辑 该做的伎俩:

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) 
{ 
    Graphics myGraphics = CreateGraphics(); 
    var bitmap = new Bitmap(Width, Height, myGraphics); 
    DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); 
    e.Graphics.DrawImage(bitmap, 0, 0); 
} 

回答

-1

一些使用Google四周,我发现:Copying content from a hidden or clipped window in XP?

看来你需要准备一个位图,用于存储网页:

// Takes a snapshot of the window hwnd, stored in the memory device context hdcMem 
HDC hdc = GetWindowDC(hwnd); 
if (hdc) 
{ 
    HDC hdcMem = CreateCompatibleDC(hdc); 
    if (hdcMem) 
    { 
     RECT rc; 
     GetWindowRect(hwnd, &rc); 

     HBITMAP hbitmap = CreateCompatibleBitmap(hdc, RECTWIDTH(rc), RECTHEIGHT(rc)); 
     if (hbitmap) 
     { 
      SelectObject(hdcMem, hbitmap); 

      PrintWindow(hwnd, hdcMem, 0); 

      DeleteObject(hbitmap); 
     } 
     DeleteObject(hdcMem); 
    } 
    ReleaseDC(hwnd, hdc); 
} 
+0

它可能不是C#,但C代码表示您可能需要创建一个窗口大小的位图缓冲区。 – Pieter21 2014-08-28 14:33:17

+0

我会试试看,谢谢。 – 2014-08-29 05:24:08