Solved

Reading of graphical output of another application (D7)

Posted on 2011-09-30
6
268 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 25

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

17 Experts available now in Live!

Get 1:1 Help Now