Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Taking screenshot behind the form using magnification (https://www.delphipraxis.net/195190-taking-screenshot-behind-form-using-magnification.html)

victorlus 9. Feb 2018 18:23

Delphi-Version: XE5

Taking screenshot behind the form using magnification
 
I am trying to capture screen behind form, photo that was capture is black someone has a solution?

https://i.imgur.com/lBO4I3J.png

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Magnification, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    Initialized: BOOL;
    magWindowRect: TRect;
    hwndMag: HWND;
    HandleToMagnifier: HWND;
    function MyCreateDIBSection(DC: HDC; Width, Height: integer; BitCount: integer): HBITMAP;
    procedure DrawMagnifier(Width, Height: Integer);
    procedure RefreshMagnifier(rc:TRect);
   // procedure ResizeMagnifier;
    procedure InitializeMagnifier;
  public
    { Public declarations }
  end;


const
  MagFactor = 2.0;

var
  Form1: TForm1;


implementation

{$R *.dfm}


function TForm1.MyCreateDIBSection(DC: HDC; Width, Height, BitCount: integer): HBITMAP;
var
  lpbmi: TBitmapInfo;
  P: Pointer;
  AbITmAP : hbitmap;
  db : tbitmap;

begin

  Fillchar(lpbmi, SizeOf(TBitmapInfo), 0);
  lpbmi.bmiHeader.biSize := sizeof(lpbmi.bmiHeader);
  lpbmi.bmiHeader.biHeight := - Height;
  lpbmi.bmiHeader.biWidth := width;
  lpbmi.bmiHeader.biPlanes := 1;
  lpbmi.bmiHeader.biBitCount := BitCount;
  lpbmi.bmiHeader.biCompression := BI_RGB;

  Result := CreateDIBSection(DC, lpbmi, DIB_RGB_COLORS, P, 0, 0);
end;

procedure TForm1.DrawMagnifier(Width, Height: Integer);
var
  aBitmap: HBITMAP;
  DC: HDC;
  bmp: TBitmap;
begin

  DC := GetWindowDC(HwndMag);
  bmp := TBitmap.Create;
  aBitmap := 0;
  try
    aBitmap := Form1.MyCreateDIBSection(DC, Width, Height, 32);
    SelectObject(DC, aBitmap);
    bmp.Handle := aBitmap;
    bmp.SaveToFile('Screen.bmp');
  finally
    DeleteObject(aBitmap);
    DeleteDC(DC);
    bmp.Free;
  end;
end;

procedure TForm1.RefreshMagnifier(rc: TRect);
begin

  MagSetWindowSource(hwndMag, rc);
  SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);

  InvalidateRect(hwndMag, rc, true);
end;


procedure TForm1.InitializeMagnifier;
var
  matrix: TMagTransform;
    desktop : hwnd;
  desktoprect, sourceRect: TRect;
  filterList: THWNDArray;
  m_ScreenX, m_ScreenY, m_ScreenT, m_ScreenL: Integer;
begin

   desktop := GetDesktopWindow;
  GetWindowRect(desktop, desktoprect);

  m_ScreenT := desktoprect.Top;
  m_ScreenL := desktoprect.Left;
  m_ScreenX := desktoprect.right;
  m_ScreenY := desktoprect.bottom;

  hwndMag := CreateWindow(WC_MAGNIFIER, 'MagnifierWindow',
    WS_CHILD or MS_SHOWMAGNIFIEDCURSOR, 0, 0, m_ScreenX,
    m_ScreenY, Form1.handle, 0, hInstance, nil);

  if hwndMag = 0 then
    close;

  FillChar(matrix, Sizeof(matrix), 0);
  matrix.v[0][0] := MagFactor;
  matrix.v[1][1] := MagFactor;
  matrix.v[2][2] := 1.0;
  if MagSetWindowTransform(hWndMag, matrix) then
//    tmr1.Enabled := true;
end;



procedure MagScreenShot;
var
  desktop : hwnd;
  desktoprect, sourceRect: TRect;
  filterList: THWNDArray;
  m_ScreenX, m_ScreenY, m_ScreenT, m_ScreenL: Integer;
begin

  Form1.WindowState := wsMaximized;

  desktop := GetDesktopWindow;
  GetWindowRect(desktop, desktoprect);

  Form1.DrawMagnifier(desktoprect.Right, desktoprect.Bottom);
  Form1.RefreshMagnifier(desktoprect);

