uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TmyForm = class(TForm)
EnumTimes: TMemo;
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
fEnumCalled:TDateTime;
fFindCalled:TDateTime;
fFindTime:Integer;
public
end;
var
myForm: TmyForm;
implementation
{$R *.dfm}
uses
DateUtils;
//Careful with definition of this routine (dont forget stdcall)
//otherwise your parameters wont be passed in good order
function OnWindowFound(wnd:HWND; myParam:Integer):Boolean; stdcall;
var
sender:TMyForm;
sWindowName:string;
nEnumTime:integer;
processID:DWORD;
begin
//in this example myParam will contain reference to a TMyForms object
sender:=TMyForm(myParam);
//Get the title of this window
SetLength(sWindowName,255);
GetWindowText(wnd,Pchar(sWindowName),255);
//Because in your case most windows are in a different process it is
//slightly more efficient if you use SendMessage to get the window name
//SendMessage(wnd,WM_GETTEXT,255,pChar(sWindowName));
//Calculate the time taken up to here
nEnumTime:=MillisecondsBetween(Now,myform.fEnumCalled);
//Add the window and time taken to find it in EnumTimes Memo lines
sender.EnumTimes.Lines.Add(Format('EnumWindow took %d milliseconds to find '+sWindowName,[nEnumTime]));
//Continue looking for more windows
result:=true; //return false if you found what you wanted
end;
procedure TmyForm.Button1Click(Sender: TObject);
var
nEnumTime:integer;
lp:Integer;
begin
//Time of EnumWindows call
fEnumCalled:=Now();
EnumWindows(@OnWindowFound,integer(self));
nEnumTime:=MilliSecondsBetween(Now,self.fEnumCalled);
//Add the time that enum took to complete in EnumTimes list
self.EnumTimes.Lines.Add(Format('EnumWindow returned after %d milliseconds',[nEnumTime]));
end;
procedure TmyForm.Button2Click(Sender: TObject);
var
nFindTime:integer;
p:PChar;
sWindowName:string;
wnd:HWND;
begin
//Time of FindWindow call
fFindCalled:=Now();
wnd:=FindWindow('TMyForm',nil);
if wnd<>0 then
begin
//How long did it take ?
nFindTime:=MilliSecondsBetween(Now,fFindCalled);
//Show how long did FindWindow take
ShowMessage(Format('Find window took %d milliseconds',[nFindTime]));
//Retreive the window caption. (this time using SendMessage (just another way))
SetLength(sWindowName,255);
SendMessage(wnd,WM_GETTEXT,255,integer(p));
end
else
ShowMessage('Could not find any window of class TMyForm');
end;