Gold mine of Visual C++ tricks!
How to capture the screenshot of window?
![]()
We’re familear with PrintScreen and Alt+PrintScreen shortcuts. They are used to take the screenshot of entire desktop or a particular window. Well, Is it possible to take the snapshot of a particular control or a particular window by yourself?
![]()
You can use the api – PrintWindow(). If you provide the window handle, the screenshot of the window will be drawn to the provided device context. See the screenshot application which captures the screenshot of calculator application.

See the code snippet.
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 );
}
![]()
The PrintWindow() is available only from XP onwards. So for getting this compiled, you’ve to add _WIN32_WINNT=0×0501 to preprocessor definitions in project settings.
Also check WM_PRINT message, which is more or less same as PrintWindow().
![]()
Targeted Audience – Beginners.
| Print article | This entry was posted by Jijo Raj on July 14, 2008 at 6:36 pm, and is filed under Codeproject, GDI & GDI+. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
Will this also take a screenshot of a screen that is not visible? Because that would be very handy to me.
Thanks in Advance
about 1 year ago
v nice
about 10 months ago
Thanks for nice info
about 10 months ago
Thanks Anup! Keep watching.
Regards,
Jijo.