end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  MagScreenShot;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

 Initialized := MagInitialize;
  if not Initialized then
  begin
    Application.MessageBox('Init magnification failed', 'Error',
      mb_Ok + mb_IconError);
    close;
  end;

  InitializeMagnifier;


end;


procedure TForm1.FormDestroy(Sender: TObject);
begin
  if (initialized) then
    MagUninitialize;
end;

end.

Luckie 9. Feb 2018 19:06

AW: Taking screenshot behind the form using magnification
 
Windows doesen't render invisible parts of the screen due to performance issues. At least older Windows versions didn't render invisible parts of the screen.

EWeiss 9. Feb 2018 19:26

AW: Taking screenshot behind the form using magnification
 
Den code kenne ich irgendwo her. :)

gruss

victorlus 9. Feb 2018 19:44

AW: Taking screenshot behind the form using magnification
 
Zitat:

Zitat von EWeiss (Beitrag 1393495)
Den code kenne ich irgendwo her. :)

gruss

Yes you and the code creator, can you help me?
I'm in trouble, I want to take a photo behind the form.

EWeiss 9. Feb 2018 19:59

AW: Taking screenshot behind the form using magnification
 
what your want, that?

http://www.delphipraxis.net/1393413-post19.html

greets

victorlus 9. Feb 2018 20:04

AW: Taking screenshot behind the form using magnification
 
Zitat:

Zitat von EWeiss (Beitrag 1393497)



So in my case I can not hide the form, I need to make it visible to the client all the time

EWeiss 9. Feb 2018 20:11

AW: Taking screenshot behind the form using magnification
 
Zitat:

Zitat von victorlus (Beitrag 1393498)
Zitat:

Zitat von EWeiss (Beitrag 1393497)



So in my case I can not hide the form, I need to make it visible to the client all the time

i think that is only available with DirectX.
Magnification is not the way to do it and will not work in conjunction with DWM.

EDIT:
without hide or minimize your form.. see
Zitat:

Windows doesen't render invisible parts of the screen due to performance issues. At least older Windows versions didn't render invisible parts of the screen.
if the form hidden or minimized then a simple BitBlt Call should work on all Window Versions to


greets

Zacherl 9. Feb 2018 20:14

AW: Taking screenshot behind the form using magnification
 
Zitat:

Zitat von victorlus (Beitrag 1393498)
So in my case I can not hide the form, I need to make it visible to the client all the time

Could you please explain what EXACTLY you are trying to achieve? There might be an easy way to work around that problem. The magnification API is a hack .. I remember reading the article you linked in your original post. The author stated that it is not possible to obtain a bitmap from the maginification window and thats why he used the (deprecated) callback to manually save the bitmap.

I guess the magnification API uses DirectX to paint the screenshot, so you might be able to intercept that process by hooking some DirectX interfaces, but thats hacky as well.

victorlus 9. Feb 2018 20:18

AW: Taking screenshot behind the form using magnification
 
My goal is to be able to capture what's behind the form, which works on win7, win8, win10, if it's very difficult I can pay to do it for myself.

Rollo62 10. Feb 2018 05:38

AW: Taking screenshot behind the form using magnification
 
Are you looking for hidden changes behind the form ?
I'm afraid Windows won't update the hidden regions at all, for a good reason: Performance.
They only might get updated when these regions get visible.

Rollo

Zacherl 10. Feb 2018 14:51

AW: Taking screenshot behind the form using magnification
 
Zitat:

Zitat von victorlus (Beitrag 1393501)
My goal is to be able to capture what's behind the form, which works on win7, win8, win10, if it's very difficult I can pay to do it for myself.

Yeah I understood that, but WHY do you want to do that?

victorlus 10. Feb 2018 15:49

AW: Taking screenshot behind the form using magnification
 
Zitat:

Zitat von Zacherl (Beitrag 1393535)
Zitat:

Zitat von victorlus (Beitrag 1393501)
My goal is to be able to capture what's behind the form, which works on win7, win8, win10, if it's very difficult I can pay to do it for myself.

Yeah I understood that, but WHY do you want to do that?

I need to do this, to monitor what's behind the form
My program will work this way, visible to the client all time, I can see what is behind the form

Zacherl 10. Feb 2018 15:53

AW: Taking screenshot behind the form using magnification
 
Zitat:

Zitat von victorlus (Beitrag 1393537)
Zitat:

Zitat von Zacherl (Beitrag 1393535)
Zitat:

