«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

» Help

Take a screen shot of an inactive window


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?

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

The actual WindowSnap function is defined as:

function WindowSnap(windowHandle: HWND; bmp: TBitmap): boolean;
  r: TRect;
  user32DLLHandle: THandle;
  printWindowAPI: function(sourceHandle: HWND; destinationHandle: HDC;
    nFlags: UINT): BOOL; stdcall;
  result := False;
  user32DLLHandle := GetModuleHandle(user32) ;
  if user32DLLHandle <> 0 then
    @printWindowAPI := GetProcAddress(huser32, 'PrintWindow') ;
    if @printWindowAPI <> nil then
      GetWindowRect(windowHandle, r) ;
      bmp.Width := r.Right - r.Left;
      bmp.Height := r.Bottom - r.Top;
        result := printWindowAPI(windowHandle, bmp.Canvas.Handle, 0) ;
end; (*WindowSnap*)

Note that the first parameter to the WindowSnap function is a HWND value (THandle) - the handle of the window you want to capture.

Author: Unknown
Contributor: Riccardo Faiella (Topellina)
Added: 2012-07-06
Last updated: 2012-07-06

« Return to contents »