nafa2221
asked on
Window
How do I check to see if a window is active by its title. Lets say its iternet explorer and itnernet explorers title bar contains 'Microsoft Internet Explorer'...thanks
ASKER
I found the following function to find a window by title, but can you review the code and give me an example? Thanks.
function FindWindowChildByTitle( const ParentWnd : HWnd; const Title : string) : HWnd;
var
W1st, Wth : HWnd;
WinName : PChar;
begin
Result:=0;
W1st:=GetWindow(ParentWnd, GW_CHILD);
if W1st <> 0 then begin
WinName:=StrAlloc(256);
Wth:=W1st;
repeat
GetWindowText(Wth,WinName, 255);
if StrPas(WinName) = Title then begin
Result:=Wth;
break;
end;
Wth:=GetNextWindow(Wth, GW_HWNDNEXT);
until (Wth = 0)or(Wth = W1st);
StrDispose(WinName);
end;
end;
function FindWindowChildByTitle( const ParentWnd : HWnd; const Title : string) : HWnd;
var
W1st, Wth : HWnd;
WinName : PChar;
begin
Result:=0;
W1st:=GetWindow(ParentWnd,
if W1st <> 0 then begin
WinName:=StrAlloc(256);
Wth:=W1st;
repeat
GetWindowText(Wth,WinName,
if StrPas(WinName) = Title then begin
Result:=Wth;
break;
end;
Wth:=GetNextWindow(Wth, GW_HWNDNEXT);
until (Wth = 0)or(Wth = W1st);
StrDispose(WinName);
end;
end;
This is not what you asked for, this function presumes that you know the parent HWND. I have a good example of finding a window's hwnd on my webpage, if you want to take a look. I also have a fairly new component that I wrote called TWindowWorks which should help you. WWW.DrDelphi.Com
Good luck!!
Good luck!!
ASKER
I know the parent HWND its "AOL Frame25", and the title is "America Online". I am writeing a program for my cousin that keeps him on AOL. Also...dont even say I should do this in VB cuz I will slap you ;] Thanks
Hello,
look at the following unit.
this should work...
you can't make the test by clicking of a button because your application
will have the focus. Replace Form1
with what you want. Let me know if it's working or not.
I tested it and it is...
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
btnButton1: TButton;
procedure btnButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
fWindowClass: string;
fWindowCaption: string;
implementation
{$R *.DFM}
function getChildWinHandle(WinHandl e: THandle; FoundWindow: PHandle): BOOL; stdcall;
var
Buffer: array[0..50] of Char;
begin
Result := True;
// GetClassName(WinHandle, Buffer, SizeOf(Buffer) - 1);
// if pos(PChar(fWindowClass), Buffer) <> 0 then
// begin
// GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
// if Pos(fWindowCaption, Buffer) <> 0 then
// begin
// FoundWindow^ := WinHandle;
// Result := False;
// end;
// end;
GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
if Pos(fWindowCaption, Buffer) <> 0 then
begin
FoundWindow^ := WinHandle;
Result := False;
end;
end;
function getWindowHandle(WindowCapt ion: string; WindowClass: string): THandle;
begin
fWindowCaption := WindowCaption;
fWindowClass := WindowClass;
result := 0;
EnumWindows(@getChildWinHa ndle, Integer(@result));
end;
function isContinuusRunning: THandle;
begin
result := getWindowHandle('Form1', '');
end;
procedure TForm1.btnButton1Click(Sen der: TObject);
var
cMainWindow: THandle;
wp: TWindowPlacement;
begin
cMainWindow := isContinuusRunning;
GetWindowPlacement(cMainWi ndow, @wp);
showmessage(inttostr(wp.sh owCmd));
if wp.showCmd = SW_SHOWNORMAL then
showmessage('form is visible')
else
showmessage('form is not visible');
end;
end.
look at the following unit.
this should work...
you can't make the test by clicking of a button because your application
will have the focus. Replace Form1
with what you want. Let me know if it's working or not.
I tested it and it is...
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
btnButton1: TButton;
procedure btnButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
fWindowClass: string;
fWindowCaption: string;
implementation
{$R *.DFM}
function getChildWinHandle(WinHandl
var
Buffer: array[0..50] of Char;
begin
Result := True;
// GetClassName(WinHandle, Buffer, SizeOf(Buffer) - 1);
// if pos(PChar(fWindowClass), Buffer) <> 0 then
// begin
// GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
// if Pos(fWindowCaption, Buffer) <> 0 then
// begin
// FoundWindow^ := WinHandle;
// Result := False;
// end;
// end;
GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
if Pos(fWindowCaption, Buffer) <> 0 then
begin
FoundWindow^ := WinHandle;
Result := False;
end;
end;
function getWindowHandle(WindowCapt
begin
fWindowCaption := WindowCaption;
fWindowClass := WindowClass;
result := 0;
EnumWindows(@getChildWinHa
end;
function isContinuusRunning: THandle;
begin
result := getWindowHandle('Form1', '');
end;
procedure TForm1.btnButton1Click(Sen
var
cMainWindow: THandle;
wp: TWindowPlacement;
begin
cMainWindow := isContinuusRunning;
GetWindowPlacement(cMainWi
showmessage(inttostr(wp.sh
if wp.showCmd = SW_SHOWNORMAL then
showmessage('form is visible')
else
showmessage('form is not visible');
end;
end.
use one of those parameters to test the state of the program you want.
because it's not laways sw_shownormal:
found in windows.pas
SW_HIDE = 0;
{$EXTERNALSYM SW_SHOWNORMAL}
SW_SHOWNORMAL = 1;
{$EXTERNALSYM SW_NORMAL}
SW_NORMAL = 1;
{$EXTERNALSYM SW_SHOWMINIMIZED}
SW_SHOWMINIMIZED = 2;
{$EXTERNALSYM SW_SHOWMAXIMIZED}
SW_SHOWMAXIMIZED = 3;
{$EXTERNALSYM SW_MAXIMIZE}
SW_MAXIMIZE = 3;
{$EXTERNALSYM SW_SHOWNOACTIVATE}
SW_SHOWNOACTIVATE = 4;
{$EXTERNALSYM SW_SHOW}
SW_SHOW = 5;
{$EXTERNALSYM SW_MINIMIZE}
SW_MINIMIZE = 6;
{$EXTERNALSYM SW_SHOWMINNOACTIVE}
SW_SHOWMINNOACTIVE = 7;
{$EXTERNALSYM SW_SHOWNA}
SW_SHOWNA = 8;
{$EXTERNALSYM SW_RESTORE}
SW_RESTORE = 9;
{$EXTERNALSYM SW_SHOWDEFAULT}
SW_SHOWDEFAULT = 10;
{$EXTERNALSYM SW_MAX}
SW_MAX = 10;
because it's not laways sw_shownormal:
found in windows.pas
SW_HIDE = 0;
{$EXTERNALSYM SW_SHOWNORMAL}
SW_SHOWNORMAL = 1;
{$EXTERNALSYM SW_NORMAL}
SW_NORMAL = 1;
{$EXTERNALSYM SW_SHOWMINIMIZED}
SW_SHOWMINIMIZED = 2;
{$EXTERNALSYM SW_SHOWMAXIMIZED}
SW_SHOWMAXIMIZED = 3;
{$EXTERNALSYM SW_MAXIMIZE}
SW_MAXIMIZE = 3;
{$EXTERNALSYM SW_SHOWNOACTIVATE}
SW_SHOWNOACTIVATE = 4;
{$EXTERNALSYM SW_SHOW}
SW_SHOW = 5;
{$EXTERNALSYM SW_MINIMIZE}
SW_MINIMIZE = 6;
{$EXTERNALSYM SW_SHOWMINNOACTIVE}
SW_SHOWMINNOACTIVE = 7;
{$EXTERNALSYM SW_SHOWNA}
SW_SHOWNA = 8;
{$EXTERNALSYM SW_RESTORE}
SW_RESTORE = 9;
{$EXTERNALSYM SW_SHOWDEFAULT}
SW_SHOWDEFAULT = 10;
{$EXTERNALSYM SW_MAX}
SW_MAX = 10;
ASKER
Can u give me an example that will find to see if the window with the title America Online is running? Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Let's try again :
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
btnButton1: TButton;
procedure btnButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
fWindowClass: string;
fWindowCaption: string;
implementation
{$R *.DFM}
function getChildWinHandle(WinHandl e: THandle; FoundWindow: PHandle): BOOL; stdcall;
var
Buffer: array[0..50] of Char;
begin
Result := True;
GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
if Pos(fWindowCaption, Buffer) <> 0 then
begin
FoundWindow^ := WinHandle;
Result := False;
end;
end;
function getWindowHandle(WindowCapt ion: string; WindowClass: string): THandle;
begin
fWindowCaption := WindowCaption;
fWindowClass := WindowClass;
result := 0;
EnumWindows(@getChildWinHa ndle, Integer(@result));
end;
//finds the handle of the window
function isAOLRunning: THandle;
begin
result := getWindowHandle('America Online', '');
end;
procedure TForm1.btnButton1Click(Sen der: TObject);
var
cMainWindow: THandle;
wp: TWindowPlacement;
begin
cMainWindow := isAOLRunning;
if cMainWindow = 0 then
begin
showmessage('AOL is not running..');
exit;
end;
//this part is for checking the state of the running app
GetWindowPlacement(cMainWi ndow, @wp);
if wp.showCmd = SW_SHOWNORMAL then
showmessage('AOL is open')
else
showmessage('AOL is closed '+intToStr(wp.showCmd));
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
btnButton1: TButton;
procedure btnButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
fWindowClass: string;
fWindowCaption: string;
implementation
{$R *.DFM}
function getChildWinHandle(WinHandl
var
Buffer: array[0..50] of Char;
begin
Result := True;
GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
if Pos(fWindowCaption, Buffer) <> 0 then
begin
FoundWindow^ := WinHandle;
Result := False;
end;
end;
function getWindowHandle(WindowCapt
begin
fWindowCaption := WindowCaption;
fWindowClass := WindowClass;
result := 0;
EnumWindows(@getChildWinHa
end;
//finds the handle of the window
function isAOLRunning: THandle;
begin
result := getWindowHandle('America Online', '');
end;
procedure TForm1.btnButton1Click(Sen
var
cMainWindow: THandle;
wp: TWindowPlacement;
begin
cMainWindow := isAOLRunning;
if cMainWindow = 0 then
begin
showmessage('AOL is not running..');
exit;
end;
//this part is for checking the state of the running app
GetWindowPlacement(cMainWi
if wp.showCmd = SW_SHOWNORMAL then
showmessage('AOL is open')
else
showmessage('AOL is closed '+intToStr(wp.showCmd));
end;
end.
Tussin, you only check if the form exists.
Not if it is active ;)
Not if it is active ;)
well, i guess that can be done with a few lines of code. may be this is already in the answers provided. i didn't check them all. (They appeared too long and i didn't have the time. sorry folks.)
//declare global variables in your unit.
const partialtitle = 'partial title of the window you want to find';
var HWND_aol : HWND;
function ewp(h: HWND; var l : longint):boolean;stdcall;
var c : array[0..255] of char;
begin
GetWindowText(h, @c, 255);
if pos(partialtitle, StrPas(@c)) <> 0 then
begin;
HWND_AOL := h;
Result := false; //stop if window found.
end
else Result := true;
end;
//in your code...
begin;
hwnd_aol := 0;
enumWindows(@ewp, 0); //find the handle of aol
if HWND_aol <> 0 then
begin;
if (GetActiveWindow = hwnd_AOL) then
ShowMessage('AOL is active Window')
else ShowMessage('AOL is running, but is not the active Window');
end
else ShowMessage('AOL is NOT runningn');
end;
//declare global variables in your unit.
const partialtitle = 'partial title of the window you want to find';
var HWND_aol : HWND;
function ewp(h: HWND; var l : longint):boolean;stdcall;
var c : array[0..255] of char;
begin
GetWindowText(h, @c, 255);
if pos(partialtitle, StrPas(@c)) <> 0 then
begin;
HWND_AOL := h;
Result := false; //stop if window found.
end
else Result := true;
end;
//in your code...
begin;
hwnd_aol := 0;
enumWindows(@ewp, 0); //find the handle of aol
if HWND_aol <> 0 then
begin;
if (GetActiveWindow = hwnd_AOL) then
ShowMessage('AOL is active Window')
else ShowMessage('AOL is running, but is not the active Window');
end
else ShowMessage('AOL is NOT runningn');
end;
well, i guess that can be done with a few lines of code. may be this is already in the answers provided. i didn't check them all. (They appeared too long and i didn't have the time. sorry folks.)
//declare global variables in your unit.
const partialtitle = 'partial title of the window you want to find';
var HWND_aol : HWND;
function ewp(h: HWND; var l : longint):boolean;stdcall;
var c : array[0..255] of char;
begin
GetWindowText(h, @c, 255);
if pos(partialtitle, StrPas(@c)) <> 0 then
begin;
HWND_AOL := h;
Result := false; //stop if window found.
end
else Result := true;
end;
//in your code...
begin;
hwnd_aol := 0;
enumWindows(@ewp, 0); //find the handle of aol
if HWND_aol <> 0 then
begin;
if (GetActiveWindow = hwnd_AOL) then
ShowMessage('AOL is active Window')
else ShowMessage('AOL is running, but is not the active Window');
end
else ShowMessage('AOL is NOT runningn');
end;
//declare global variables in your unit.
const partialtitle = 'partial title of the window you want to find';
var HWND_aol : HWND;
function ewp(h: HWND; var l : longint):boolean;stdcall;
var c : array[0..255] of char;
begin
GetWindowText(h, @c, 255);
if pos(partialtitle, StrPas(@c)) <> 0 then
begin;
HWND_AOL := h;
Result := false; //stop if window found.
end
else Result := true;
end;
//in your code...
begin;
hwnd_aol := 0;
enumWindows(@ewp, 0); //find the handle of aol
if HWND_aol <> 0 then
begin;
if (GetActiveWindow = hwnd_AOL) then
ShowMessage('AOL is active Window')
else ShowMessage('AOL is running, but is not the active Window');
end
else ShowMessage('AOL is NOT runningn');
end;
well, after typing it, my comments looks pretty long as well. so may be it ain't an improvement after all.
To keep the connection, can't you just have a timer, which after every 300000 milliseconds does a:
ShellExecute( 0, 'ping www.altavista.com', nil, nil, SW_HIDE ) ;
Or something...
Dont understand AOL though, so I am probably wrong :O)
Tim.
ShellExecute( 0, 'ping www.altavista.com', nil, nil, SW_HIDE ) ;
Or something...
Dont understand AOL though, so I am probably wrong :O)
Tim.
WinCaption:array[0..255] of char;
begin
WinHandle:=GetForegroundwi
GetwindowText (WinHandle,WinCaption,254)
showmessage(WinCaption);