Zitat von victorlus (Beitrag 1393501)
My goal is to be able to capture what's behind the form, which works on win7, win8, win10, if it's very difficult I can pay to do it for myself.

Yeah I understood that, but WHY do you want to do that?

I need to do this, to monitor what's behind the form
My program will work this way, visible to the client all time, I can see what is behind the form

Mhh sorry, I guess I cant help you with that. Not sure whats the purpose of such a program. You could use MSDN-Library durchsuchenPrintWindow for specific windows (you might even be able to compose your own "desktop" by iterating through all visible windows). This should work for background windows, but might not work for minimized ones.

EWeiss 10. Feb 2018 20:29

AW: Taking screenshot behind the form using magnification
 
Zitat:

This should work for background windows, but might not work for minimized ones.
NO that not work.
Zitat:

The PrintWindow function copies a visual window into the specified device context (DC), typically a printer DC.
The Desktop is not a real Window.
And PrintWindow works only for the own Window (or within active Desktop), you can not capture the Background from Desktop that Fails.

and i have tell forget it without minimize, or hide your Form.

nobody can help for that.
but here is what you can see behind the form. :lol:

greets

HolgerX 11. Feb 2018 06:17

AW: Taking screenshot behind the form using magnification
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hm..

1.) It is possible to make a ScreenShot without the foreground Application!

2.) The DESKTOP is a Window! It is the Mainwindow of an 'Explorer' instanz, in older Windowsversions 'Progman'!

I made a small demo, where i use PrintWindow!
(Test_ScreenShot_BackGround.zip)
It is made with Delphi 6 and goes to all Apps in Z-Order, starting with the last (Desktop!).

I have tested it in Windows8.1 (Classic Shell).

EWeiss 11. Feb 2018 08:01

AW: Taking screenshot behind the form using magnification
 
Zitat:

Zitat von HolgerX (Beitrag 1393551)
Hm..

1.) It is possible to make a ScreenShot without the foreground Application!

2.) The DESKTOP is a Window! It is the Mainwindow of an 'Explorer' instanz, in older Windowsversions 'Progman'!

I made a small demo, where i use PrintWindow!
(Test_ScreenShot_BackGround.zip)
It is made with Delphi 6 and goes to all Apps in Z-Order, starting with the last (Desktop!).

I have tested it in Windows8.1 (Classic Shell).

Zitat:

It is possible to make a ScreenShot
and your think that is a sreenshot? sorry for me that is nothing
and Fails on W7 Forget it (40% Cpu? )
you should handle WM_PRINT and WM_PRINTCLIENT. (i think you do nothing of both)

Zitat:

The DESKTOP is a Window!
not for me.. then the Handle never changed.
which is only a Container where the real Window are Painting.
what is a Window

greets

HolgerX 11. Feb 2018 09:45

AW: Taking screenshot behind the form using magnification
 
Hmm..

@EWeiss

1.)

Zitat:

Zitat von EWeiss (Beitrag 1393552)
..
you should handle WM_PRINT and WM_PRINTCLIENT. (i think you do nothing of both)


Sorry, you have to read the API-Description by Microsoft correct:

Zitat:

The application that owns the window referenced by hWnd processes the PrintWindow call and renders the image in the device context that is referenced by hdcBlt. The application receives a WM_PRINT message or, if the PW_PRINTCLIENT flag is specified, a WM_PRINTCLIENT message. For more information, see WM_PRINT and WM_PRINTCLIENT.
https://msdn.microsoft.com/de-de/lib...(v=vs.85).aspx


The app with the HWND will receive a WM_PRINT, NOT the caller of PrintWindow!


2.)
Your Screenshoot of your Videoplayer didn't work, of course it use DirectX for the Videoplaying, which is NOT handle by Windows GDI and also NOT could redraw by Microsoft GDI! ('Overlayed Window').



3.)
Delphi-Quellcode:
H := GetWindow(self.Handle, GW_HWNDLAST); // Desktop
Give you the LAST Window in the Z-Order, and this IS the Desktopwindow.

Use this Handle only with 'function ScreenshotHidden(wnd: HWND; const bmp: TBitmap): Boolean;' and you will see...

In old Windows the desktop is handled by Progman.exe
(https://en.wikipedia.org/wiki/Program_Manager)

In newer ones it is replaced by explorer.exe
Look in 'Shell' in
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon (per machine)
or
HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon (per user)
and you will see explorer.exe!

In replacing this you could use your own Desktoptool, like kiosksystems do!!!



4.)
A (i think) real Screenshot could only be made with ALL windows including the top one, of course it make a copy of the Screen DC, where all Windows draw itself in the Z-Order!

