0

I have an application that has drawn a grid using CDC (it has text, rectangle, and bitmaps). I want to take a screenshot of that finished grid when it is saved and use that screenshot as a "preview" for the file.

How can I take a screenshot of my application and save it?

Thank you,

mgalal
  • 427
  • 12
  • 24

2 Answers2

0

Answer 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 );
}
Community
  • 1
  • 1
crea7or
  • 4,421
  • 2
  • 26
  • 37
  • Although I ended up using a different method to take a screenshot. I needed to capture even the hidden parts of the window, and PrintWindow did not capture it. – mgalal Aug 08 '12 at 00:01
0

Ultimately I ended up doing it this way because I wanted to capture even the hidden parts of the window (since the content extends beyond the screen and requires scrolling):

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);

///Drew items into CaptureDC like I did for OnDraw HERE///

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);
mgalal
  • 427
  • 12
  • 24