Recent

Author Topic: Find specific window and retrieve it's content (windows VISTA)  (Read 13571 times)

--Dynamic-Pascal--

  • Newbie
  • Posts: 5
Find specific window and retrieve it's content (windows VISTA)
« on: September 23, 2010, 10:45:23 am »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #1 on: September 23, 2010, 11:27:22 am »
I 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--

  • Newbie
  • Posts: 5
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #2 on: September 23, 2010, 11:48:40 am »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #3 on: September 23, 2010, 12:28:16 pm »
And this?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   wnd: HWnd;
  4.   aDC:HDC;
  5. begin
  6.   wnd := FindWindow('Notepad',nil);
  7.   if wnd>0 then
  8.   begin
  9.     aDC:=GetDC(wnd);
  10.     if aDC>0 then
  11.       Image1.Picture.Bitmap.LoadFromDevice(aDC);
  12.   end;
  13. end;      

--Dynamic-Pascal--

  • Newbie
  • Posts: 5
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #4 on: September 23, 2010, 04:46:10 pm »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #5 on: September 23, 2010, 10:38:19 pm »
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

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.
« Last Edit: September 23, 2010, 10:40:05 pm by theo »

--Dynamic-Pascal--

  • Newbie
  • Posts: 5
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #6 on: September 23, 2010, 10:57:56 pm »
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--

  • Newbie
  • Posts: 5
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #7 on: September 25, 2010, 07:00:00 pm »
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][+][-]
  1. [tt][size=8pt]unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls, ExtCtrls, Buttons;
  10.  
  11. type
  12.   { TForm1 }
  13.   TForm1 = class(TForm)
  14.     BitBtn1: TBitBtn;
  15.     Image1: TImage;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     procedure BitBtn1Click(Sender: TObject);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.    Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31. //procedure FindTheWindow;
  32. procedure TForm1.BitBtn1Click(Sender: TObject);
  33. var
  34.    wnd: HWnd;
  35.    aDC:HDC;
  36.    coord: tpoint;
  37.  
  38. begin
  39.      coord := Mouse.CursorPos;
  40.      Form1.Label1.Caption := ('X=' + IntToStr(coord.X));
  41.      Form1.Label2.Caption := ('Y=' + IntToStr(coord.Y));
  42.      Form1.Label1.Show;
  43.      Form1.Label2.Show;
  44.      Form1.Hide;
  45.      Sleep(300);
  46.      wnd := WindowFromPoint(coord);
  47.      if wnd>0 then
  48.      begin
  49.           aDC:=GetDC(wnd);
  50.           if aDC>0 then
  51.           Form1.Image1.Picture.Bitmap.LoadFromDevice(aDC);
  52.           form1.Show;
  53.      end;
  54. end;
  55.  
  56. end.[/size] [/tt]

captian jaster

  • Guest
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #8 on: September 26, 2010, 12:24:49 am »
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][+][-]
  1. [tt][size=8pt]unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls, ExtCtrls, Buttons;
  10.  
  11. type
  12.   { TForm1 }
  13.   TForm1 = class(TForm)
  14.     BitBtn1: TBitBtn;
  15.     Image1: TImage;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     procedure BitBtn1Click(Sender: TObject);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.    Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31. //procedure FindTheWindow;
  32. procedure TForm1.BitBtn1Click(Sender: TObject);
  33. var
  34.    wnd: HWnd;
  35.    aDC:HDC;
  36.    coord: tpoint;
  37.  
  38. begin
  39.      coord := Mouse.CursorPos;
  40.      Form1.Label1.Caption := ('X=' + IntToStr(coord.X));
  41.      Form1.Label2.Caption := ('Y=' + IntToStr(coord.Y));
  42.      Form1.Label1.Show;
  43.      Form1.Label2.Show;
  44.      Form1.Hide;
  45.      Sleep(300);
  46.      wnd := WindowFromPoint(coord);
  47.      if wnd>0 then
  48.      begin
  49.           aDC:=GetDC(wnd);
  50.           if aDC>0 then
  51.           Form1.Image1.Picture.Bitmap.LoadFromDevice(aDC);
  52.           form1.Show;
  53.      end;
  54. end;
  55.  
  56. 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 :P
ONTopic:
Nice code

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #9 on: September 26, 2010, 12:50:22 am »
Quote
Were 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

  • Guest
Re: Find specific window and retrieve it's content (windows VISTA)
« Reply #10 on: September 26, 2010, 04:25:30 am »
thanks again typo

 

TinyPortal © 2005-2018