Website development and design blog, tutorials and inspiration

Grab Screen Shot with Delphi

Delphi Screen Shots

By , Written on in Software Engineering

Grab Screen Shot with Delphi

154 words, estimated reading time 1 minutes.

This short procedure will capture the current screen contents to a Bitmap which you can manipulate or save to disk.

  1. procedure GrabScreen(bm: TBitMap; gt : GrabType);
  2. var
  3. DestRect, SourceRect: TRect;
  4. h: THandle;
  5. hdcSrc : THandle;
  6. pt : TPoint;
  7. begin
  8. case(gt) of
  9. GTWINDOW, GTCLIENT : h := GetForeGroundWindow;
  10. GTSCREEN : h := GetDesktopWindow;
  11. else h := 0;
  12. end;
  13. if h <> 0 then
  14. begin
  15. try
  16. if gt = GTCLIENT then
  17. begin
  18. hdcSrc := GetDC(h); // use this for ClientRect
  19. Windows.GetClientRect(h,SourceRect);
  20. end
  21. else
  22. begin
  23. hdcSrc := GetWindowDC(h);
  24. GetWindowRect(h, SourceRect);
  25. end;
  26. bm.Width := SourceRect.Right - SourceRect.Left;
  27. bm.Height := SourceRect.Bottom - SourceRect.Top;
  28. DestRect := Rect(0, 0, SourceRect.Right - SourceRect.Left, SourceRect.Bottom - SourceRect.Top);
  29. StretchBlt(bm.Canvas.Handle, 0, 0, bm.Width,
  30. bm.Height, hdcSrc,
  31. 0,0,SourceRect.Right - SourceRect.Left,
  32. SourceRect.Bottom - SourceRect.Top,
  33. SRCCOPY);
  34. if gt = GTCLIENT then
  35. begin
  36. pt.X :=SourceRect.Left;
  37. pt.Y :=SourceRect.Top;
  38. // call Windows.ClientToScreen() to translate X and Y
  39. Windows.ClientToScreen(h, pt);
  40. //DrawCursor(bm,pt.X, pt.Y);
  41. end
  42. //
  43. else
  44. //DrawCursor(bm,SourceRect.Left, SourceRect.Top);
  45. finally
  46. ReleaseDC(0, hdcSrc);
  47. end;
  48. end;
  49. end;

Last updated on: Saturday 24th June 2017

Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.