Solved

Screenshot or Alt and Printscreen

Posted on 2001-08-29
4
1,591 Views
Last Modified: 2010-05-18
I want to take a desktop screenshot or simulate alt + printscreen from Delphi 5. Are there any API calls or Delphi code to do this? Thanx, Magnus.
0
Comment
Question by:skynergy
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 50 total points
ID: 6435487
didn't try to compile it, but I think it will work:

uses
  Windows, clipbrd;

procedure CaptureScreen(WholeDesktop: Boolean);
var
  Desktop: HWND;
  DesktopDC: HDC;
  FWidth, FHeight, FLeft, FTop: Integer;
  AImage: TBitmap;
  WindowRect: TRect;
  AFormat: Word;
  AData, APalette : THandle;
begin
  Desktop := GetDesktopWindow;

  AImage := TBitmap.Create;
  if WholeDesktop then
  begin
    FWidth := Screen.Width;
    FHeight := Screen.Height;
    FLeft := 1;
    FTop := 1;
  end else
  begin
    GetWindowRect(GetTopWindow(0), WindowRect);
    FWidth := WindowRect.Right - WindowRect.Left;
    FHeight := WindowRect.Bottom - WindowRect.Top;
    FLeft := WindowRect.Left;
    FTop := WindowRect.Top;
  end;

  DesktopDC := GetWindowDC(Desktop);
  try
    AImage.Width := FWidth;
    AImage.Height := FHeight;
    BitBlt(AImage.Canvas.Handle, 1, 1, FWidth, FHeight,
      DesktopDC, FLeft, FTop, SRCCOPY);
    AImage.SaveToClipBoardFormat(AFormat, AData, APalette);
    ClipBoard.SetAsHandle(AFormat, AData);
  finally
    ReleaseDC(Desktop, DesktopDC);
    AImage.Free;
  end;
end;
0
 

Author Comment

by:skynergy
ID: 6435588
Hi there. When compiling I'm geting the following error: "[Error] Functions.pas(1189): Types of actual and formal var parameters must be identical" for "AImage.SaveToClipBoardFormat(AFormat, AData, APalette);".

I have changed the procedure slighty for my needs:

procedure CaptureScreen(csWholeDesktop: Boolean; var csBitMap: TImage);
var
 Desktop: HWND;
 DesktopDC: HDC;
 FWidth, FHeight, FLeft, FTop: Integer;
 AImage: TBitmap;
 WindowRect: TRect;
 AFormat: Word;
 AData, APalette : THandle;
begin
 Desktop := GetDesktopWindow;

 AImage := TBitmap.Create;
 if csWholeDesktop then
 begin
   FWidth := Screen.Width;
   FHeight := Screen.Height;
   FLeft := 0;
   FTop := 0;
 end else
 begin
   GetWindowRect(GetTopWindow(0), WindowRect);
   FWidth := WindowRect.Right - WindowRect.Left;
   FHeight := WindowRect.Bottom - WindowRect.Top;
   FLeft := WindowRect.Left;
   FTop := WindowRect.Top;
 end;

 DesktopDC := GetWindowDC(Desktop);
 try
   AImage.Width := FWidth;
   AImage.Height := FHeight;
   BitBlt(AImage.Canvas.Handle, 0, 0, FWidth, FHeight,
     DesktopDC, FLeft, FTop, SRCCOPY);
 finally
   ReleaseDC(Desktop, DesktopDC);
   csBitMap.Picture.Assign(AImage);
   AImage.Free;
 end;
end;

You wil also notice that I changed all the 1's to 0's because there was a grey line on the left for a whole desktop capture.

Thanx a lot for this code!
Magnus
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6439132
ah, so instead of capturing to clipboard, you actually wanted to save it into a file :)

I would advise putting
  csBitMap.Picture.Assign(AImage);
after the bitblt statement instead of in the finally statement

Oh, and my mistake, I should've put in 0, 0 instead of 1, 1 as the starting point :)



DragonSlayer.
0
 

Author Comment

by:skynergy
ID: 6522737
Thanx for all the code examples!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question