Solved

Reading of graphical output of another application (D7)

Posted on 2011-09-30
6
269 Views
Last Modified: 2013-11-22
I have a very old application that doesn't have logging capabilities. It has only graphical representation of information needed. I attached link to a picture where different readings can be seen.
Vertical indicator with scale that has zero in the middle.

I need a code that can read and log that indicator's reading to a file, with frequency up to once per second.

I have an idea in general how it should be done but no idea how to code it.

So, in general:
1. it needs to get window handle base on window's name
2. make screenshot of rectangle one pixel wide and 100% of indicator's high
3. find current reading (background is constant, so, the needed reading is a border between color and background)
4. write it to a log file

I hope the description is clear.

sc.gif
0
Comment
Question by:3axap
  • 4
6 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 36895970
don't have time to piece it together. you had the methodology right.
here is how to get a window by its title
function SearchMainWindowByName(Start:String;Mid:String='';UpC:boolean=True):THandle;
type
 RR=Record
     S,M:String;
     L:integer;
     U:boolean;
     hWindow:HWND;
    end;
 pRR=^RR;
Var
 R:RR;
 function EnumProcess(hHwnd: HWND; lParam : integer): boolean; stdcall;
 var
  pPid : DWORD;
  title, ClassName : string;
  R:pRR;
 begin
  //if the returned value in null the
  //callback has failed, so set to false and exit.
  if (hHwnd=0) then result := false else
   begin
    R:=pRR(lParam);
    //additional functions to get more
    //information about a process.
    //get the Process Identification number.
{$ifdef VER140}
      GetWindowThreadProcessId(hHwnd,pPid);
{$else}
      GetWindowThreadProcessId(hHwnd,@pPid);
{$endif}
    SetLength(ClassName, 255);
    SetLength(ClassName,
              GetClassName(hHwnd,
                           PChar(className),
                           Length(className)));
    SetLength(title, 255);
    SetLength(title, GetWindowText(hHwnd, PChar(title), Length(title)));
    //Display the process information
    //by adding it to a list box
    if Assigned(SearchWindowList) Then SearchWindowList.Add
      ('Class Name = ' + className +
       '; Title = ' + title +
       '; HWND = '+ IntToStr(hHwnd) +
       '; Pid = ' + IntToStr(pPid));
    if R^.U Then
     begin
      Title:=UpperCase(title);
      ClassName:=UpperCase(ClassName);
     end;
    if Copy(title,1,R^.L)=R^.S Then
     begin
      if (R^.M='') Or (Pos(R^.M,title)>0)
        Then R^.hWindow:=hHwnd;
     end Else
    if Copy(ClassName,1,R^.L)=R^.S Then
     begin
      if (R^.M='') Or (Pos(R^.M,ClassName)>0)
        Then R^.hWindow:=hHwnd;
     end;
    Result := R^.hWindow=0;
  end;
 end;
begin
 R.hWindow:=0;
 if UpC Then
  begin
   R.S:=UpperCase(Start);
   R.M:=UpperCase(Mid);
  end Else
  begin
   R.S:=Start;
   R.M:=Mid;
  end;
 R.L:=Length(Start);
 R.U:=Upc;
 if Assigned(SearchWindowList) Then SearchWindowList.Clear;
 EnumWindows(@EnumProcess,Integer(@R));
 Result:=R.hWindow;
end;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
ID: 36895971
here is how to get the window as bitmap

ScreenShot(WinHandle, Bmp, True ); // true = client area
procedure ScreenShot(hWin:THandle;destBitmap : TBitmap;ClientArea:Boolean=True);
var
   w,h : integer;
   DC : HDC;
   r : TRect;
begin
 if ClientArea then
  begin
   dc := GetDC(hWin) ;
   GetClientRect(hWin,r);
  end else
  begin
   dc := GetWindowDC(hWin) ;
   GetWindowRect(hWin,r) ;
  end;
 w := r.Right - r.Left;
 h := r.Bottom - r.Top;
 try
  destBitmap.Width := w;
  destBitmap.Height := h;
  BitBlt(destBitmap.Canvas.Handle,
         0,
         0,
         destBitmap.Width,
         destBitmap.Height,
         DC,
         0,
         0,
         SRCCOPY) ;
 finally
  ReleaseDC(hWin, DC) ;
 end;
end;

Open in new window

0
 
LVL 25

Accepted Solution

by:
epasquier earned 400 total points
ID: 36895973
then with the bitmap, you use bmp.canvas.Pixels[X,Y] and test the color to find when it's not grey (starting from top to middle and bottom to middle)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:3axap
ID: 36896017
Pasquier, thank you.
Actually "test the color to find when it's not grey (starting from top to middle and bottom to middle)" is the hardest part for me :o)
I'll try to get those peices together.

Speed wise, base on your experience, is this methodology optimal enough to run with 1Hz frequency and don't eat much CPU time?
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36896045
sure.
0
 
LVL 26

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 100 total points
ID: 36909484

For detecting colors:

...
bmp: TBitmap;
...
if bmp.Canvas.Pixels[x, y] = clGray then ....

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ddeman not working in activex 3 93
Delphi Firemonkey Need Sample for Online Shopping Example. 2 120
Delphi Form ownership 4 72
Installshield for Embarcadero EX 10.1 Berlin 4 18
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now