Recent

Author Topic: Freepascal - Console application to make Desktop screenshot on Windows 7  (Read 3996 times)

dogriz

  • Full Member
  • ***
  • Posts: 105
    • Tech blog - Delphi, Lazarus, Firebird, Windows, Linux, Android...
Is it posible to create console application that can take screenshots of Windows 7 desktop and save it to image file?
FPC 3.0.4
Lazarus 2.0.2
Debian x86_64, arm

Trenatos

  • Hero Member
  • *****
  • Posts: 508
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: Freepascal - Console application to make Desktop screenshot on Windows 7
« Reply #1 on: September 25, 2015, 06:38:53 pm »
Is it possible? Yes.

I haven't done it myself, but this should help:  http://wiki.lazarus.freepascal.org/Developing_with_Graphics#Taking_a_screenshot_of_the_screen

dogriz

  • Full Member
  • ***
  • Posts: 105
    • Tech blog - Delphi, Lazarus, Firebird, Windows, Linux, Android...
Re: Freepascal - Console application to make Desktop screenshot on Windows 7
« Reply #2 on: September 25, 2015, 08:23:18 pm »
Yes, I know this method, I've used something similar in Delphi. But what I want is to accomplish that without using Graphics and LCL (for example: simulate print screen button and get result from clipboard somehow... maybe, I don't know)
FPC 3.0.4
Lazarus 2.0.2
Debian x86_64, arm

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Freepascal - Console application to make Desktop screenshot on Windows 7
« Reply #3 on: September 25, 2015, 11:09:01 pm »
Quote
But what I want is to accomplish that without using Graphics and LCL
Maybe this can help you (which also leads to this)

Microsoft also has an article about it.

Pretty weird that i just stumbled upon those while googling   :-X
« Last Edit: September 25, 2015, 11:12:47 pm by molly »

GetMem

  • Hero Member
  • *****
  • Posts: 3508
Re: Freepascal - Console application to make Desktop screenshot on Windows 7
« Reply #4 on: September 26, 2015, 09:37:16 am »
Here you go:
Code: [Select]
program Test;

{$MODE Delphi}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  windows,
  Classes
  { you can add units after this };

function ScreenShot(const AFileName: String): Boolean;
var
  bFileHeader: TBitmapFileHeader;
  bHandle: HGLOBAL;
  bInfoHeader: PBitmapInfoHeader;
  ms: TMemoryStream;
begin
  Result := False;
  Keybd_Event(VK_SNAPSHOT, MapVirtualKey(VK_SNAPSHOT, 0), 0, 0);
  Keybd_Event(VK_SNAPSHOT, MapVirtualKey(VK_SNAPSHOT, 0), KEYEVENTF_KEYUP, 0);
  Sleep(2000);
  if OpenClipboard(0) then
  begin
    bHandle := GetClipboardData(CF_DIB);
    if bHandle <> 0 then
    begin
      bInfoHeader := GlobalLock(bHandle);
      if bInfoHeader <> nil then
      begin
        FillChar(bFileHeader, SizeOf(bFileHeader), 0);
        bFileHeader.bfType := $4D42;
        bFileHeader.bfSize := SizeOf(bFileHeader) + GlobalSize(bHandle);
        bFileHeader.bfOffBits := SizeOf(bFileHeader) + bInfoHeader.biSize;
        ms := TMemoryStream.Create;
        try
          ms.WriteBuffer(bFileHeader, SizeOf(bFileHeader));
          ms.WriteBuffer(bInfoHeader^, bFileHeader.bfSize - SizeOf(bFileHeader));
          ms.Position := 0;
          ms.SaveToFile(AFileName);
          Result := True;
        finally
          ms.Free;
        end;
        GlobalUnlock(bHandle)
      end;
    end;
    CloseClipboard;
  end;
end;

begin
  if ScreenShot('c:\test.bmp') then
    Writeln('success')
  else
    Writeln('failed');
  Readln;
end.                   
« Last Edit: September 26, 2015, 09:40:32 am by GetMem »

dogriz

  • Full Member
  • ***
  • Posts: 105
    • Tech blog - Delphi, Lazarus, Firebird, Windows, Linux, Android...
Re: Freepascal - Console application to make Desktop screenshot on Windows 7
« Reply #5 on: September 26, 2015, 12:26:21 pm »
Thanks GetMem, this is what I was looking for.
FPC 3.0.4
Lazarus 2.0.2
Debian x86_64, arm