?
Solved

Enum all windows caption into combobox

Posted on 2000-01-18
13
Medium Priority
?
420 Views
Last Modified: 2010-04-06

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

into combobox as text

Palli
0
Comment
Question by:lulli
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 6

Expert Comment

by:edey
ID: 2364023
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
 

Author Comment

by:lulli
ID: 2364046
This won't work
0
 
LVL 17

Accepted Solution

by:
inthe earned 120 total points
ID: 2364071
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
LVL 17

Expert Comment

by:inthe
ID: 2364074
that shouldve read
adding the line:
Form1.Combobox1.Items.Add(text);
also adds the caption to a combo..
0
 

Author Comment

by:lulli
ID: 2364109
inthe-->>

this won't display all windows, i know about one window that are running and i can't see him , hmmm
0
 
LVL 6

Expert Comment

by:edey
ID: 2364170
Our suggestions only enumerate top level windows, is the one your looking for a child window?

GL
mike
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2364325
take a look at my site... www.drdelphi.com.

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


Good luck
!!

0
 

Author Comment

by:lulli
ID: 2366047
inthe-->>

this won't display all windows, i know about one window that are running and i can't see him , hmmm
0
 

Author Comment

by:lulli
ID: 2366081
is i use this function, there will be too many windows,
is not to functions for this? you are the expert
0
 
LVL 6

Expert Comment

by:edey
ID: 2367465
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
 

Author Comment

by:lulli
ID: 2367584
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
 

Author Comment

by:lulli
ID: 2369318
Hello!!!!!!!!!!!!!!!1
0
 

Author Comment

by:lulli
ID: 2369319
Hello!!!!!!!!!!!!!!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

601 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