unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
PFindWindowStruct = ^TFindWindowStruct;
TFindWindowStruct =
record
Caption:
string;
ClassName:
String;
WindowHandle: THandle;
end;
function EnumWindowsProc(hWindow: hWnd; lParam: LongInt): boolean;
stdcall;
var lpBuffer: PChar;
WindowCaptionFound: boolean;
ClassNameFound: boolean;
begin
GetMem(lpBuffer, 255);
result:=true;
WindowCaptionFound:=false;
ClassNameFound:=false;
try
if GetWindowText(hWindow, lpBuffer,255)>0
then
if Pos(PFindWindowStruct(lParam).Caption, StrPas(lpBuffer))>0
then WindowCaptionFound:=true;
if PFindWindowStruct(lParam).ClassName='
'
then
ClassNameFound:=true
else if GetClassName(hWindow, lpBuffer, 255)>0
then
if Pos(PFindWindowStruct(lParam).ClassName, StrPas(lpBuffer))>0
then ClassNameFound:=true;
if (WindowCaptionFound
and ClassNameFound)
then begin
PFindWindowStruct(lParam).WindowHandle:=hWindow;
result:=false;
end;
finally
FreeMem(lpBuffer, sizeof(lpBuffer^));
end;
end;
function FindAWindow(caption:
string; ClassName:
string): THandle;
var WindowInfo: TFindWindowStruct;
begin
with WindowInfo
do begin
caption:=caption;
className:=ClassName;
WindowHandle:=0;
EnumWindows(@EnumWindowsProc,LongInt(@WindowInfo));
result:=WindowHandle;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var TheWindowHandle: THandle;
begin
TheWindowHandle:=FindAWindow('
Delphi', '
');
if TheWindowHandle=0
then
ShowMessage('
Window not found!')
else
BringWindowToTop(TheWindowHandle);
end;
end.