The only possibility to make somthing neer a Screenshot is to do the same what MS Windows do in refreshing the Screen: Go throu all Windows and let them 'PrintWindow' himself to the Screen DC!

This is used by my 'Testtool'!

Mircosoft do in this way a lot of things with every Windowprint, like round the corners....
That is, why it could look differnt to the screen!

EWeiss 11. Feb 2018 10:11

AW: Taking screenshot behind the form using magnification
 
Deutsch.. sorry mein Englisch ist unter aller Sau. ;)

Zuvor! Geht nicht gegen deine Person sondern ist rein Fachlich gesehen.

Zitat:

The app with the HWND will receive a WM_PRINT, NOT the caller of PrintWindow!
Warum werden dann alle meine Anwendungen die über WM_PRINT gezeichnet werden bei dir nicht aufgenommen?

Zitat:

Your Screenshoot of your Videoplayer didn't work, of course it use DirectX for the Videoplaying, which is NOT handle by Windows GDI and also NOT could redraw by Microsoft GDI! ('Overlayed Window').
Deshalb ist dein Tool nicht zu gebrauchen.
Du siehst schon die ganzen schwarzen Fenster die nicht aufgenommen werden unabhängig von meinem Video Player?
Der Startbutton, GDIClock, OTTB, Taskleiste, die Visualisierung in meinem Media Player und so weiter.
Ein Screenshot sollte das wiedergeben was im originalen Zustand vorhanden ist das schließt auch die Transparenz mit ein.
Was soll man mit einer halbfertigen Anwendung die alles verfälscht.

Zitat:

Desktopwindow
Der Desktop ist kein reales Fenster.
Ein Fenster unterliegt speziellen Voraussetzung um als Window definiert zu werden deshalb mein Link.
Fakt ist der Desktop hat diese Vorrausetzungen nun mal nicht.
Da kann man rumreden wie man will.

Auch wenn es DesktopWindow heißt ist es kein Fenster im herkömmlichen sinne.

Aber auch unter W7 (Classic Shell) ist es nicht zu gebrauchen wie du sehen kannst.

gruss

Rollo62 11. Feb 2018 11:18

AW: Taking screenshot behind the form using magnification
 
You still have not explained what is your final goal.
Just to have a background picture doesn't make much sense, you can take it before showing your app.

So what exctly are you looking for, maybe there are other measures to get this info extracted from a window handle or message notificationbut without such knowledge I'm afraid its hard to help you with that.

Rollo

HolgerX 11. Feb 2018 12:21

AW: Taking screenshot behind the form using magnification
 
Hmm..


@EWeiss

Sorry, dass deine Programme anders funktionieren ist ja bereits hinreichend bekannt...
Weshalb diese kein WM_Paint beim Aufruf von PaintWindow erhalten, da musst du wohl die Fa. Microsoft befragen und nicht mein einfaches Testtool niedermachen.
Ganz nach dem Motto, wenn Du selber nicht weiter weist, machst Du alles nieder, mit was andere Versuchen zu helfen/Hinweise zu geben... (auch hinreichend bekannt..)


Mit Fenstern, welche mit normalen Windows GDI über die 'WINDOWS-API' PaintWindow gezeichnet werden (z.B. Notepad), funktioniert es....

Und auch wenn Du es mir nicht glauben willst:
Das, was umgangssprachlich als 'Desktop' betitelt wird, ist (seit Windows NT4/2000) ein Explorer-Fenster, welches als 'SHELL' von Windows gestartet wird! Dieses zeigt dir u.a. deine 'Desktop-Icons' an. Ohne diese 'SHELL' hast du einen nackten Bildschirm ohne Startmenu und Co..

Beschäftige dich mal mit der WIndows-API 'OpenDesktop', 'CreateDesktop', 'EnumDesktops', 'SetThreadDesktop'...
Damit kann man einen 'eigenen' virtuellen Desktop erstellen. Um hier Icons platzieren zu können oder ein Startmenü verwenden zu können, muss du hier (wie es Windows beim Ersten Desktop automatisch macht) ebenfalls einen Programm manager starten, wie z.B. den Explorer.exe.
Dieser verhält sich zu einem zusätzlich gestartetem Explorer anders (FullScreen, Borderless, Große Icons,...).


