1. Home
  2. Computing & Technology
  3. Delphi Programming

Take a Screen Shot of an Inactive Window
Using PrintWindow API from Delphi

By Zarko Gajic, About.com

Take a Screen Shot of an Inactive Window - Using PrintWindow API and Delphi

Take a Screen Shot of an Inactive Window - Using PrintWindow API and Delphi

Taking a screen shot of a window using Delphi code is rather easy.
A screen shot (screen capture) is a copy of the screen's contents that can be saved as a graphics file or displayed in a graphics "aware" control, for example TImage.

In most cases you will want to take a screen shot of the active window or the Windows Desktop.

What if you need to do a screen capture of all the running applications - most of them will be inactive and not visible to the user?

WindowSnap - Inactive Window Screen Capture

Windows XP also introduces the new printing API, PrintWindow. This API enables the caller to snapshot a visual copy of a window into a device context.

Drop a TImage (named "Image1") on a form and use the following code:

WindowSnap(Self.Handle, Image1.Picture.Bitmap) ;
Image1.Refresh;
The actual WindowSnap function is defined as:
function WindowSnap(windowHandle: HWND; bmp: TBitmap): boolean;
var
  r: TRect;
  user32DLLHandle: THandle;
  printWindowAPI: function(sourceHandle: HWND; destinationHandle: HDC; nFlags: UINT): BOOL; stdcall;
begin
  result := False;
  user32DLLHandle := GetModuleHandle(user32) ;
  if user32DLLHandle <> 0 then
  begin
    @printWindowAPI := GetProcAddress(huser32, 'PrintWindow') ;
    if @printWindowAPI <> nil then
    begin
      GetWindowRect(windowHandle, r) ;
      bmp.Width := r.Right - r.Left;
      bmp.Height := r.Bottom - r.Top;
      bmp.Canvas.Lock;
      try
        result := printWindowAPI(windowHandle, bmp.Canvas.Handle, 0) ;
      finally
        bmp.Canvas.Unlock;
      end;
    end;
  end;
end; (*WindowSnap*)
Note that the first parameter to the WindowSnap procedure is a HWND value (THandle) - the handle of the window you want to capture.

WinDowse is an extremely convenient and easy to use tool for obtaining necessary technical information about any window (handle, child windows, etc.)

Here's an idea: enumerate top-level windows (to grab their handles) and create your own task-switcher :)

Delphi tips navigator:
» Select Which Panel Displays AutoHint in Delphi's TStatusBar
« Use Custom Check Box Images for a ListView - Delphi 's TListView Checkboxes are Ugly

More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2008 Tips
  7. Take a Screen Shot of an Inactive Window - Using PrintWindow API from Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.