Solved

Get current running windows' title.

Posted on 2000-02-25
19
254 Views
Last Modified: 2010-04-04
I need to find out the current visible running windows' title. Included sub windows under each application like a error msg window or a "Save As" window.

Thanks

Jammy
0
Comment
Question by:jlislo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
  • +3
19 Comments
 
LVL 6

Expert Comment

by:edey
ID: 2558877
you can use getForeGroundWindow to get the active window handle:

The GetForegroundWindow function returns the handle of the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.

HWND GetForegroundWindow(VOID)


and then call enumChildWindows to get all of it's children



GL
Mike
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2558950
use this:

procedure TForm1.Button1Click(Sender: TObject);
var a: array[0..255] of Char;
begin
  GetWindowText(GetForegroundWindow, a, 255);
  ShowMessage(a);
end;


Epsylon.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2558957
Using Sleep(3000) gives you 3 seconds to make another window active....


procedure TForm1.Button1Click(Sender: TObject);
var a: array[0..255] of Char;
begin
  Sleep(3000);
  GetWindowText(GetForegroundWindow, a, 255);
  ShowMessage(a);
end;
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!

 

Author Comment

by:jlislo
ID: 2558959
I am very good at API call can you show me some sample code. Thanks.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2558973
Epsylon changed the proposed answer to a comment
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2558976
This is more like it:

procedure TForm1.Button1Click(Sender: TObject);
var a: PChar;
    wnd: HWND;
    len: Integer;
begin
  Sleep(2000);
  wnd := GetForegroundWindow;
  len := GetWindowTextLength(wnd);
  a := AllocMem(len+1);
  GetWindowText(wnd, a, len+1);
  ShowMessage(a);
  FreeMem(a);
end;
0
 

Author Comment

by:jlislo
ID: 2558993
Epsylon,

I also need to know other application's windows title not only the one I am running. Thanks.
0
 

Author Comment

by:jlislo
ID: 2559013
Epsylon,

I also need to know other application's windows title not only the one I am running. Thanks.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2559187
This finds all controls. Don't know how to filter windows of of it.


function FindName(wnd:HWND): String;
var a: PChar;
    len: Integer;
begin
  len := GetWindowTextLength(wnd);
  a := AllocMem(len+1);
  GetWindowText(wnd, a, len+1);
  Result := a;
  FreeMem(a);
end;

function CallBack(wnd: HWND; x: Cardinal): Boolean; stdcall;
begin
  ShowMessage(FindName(wnd));
  Result := true;
end;

procedure TForm1.Button1Click(Sender: TObject);
var wnd: HWND;
begin
  Sleep(2000);
  wnd := GetForegroundWindow;
  ShowMessage(FindName(wnd));
  EnumChildWindows(wnd, @CallBack, 0);
end;
0
 

Author Comment

by:jlislo
ID: 2559271
Epsylon,

This one still give me only the application windows. I have Delphi, outlook and explore running it didn't return any windows' title of those.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2559414
here an example to get all running programs windows caption ,handle etc.:


unit Unit1;

interface

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

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

var
  Form1: TForm1;
 
implementation
 
{$R *.DFM}

type
  EnumWindowsProc = function (Hwnd: THandle;Param: Pointer): Boolean; stdcall;

function GetWindowExeName(Handle: THandle): String;
var
  PE: TProcessEntry32;
  Snap: THandle;
  ProcessId: cardinal;
begin
  GetWindowThreadProcessId(Handle,@ProcessId);
  Snap:= CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if Snap <> 0 then begin
    if Process32First(Snap, PE) then
      if PE.th32ProcessID = ProcessId then
        Result:= String(PE.szExeFile)
      else while Process32Next(Snap, PE) do
        if PE.th32ProcessID = ProcessId then begin
          Result:= String(PE.szExeFile);
          break;
        end;
    CloseHandle(Snap);
  end;
end;

function GetTitle (Hwnd: THandle; Param: Pointer): Boolean; stdcall;
 var
  Text,TempString: string;
begin
  If (GetWindowLong(Hwnd,GWL_HWNDPARENT)=0) and (IsWindowVisible(Hwnd) or IsIconic(Hwnd))then
  begin
TempString := GetWindowExeName(Hwnd);
  SetLength (Text, 100);
  GetWindowText (Hwnd, PChar (Text), 100);
  Form1.ListBox1.Items.Add (IntToStr (Hwnd) + ' : '  + TempString + ' : ' +text);
  Result := True;
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  EWProc: EnumWindowsProc;
begin
  ListBox1.Items.Clear;
  EWProc := GetTitle;
  EnumWindows (@EWProc, 0);
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
wnd :Hwnd;
temp :string;
EWProc: EnumWindowsProc;
ClickedOK :  boolean;
begin
clickedOK := InputQuery('Which Process do U wanna Kill','Process Number',temp);
if ClickedOK then
begin
Wnd := hwnd(strToInt(temp));
PostMessage(Wnd,WM_SYSCOMMAND,SC_CLOSE, 0);
  sleep(100);
  ListBox1.Items.Clear;
  EWProc := GetTitle;
  EnumWindows (@EWProc, 0);
end;
end;
 
end.



you probably dont want button2 click code  (unless you want to kill other windows)

you can also change the line:
Form1.ListBox1.Items.Add(IntToStr (Hwnd) + ' : '  + TempString + ' : ' +text);

to just :

Form1.ListBox1.Items.Add(text);
(text holds each windows title/caption in loop)

Regards Barry
0
 

Author Comment

by:jlislo
ID: 2559565
I saw the result you write for karen. I add the following lines

  showmessage(inttostr(GetWindowLong(Hwnd,GWL_HWNDPARENT))) ;

  if  IsWindowVisible(Hwnd)
  then showmessage('T') else showmessage('F');

  if  IsWindowVisible(Hwnd)
  then showmessage('T') else showmessage('F');

to check the GetTitle function because I got nothing in listbox (I am using D5). The showmessage return 0,F and F that tell me it never get into the if statement to collect information. Maybe you can tell me what I did wrong here. Thanks
0
 

Author Comment

by:jlislo
ID: 2559574
Delphi 5 + Windows 2000 perf.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2559620
yes you cant use toolhelp32 functions  (in unit TlHelp32) .unfortunaltly these are not supported in win2k ..therefore the code i post is useless for you.
0
 

Author Comment

by:jlislo
ID: 2559645
Thank you anyway.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2559675
Epsylon changed the proposed answer to a comment
0
 
LVL 20

Accepted Solution

by:
Madshi earned 150 total points
ID: 2560519
Epsylon, Barry,

*PLEASE* don't declare your callback functions like this:

function CallBack(wnd: HWND; x: Cardinal): Boolean; stdcall;
function GetTitle (Hwnd: THandle; Param: Pointer): Boolean; stdcall;
 
The result type has to be "BOOL", which is a LongBoolean (= 4 bytes), not a Boolean (= 1 byte).

jlislo, you can download the free unit "enumStuff.pas" from my homepage

http://beam.to/madshi

With this unit you can enumerate the top level windows of all applications easily.

Look at GetWindowList and GetChildWindowList.

Regards, Madshi.
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2563276
Here yar.... thanks to Inthe, my program is possible :) THANKS Barry...... All the source code is included, i gets handles and everything else as well.....

http://members.xoom.com/craig_c/mouse.zip

Thanks again Barry....


Craig C.
0
 

Author Comment

by:jlislo
ID: 2564933
Thank you very much!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

632 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