Board index » delphi » Help with API 'findwindow' in delphi

Help with API 'findwindow' in delphi

Want to use the calculater in an application I am writing.

procedure TForm1.Button1Click(Sender: TObject);
const
   wname : pchar = 'Calculator';
   null : pchar = '';
var
   chnd,t : Thandle;

begin
   chnd := findWindow(null,wname);
   if chnd = 0 then
      chnd := winexec('calc.exe',SW_SHOW)
   else
      t := setFocus(chnd);
   end;
end;

find window returns a handle = 0  ie. not found???

Thanks

Steve..............

 

Re:Help with API 'findwindow' in delphi


Hi there,

It's better to use the ClassName of an app (if you know it :-) when using
the FindWindow call. The following code works perfectly (just cut and
paste! :-)...

Notes:
1) you don't (always) need to declare a CONST of type PChar when
something requires a PChar as a parameter (as you did in the code you
posted).
2) Borland allows you to call functions as if they were procedures so you
don't need to bother assigning a return value...

procedure TForm1.Button1Click(Sender: TObject);
var
  chnd: HWnd;
begin
   chnd:=findWindow('SciCalc',nil);     { <- SciCalc is the ClassName  }
   if chnd = 0 then                     { the Windows 3.1x calculator  }
     chnd:=winexec('calc.exe',SW_SHOW)
   else
     WinProcs.SetFocus(chnd);           { <-'SetFocus' is ALSO a method }
   end;                                 { of TFORM so be careful!       }
end;

Cheers,
G.---

Other Threads