I have struggled with this problem enough (months... :-[ ), and I need code examples that works.
var
ScreenDC: HDC;
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
try
ScreenDC := GetDC(0);
Bitmap.LoadFromDevice(ScreenDC);
ReleaseDC(0, ScreenDC);
Canvas.Draw(0, 0, Bitmap);
finally
Bitmap.Free;
end;
end;
MyBitmap := TBitmap.Create;
hWin := GetForegroundWindow;
It works for Notepad (swedish "Anteckningar"), but not other programs. I guess that I have figured out how to put it all together within this year...
BTW: how to get posted code Pascal-formated?
/anders
I came up with a solution where i use the "BitBtn" in the Form as a pointer to retrieve the mouse position. I use that position to get the windows handler.OFFTOPIC:
When you click the "BitBtn" the Form is hidden and a snapshot of the visible window is taken (don't try it on Lazarus' windows!).
Source code is included in "FindWindow.zip".
[tt][size=8pt]unit Unit1; {$mode objfpc}{$H+} interface uses Windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, Buttons; type { TForm1 } TForm1 = class(TForm) BitBtn1: TBitBtn; Image1: TImage; Label1: TLabel; Label2: TLabel; procedure BitBtn1Click(Sender: TObject); private { private declarations } public { public declarations } end; var Form1: TForm1; implementation {$R *.lfm} //procedure FindTheWindow; procedure TForm1.BitBtn1Click(Sender: TObject); var wnd: HWnd; aDC:HDC; coord: tpoint; begin coord := Mouse.CursorPos; Form1.Label1.Caption := ('X=' + IntToStr(coord.X)); Form1.Label2.Caption := ('Y=' + IntToStr(coord.Y)); Form1.Label1.Show; Form1.Label2.Show; Form1.Hide; Sleep(300); wnd := WindowFromPoint(coord); if wnd>0 then begin aDC:=GetDC(wnd); if aDC>0 then Form1.Image1.Picture.Bitmap.LoadFromDevice(aDC); form1.Show; end; end; end.[/size] [/tt]
Were can I find out more of the windows unit types/procedures and functions?