Solved

Reading of graphical output of another application (D7)

Posted on 2011-09-30
6
277 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

778 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