Solved

Window

Posted on 2000-04-03
14
168 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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now