Weshalb beim 'PaintWindow' einige Bestandteile schwarz bleiben, wirst Du wohl ebenfalls entweder bei den Programmieren dieser Programme oder ebenfalls bei Microsoft anfragen müssen.

Denn 'PaintWindow' ist nur ein API-Aufruf. Was darin passiert, oder auch nicht, bestimmt Microsoft oder eventuell der Programmierer der Applikation, wenn er eine andere Option zum Zeichnen seines Applikationsfenster benutzt.


Bei DirektX ist es so, dass dieses das Malen (Stichwort Overlay!) selber erledigt und nicht unbedingt auf WM_PAINT oder andere 'Windows-Messages' reagiert.
Was anderes hatte ich auch nicht geschrieben, da ich mich in

Zitat:

Zitat von HolgerX (Beitrag 1393562)
2.)
Your Screenshoot of your Videoplayer didn't work, of course it use DirectX for the Videoplaying, which is NOT handle by Windows GDI and also NOT could redraw by Microsoft GDI! ('Overlayed Window').

auch nur auf deinen Videoplayer bezogen habe, der DirektX und co. verwendet.

Um einen vollständiges Bild von einem externen DirectX-Fenster zu erhalten ist ein höherer Aufwand notwendig.
(Nutze Google, um dich selber zu überzeugen.)


Wenn ich falsch liege, kann mich ja jemand anderes korrigieren, jedoch bitte nicht EWeiss..


Es ist schade, dass dieser Thread wieder so ausartet...
Deshalb ist in diesem Thread nun auch für mich Schluss und dem Threadersteller muss jemand anderes versuchen weiterzuhelfen...

EWeiss 11. Feb 2018 13:31

AW: Taking screenshot behind the form using magnification
 
Zitat:

Wenn ich falsch liege, kann mich ja jemand anderes korrigieren, jedoch bitte nicht EWeiss..
Wenn du unrecht hast versuchst du es an einer Person festzumachen.. Ja klar doch.
Dabei schrieb ich extra es hat nichts mit dir persönlich zu tun.

Zitat:

Sorry, dass deine Programme anders funktionieren ist ja bereits hinreichend bekannt...
Tja es gibt nun mal andere Möglichkeiten etwas zu zeichnen ohne PAINT zu verwenden.
Aber vielleicht lernst du das auch noch.. irgendwann.

Zu deiner Information wenn ein Window den Windowstyle WS_EX_LAYERED verwendet dann kannst du dein PrintWindow getrost vergessen denn die Rückgabe wird immer ein schwarzes Window in deinem Screenshot sein.
Das ist nichts spezielles was nur mit meinen Programmen zu tun hat. Siehe "Yahoo Widget, Google Desktop"
Vielleicht hättest du dich erst mal schlau machen sollen.

Zitat:

Um einen vollständiges Bild von einem externen DirectX-Fenster zu erhalten ist ein höherer Aufwand notwendig.
nö ganz einfach ein klick auf die Print Taste reicht dafür.

Zitat:

Weshalb beim 'PaintWindow' einige Bestandteile schwarz bleiben
Dachte eigentlich es ging um die API PrintWindow.. :) siehe WS_EX_LAYERED WindowStyle

Zitat:

machst Du alles nieder, mit was andere Versuchen zu helfen/Hinweise zu geben...
Ich sage das was ich sehe und ja ich habe dir belegt das es nichts taugt. siehe alleine die Fenster (nicht von mir) Widgets.

Wenn ich etwas mache dann mache ich es richtig und versuche nicht etwas für gut zu verkaufen das nichts taugt
und versuche dann die Schuld anderen zu geben Me, Microsoft und Konsorte.
Ich würde bei dir definitiv nichts kaufen.. bzw. wenn 3/4 reklamieren.

Ok bin eh raus aus dem Thema muss mir das nicht antun.
Und ja EWeiss hat geantwortet. (Der Spezialist der alles anders macht als wie ihr es gerne hättet)
Warum soll ich auf deine Programmierweise, Kenntnisse Rücksicht nehmen, entweder man kann was oder lässt es bleiben.

EDIT:
Ich möchte nicht nachzählen in wie viel Threads du versucht hast meine Arbeit madig zu machen. (AnimatePng, Sorry, aber dann ist deine DLL schrot..)
Nun siehst du mal wie es ist wenn man sich berechtigter weise Kritik anhören muss.
Damit scheinst du nicht umgehen zu können.
Wie gesagt bin raus damit das nicht wieder ausartet.

gruss


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:26 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz