Solved

Window

Posted on 2000-04-03
14
171 Views
Last Modified: 2010-04-04
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
Comment
Question by:nafa2221
  • 4
  • 3
  • 3
  • +3
14 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2680623
var winhandle:Thandle;
    WinCaption:array[0..255] of char;
begin
   WinHandle:=GetForegroundwindow;
   GetwindowText   (WinHandle,WinCaption,254);
   showmessage(WinCaption);




0
 

Author Comment

by:nafa2221
ID: 2680627
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
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2680670
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:nafa2221
ID: 2680683
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
 
LVL 4

Expert Comment

by:jeurk
ID: 2680684
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
 
LVL 4

Expert Comment

by:jeurk
ID: 2680700
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
 

Author Comment

by:nafa2221
ID: 2680735
Can u give me an example that will find to see if the window with the title America Online is running? Thanks
0
 
LVL 2

Accepted Solution

by:
Tussin earned 50 total points
ID: 2680791
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
 
LVL 4

Expert Comment

by:jeurk
ID: 2680918
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
 
LVL 4

Expert Comment

by:jeurk
ID: 2680920
Tussin, you only check if the form exists.
Not if it is active ;)
0
 
LVL 7

Expert Comment

by:ahalya
ID: 2681022
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
 
LVL 7

Expert Comment

by:ahalya
ID: 2681045
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
 
LVL 7

Expert Comment

by:ahalya
ID: 2681068
well, after typing it, my comments looks pretty long as well. so may be it ain't an improvement after all.
0
 
LVL 1

Expert Comment

by:AttarSoftware
ID: 2683190
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question