[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

find window handle

Hi!

Imagine I have 2 applications opened, and they were both made using delphi!

their classes are the same (TForm).
The first one has no title.
The second has the title : "NoName - ApplicationName"
Note that the NoName can be changed.

How can I find the handle of the second one, knowing only that it has the string "ApplicationName"?


Hope that my english was clear enough!


Thanks

Joao
0
jpcs
Asked:
jpcs
  • 3
  • 2
  • 2
  • +1
1 Solution
 
MadshiCommented:
Use this one:

FindWindow('TForm', 'NoName - ApplicationName');

Or, if "NoName" can change, enumerate all windows by using "EnumWindows".

Or, if you have D4 or D5, you can download the free unit "enumStuff.pas" from my homepage

http://beam.to/madshi

Then you can use the function GetWindowList. It's a bit easier to use than EnumWindows.

Regards, Madshi.
0
 
hubdogCommented:
var
  neededhandle:THandle;
Function WindowCallback(WHandle : HWnd; Var Parm : Pointer) : Boolean; stdcall;
{This function is called once for each window}
Var winString : PChar;
begin
    winString := Allocmem(255);
    GetWindowText(WHandle,winString,255);
    if AnsiStrPos(winstring,'ApplicationName')<>nil then
  begin
    neededhandle:=whandle;//get the window you needed
    result:=false;//stop enumerate
  end
  else
    Result := True; {not find the window. Continue to enumerate windows}
  FreeMem(winString,255);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  EnumWindows(@WindowCallback,0);
end;

good luck

hubdog
0
 
intheCommented:
listening
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.

 
jpcsAuthor Commented:
Thanks people.

I've made a simple routine that checks it :

function comparestring (str1, str2 : string) : boolean;
var temp, maior, menor : string;
    i : integer;
begin
   if comparetext (str1, str2) = 0 then
   begin
      comparestring := true;
      exit;
   end;

   if length (str1) > length (str2) then
   begin
      maior := str1;
      menor := str2;
   end
   else
   begin
      maior := str2;
      menor := str1;
   end;
   if ( (maior<> '') AND (menor <> '') ) then
      for i := 0 to length (maior)-length (menor) do
      begin
         temp := maior;
         delete (temp, 1, i);
         setlength (temp, length (menor));

         if comparetext (temp, menor) = 0 then
         begin
            comparestring := true;
            exit;
         end;
      end;
   comparestring := false;
end;


I'm sorry, but I thought that there was some function that made it easier.

Thanks Madshi! Once again, i'm using your great unit. Nice Work.


Thanks people.


Joso
0
 
intheCommented:
you can alos do:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  capt : Array[0..99] of Char;
  hh : THandle;
implementation

{$R *.DFM}

Function TCallBackChildren(h : THandle;L : Longint):longBool;stdcall;
begin
  GetwindowText(h,@capt[0],100);
  if Pos('ApplicationName',StrPas(@capt[0])) <> 0 then
    hh := h;
  Result := True;
end;

Function TCallBackParent(H : THandle;L : Longint):longBool;stdcall;
begin
  GetwindowText(h,@capt[0],100);
  if Pos('ApplicationName',StrPas(@capt[0])) <> 0 then
  begin
    hh := h;
  end
  else
    EnumChildWindows(h,@TCallBackChildren,L);
  Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var LL : Longint;
begin
  hh := 0;
  EnumWindows(@TCallBackParent,LL);
  if hh <> 0 then
  begin
Showmessage('yes we found second app');
end
else showmessage('nope second app not found');
  end;

end.


Regards Barry
0
 
MadshiCommented:
So I guess I can post an answer?   :-)
0
 
jpcsAuthor Commented:
yes!


Keep on the goo work on the unit.
0
 
MadshiCommented:
Well, I have already worked a lot on that unit (and other stuff). Watch my homepage - somewhen in the next weeks there will come a lot of new stuff. But probably I will only make the dcus available for free.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now