delphifindwindow

FindWindow in FM


I am trying to use findwindow api call in FM, I can use it find in a VCL application, but not FM.

Here is my code

    function WindowExists(titlename: string): boolean;
    var
      hwd: THandle;
    begin
      hwd := 0;
      hwd := FindWindow(nil, pchar(titlename));
      Result := False;
      if not(hwd = 0) then { window was found if not nil }
       Result := True;
   end;

and here is the error I get when I try to compile [dcc32 Error] global_proc.pas(62): E2010 Incompatible types: 'HWND' and 'Pointer'

What I am doing wrong?


Solution

  • It seems likely that your problem is that your code is finding FMX.Platform.Win.FindWindow rather than Winapi.Windows.FindWindow. Furthermore FindWindow returns an HWND and not a THandle.

    Your code should be like this:

    function WindowExists(const TitleName: string): Boolean;
    var
      wnd: HWND;
    begin
      wnd := Winapi.Windows.FindWindow(nil, PChar(TitleName));
      Result := wnd <> 0;
    end;
    

    or even

    function WindowExists(const TitleName: string): Boolean;
    begin
      Result := Winapi.Windows.FindWindow(nil, PChar(TitleName)) <> 0;
    end;
    

    Note that it is always pointless to write code like this:

    A := 0;
    A := 1;
    

    That is what your code did in essence in the first two lines of the function. It is also idiomatic to use the not equal operator <> instead of negating the equal operator.