1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Capturing screen shots including the mouse cursor
Here's how to take a screen shot of the Desktop window including the mouse cursor, using Delphi code.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Zooming Desktop
• ScreenThief
• Grpahics programming

I'm sure you've seen many Delphi code samples designed to capture the screen shot of the Windows Desktop image into a TBitmap object. After all, the whole ScreenThief application was build around this idea (what's more the image is then transformed from Bitmap to Jpeg and streamed over a network ... but that's whole another story).

The trickiest part in taking the screen shot is how to include the current mouse cursor. When the user moves the mouse, the system moves a bitmap on the screen called the mouse cursor. The mouse cursor contains a single-pixel point called the hot spot, a point that the system tracks and recognizes as the position of the cursor.
One could think that getting the mouse icon (cursor) is easy, just call the GetCursor API call, and that call something like DrawIconEx to actually draw the cursor on a Canvas. However, the GetCursor call only retrieve the handle of the current cursor in the calling process - meaning that the correct cursor will not be captured, since at the time the screen shot is taken some other application (process) might be active.

GetCursorInfo, GetIconInfo, ...
The following procedure, DrawCursor, draws the current mouse cursor image on the TBitmap object passed as the ScreenShotBitmap parameter. In general, you call the DrawCursor procedure after you have captured the screen shot bitmap into the ScreenShotBitmap.

procedure DrawCursor(ScreenShotBitmap : TBitmap);
  r: TRect;
  CI: TCursorInfo;
  Icon: TIcon;
  II: TIconInfo;
  r := ScreenShotBitmap.Canvas.ClipRect;
  Icon := TIcon.Create;
    CI.cbSize := SizeOf(CI);
    if GetCursorInfo(CI) then
      if CI.Flags = CURSOR_SHOWING then
        Icon.Handle := CopyIcon(CI.hCursor);
        if GetIconInfo(Icon.Handle, II) then
                ci.ptScreenPos.x - Integer(II.xHotspot) - r.Left,
                ci.ptScreenPos.y - Integer(II.yHotspot) - r.Top,

The GetCursorInfo function TFills out the TCursorInfo record passed in. If the mouse cursor is not hidden (CI.Flags = CURSOR_SHOWING) the CopyIcon API call is used to grab the mouse graphics as TIcon object (from the hCursor field of the TCursorInfo record). Finally, GetIconInfo function retrieves information about the mouse cursor, and the mouse icon is painted on the provided ScreenShotBitmap using the Draw method of the Canvas object.

ScreenShot ... DrawCursor
The actuall call to the DrawCursor procedure could look like:

  pic : TBitmap;
  pic := TBitmap.Create;

Note that the code above calls the ScreenShot procedure that actually captures the Desktop screen into the pic (TBitmap) object.

And that's it.

As always I encourage you to post your views, comments, questions and doubts to this article on the Delphi Programming Forum. Discuss!

~ Zarko Gajic

©2017 About.com. All rights reserved.