Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

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
0
nafa2221
Asked:
nafa2221
  • 4
  • 3
  • 3
  • +3
1 Solution
 
DrDelphiCommented:
var winhandle:Thandle;
    WinCaption:array[0..255] of char;
begin
   WinHandle:=GetForegroundwindow;
   GetwindowText   (WinHandle,WinCaption,254);
   showmessage(WinCaption);




0
 
nafa2221Author Commented:
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;
0
 
DrDelphiCommented:
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!!


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nafa2221Author Commented:
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
0
 
jeurkCommented:
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(WinHandle: 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(WindowCaption: string; WindowClass: string): THandle;
begin
  fWindowCaption := WindowCaption;
  fWindowClass := WindowClass;
  result := 0;
  EnumWindows(@getChildWinHandle, Integer(@result));
end;

function isContinuusRunning: THandle;
begin
  result := getWindowHandle('Form1', '');
end;

procedure TForm1.btnButton1Click(Sender: TObject);
var
  cMainWindow: THandle;
  wp: TWindowPlacement;
begin
  cMainWindow := isContinuusRunning;

  GetWindowPlacement(cMainWindow, @wp);
  showmessage(inttostr(wp.showCmd));
  if wp.showCmd = SW_SHOWNORMAL then
    showmessage('form is visible')
  else
    showmessage('form is not visible');
end;

end.
0
 
jeurkCommented:
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;
0
 
nafa2221Author Commented:
Can u give me an example that will find to see if the window with the title America Online is running? Thanks
0
 
TussinCommented:
to nafa2221,
This code is absolutely what you need.
Function "FindWindowThatContainSubString"
Note: see example in code of Button1.OnClick
(Find a window that has "Mircrosoft Internet Explorer" and show its full text!)

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

function FindWindowThatContainSubString(const ParentWnd:HWND;const SubStr:string):HWND;
var s:array[0..256] of Char;
    h:HWND;
begin
  Result:=0;
  GetWindowText(ParentWnd,s,255);
  if Pos(SubStr,StrPas(s))>0 then
  begin
    Result:=ParentWnd;
    exit;
  end;
  h:=GetWindow(ParentWnd,GW_CHILD);
  if h=0 then
    Exit;
  repeat
    Result:=FindWindowThatContainSubString(h,SubStr);
    if Result<>0 then
      Exit;
    h:=GetNextWindow(h,GW_HWNDNEXT);
  until (h=0) or (h=ParentWnd);
end;

procedure TForm1.Button1Click(Sender: TObject);
var FullWindowText:array[0..256] of char;
    MatchedWindow:HWND;
begin
  MatchedWindow:=FindWindowThatContainSubString(GetDesktopWindow,'Microsoft Internet Explorer');
  GetWindowText(MatchedWindow,FullWindowText,255);
  ShowMessage(StrPas(FullWindowText));
end;

end.
0
 
jeurkCommented:
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(WinHandle: 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(WindowCaption: string; WindowClass: string): THandle;
begin
  fWindowCaption := WindowCaption;
  fWindowClass := WindowClass;
  result := 0;
  EnumWindows(@getChildWinHandle, Integer(@result));
end;

//finds the handle of the window
function isAOLRunning: THandle;
begin
  result := getWindowHandle('America  Online', '');
end;

procedure TForm1.btnButton1Click(Sender: 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(cMainWindow, @wp);

  if wp.showCmd = SW_SHOWNORMAL then
    showmessage('AOL is open')
  else
    showmessage('AOL is closed '+intToStr(wp.showCmd));
end;

end.
0
 
jeurkCommented:
Tussin, you only check if the form exists.
Not if it is active ;)
0
 
ahalyaCommented:
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;
0
 
ahalyaCommented:
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;
0
 
ahalyaCommented:
well, after typing it, my comments looks pretty long as well. so may be it ain't an improvement after all.
0
 
AttarSoftwareCommented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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