Enum all windows caption into combobox


i want to enum all windows captions such as "Untitled - Notepad"
"program manager" if notepad is starting,

into combobox as text

Palli
lulliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

edeyCommented:
You can try something like this:

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

function doEnumWindows(handle : HWND;lparam : cardinal):boolean;
var
//   str : array[0..255] of char;
   str : pchar;
begin
     getMem(str,255);
     sendMessage(handle,WM_GETTEXT,255,integer(str));
     form1.ListBox1.Items.Add(str);
     freeMem(str);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     enumWindows(@doEnumWindows,0);
end;

end.




Though I think you'll find that there are more windows then you think :)


GL
Mike
0
lulliAuthor Commented:
This won't work
0
intheCommented:
this was to add the handle,exename and caption to a listbox but adding a line also adds the caption to a listbox:

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;
    ComboBox1: TComboBox;
    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);
  Form1.Combobox1.Items.Add(text);
  Form1.Combobox1.text := text;
  Result := True;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  EWProc: EnumWindowsProc;
  begin
  Combobox1.Clear;
  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.


Regards Barry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

intheCommented:
that shouldve read
adding the line:
Form1.Combobox1.Items.Add(text);
also adds the caption to a combo..
0
lulliAuthor Commented:
inthe-->>

this won't display all windows, i know about one window that are running and i can't see him , hmmm
0
edeyCommented:
Our suggestions only enumerate top level windows, is the one your looking for a child window?

GL
mike
0
DrDelphiCommented:
take a look at my site... www.drdelphi.com.

You can do a search for enumwindows to get the snippet.


Good luck
!!

0
lulliAuthor Commented:
inthe-->>

this won't display all windows, i know about one window that are running and i can't see him , hmmm
0
lulliAuthor Commented:
is i use this function, there will be too many windows,
is not to functions for this? you are the expert
0
edeyCommented:
here is an usenet article, courtesy of Ralph Freidman (Thanks Team B!) that gives an example of using enumWindows & enumChildWindows to list every window.

http://x37.deja.com/[ST_rn=ps]/getdoc.xp?AN=572020928&CONTEXT=948299217.1474953234&hitnum=0


GL
Mike
0
lulliAuthor Commented:
vuff,,,,,

this show to many windows, i just want windows that are active, programs that have been started e.g
Netscape, Lotus Notes, Delphi, Notepad, Microsoft Word.......
0
lulliAuthor Commented:
Hello!!!!!!!!!!!!!!!1
0
lulliAuthor Commented:
Hello!!!!!!!!!!!!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.