Solved

Get current running windows' title.

Posted on 2000-02-25
19
248 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

15 Experts available now in Live!

Get 1:1 Help Now