2012-08-07 112 views
0

我有一个使用CDC绘制网格的应用程序(它具有文本,矩形和位图)。我想在保存完成的网格时截取它,并将该截图用作文件的“预览”。MFC C++屏幕截图

如何截取我的应用程序并保存它?

谢谢

回答

0

最后,我终于实现了这种方式,因为我想捕捉即使窗口的隐蔽部位(由于内容超出屏幕和要求滚动):

CDC* WindowToCaptureDC = AfxGetMainWnd()->GetWindowDC(); 
CDC CaptureDC; 
CDC MemDC; 
MemDC.CreateCompatibleDC(WindowToCaptureDC); 
CaptureDC.CreateCompatibleDC(WindowToCaptureDC); 

CBitmap CaptureBmp; 
CBitmap ResizeBmp; 
int pWidth = grid.tableWidth + grid.marginLeft*2; 
int pHeight = grid.tableHeight + grid.marginBottom; 

CaptureBmp.CreateCompatibleBitmap(WindowToCaptureDC, pWidth, pHeight); 
CaptureDC.SelectObject(&CaptureBmp); 

CBrush brush(RGB(255, 255, 255)); 
CaptureDC.SelectObject(&brush); 
CaptureDC.Rectangle(0, 0, pWidth, pHeight); 

///德鲁物品进入CaptureDC像我一样的OnDraw中的位置///

double width = //desired width; 
double height = //desired width; 

    //maintain aspect ratio 
if(pWidth!=width || pHeight!=height) 
{ 
    double w = width/pWidth; 
    double h = height/pHeight; 
    if(w < h) 
     height = height*w; 
    else 
     width = width*h; 
} 

ResizeBmp.CreateCompatibleBitmap(WindowToCaptureDC, width, height); 
MemDC.SelectObject(&ResizeBmp); 

MemDC.StretchBlt(0, 0, width, height, &CaptureDC, 0, 0, pWidth, pHeight, SRCCOPY); 

CImage TempImageObj; 
TempImageObj.Attach((HBITMAP)ResizeBmp.Detach()); 
CString filePath = _T("LOCATION\\image.bmp"); 
TempImageObj.Save(filePath); 
0

回答is here

void CScreenShotDlg::OnPaint() 
{ 
    // device context for painting 
    CPaintDC dc(this); 

    // Get the window handle of calculator application. 
    HWND hWnd = ::FindWindow(0, _T("Calculator")); 

    // Take screenshot. 
    PrintWindow(hWnd, 
       dc.GetSafeHdc(), 
       0); 
} 
+0

感谢您指点我在正确的方向 – mgalal 2012-08-07 23:49:31

+0

虽然我结束了使用不同的方法来截图。我需要捕捉窗口的隐藏部分,PrintWindow也没有捕获它。 – mgalal 2012-08-08 00:01:23