Find specific window and retrieve it's content (windows VISTA) lazarus / 39-Graphics ---------------------------------------------------------------------- --Dynamic-Pascal-- · 2010-09-23 08:45 Hi! 1. I want to find a visible window from a running process. 2. Make a copy of its bitmap (content) in memory (array or other structure) where I can access each pixel fast. 3. Repeat 2. 1-3 times/sec (even if the window is resized or moved). -------- I have struggled with this problem enough (months...  ), and I need code examples that works. /anders (Lazarus IDE v0.9.29) ---------------------------------------------------------------------- theo · 2010-09-23 09:27 Quote from: --Dynamic-Pascal-- on September 23, 2010, 10:45:23 amI have struggled with this problem enough (months...  ), and I need code examples that works. What did you try so far (in all those months) and where's the problem? ---------------------------------------------------------------------- --Dynamic-Pascal-- · 2010-09-23 09:48 I have tried getting the main window: Code: [Select]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; and it works fine. I could extract the window I want from this bitmap. I get in trouble if my target window is moved or resized. Another attempt getting the foreground window: Code: [Select]MyBitmap := TBitmap.Create; hWin := GetForegroundWindow; and I get my Lazarus window (as expected). ---------------------------------------------------------------------- theo · 2010-09-23 10:28 And this? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  wnd: HWnd;  aDC:HDC;begin  wnd := FindWindow('Notepad',nil);  if wnd>0 then  begin    aDC:=GetDC(wnd);    if aDC>0 then      Image1.Picture.Bitmap.LoadFromDevice(aDC);  end;end; ---------------------------------------------------------------------- --Dynamic-Pascal-- · 2010-09-23 14:46 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 ---------------------------------------------------------------------- theo · 2010-09-23 20:38 Quote from: --Dynamic-Pascal-- on September 23, 2010, 04:46:10 pmIt 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 This is pure Win-API. Please read in MSDN how things work. For example: http://msdn.microsoft.com/en-us/library/ms633499%28VS.85%29.aspx BTW: replace code with code=pascal manually. ---------------------------------------------------------------------- --Dynamic-Pascal-- · 2010-09-23 20:57 Thank You for your answers. I will try to get it working and then post a short example here if that's OK. ---------------------------------------------------------------------- --Dynamic-Pascal-- · 2010-09-25 17:00 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. 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". Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [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] ---------------------------------------------------------------------- captian jaster · 2010-09-25 22:24 Quote from: --Dynamic-Pascal-- on September 25, 2010, 07:00:00 pmI 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. 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". Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [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] OFFTOPIC: All that is used from the windows unit? Were can I find out more of the windows unit types/procedures and functions? the rtl doesn't say ONTopic: Nice code ---------------------------------------------------------------------- typo · 2010-09-25 22:50 QuoteWere can I find out more of the windows unit types/procedures and functions? Put the cursor on the word "windows" in editor and choose Open File from the popup menu. ---------------------------------------------------------------------- captian jaster · 2010-09-26 02:25 thanks again typo