Solved

Reading of graphical output of another application (D7)

Posted on 2011-09-30
6
280 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 125
Delphi XE10 Round Image 2 160
Delphi: making a BW image transparent 10 61
Browsing a TTreeView in Delphi 5 9
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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