Go Premium for a chance to win a PS4. Enter to Win

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

Get current running windows' title.

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
jlislo
Asked:
jlislo
  • 8
  • 6
  • 2
  • +3
1 Solution
 
edeyCommented:
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
 
EpsylonCommented:
use this:

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


Epsylon.
0
 
EpsylonCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jlisloAuthor Commented:
I am very good at API call can you show me some sample code. Thanks.
0
 
EpsylonCommented:
Epsylon changed the proposed answer to a comment
0
 
EpsylonCommented:
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
 
jlisloAuthor Commented:
Epsylon,

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

I also need to know other application's windows title not only the one I am running. Thanks.
0
 
EpsylonCommented:
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
 
jlisloAuthor Commented:
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
 
intheCommented:
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
 
jlisloAuthor Commented:
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
 
jlisloAuthor Commented:
Delphi 5 + Windows 2000 perf.
0
 
intheCommented:
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
 
jlisloAuthor Commented:
Thank you anyway.
0
 
EpsylonCommented:
Epsylon changed the proposed answer to a comment
0
 
MadshiCommented:
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
 
craig_capelCommented:
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
 
jlisloAuthor Commented:
Thank you very much!
0

Featured Post

Industry Leaders: 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!

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