Solved

Listview and their associated Icons .

Posted on 2003-11-03
2
336 Views
Last Modified: 2010-04-05
Hello Experts.

 Please help me with this code:(tip on Swissdelphicenter )

{Enumerate processes and terminate them }

interface  

uses  
 {...,}TLHelp32 {important !}  

// Global Variables, Globale Variablen  

var  
  aSnapshotHandle: THandle;  
  aProcessEntry32: TProcessEntry32;  
   
implementation  

procedure TForm1.BtnRefreshClick(Sender: TObject);  
var  
  i: Integer;  
  bContinue: BOOL;  
  NewItem: TListItem;  
begin  
  ListView1.Items.Clear;  
  aSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  
  aProcessEntry32.dwSize := SizeOf(aProcessEntry32);  
  bContinue := Process32First(aSnapshotHandle, aProcessEntry32);  
  while Integer(bContinue) <> 0 do  
  begin  
    NewItem := ListView1.Items.Add;  
    NewItem.Caption := ExtractFileName(aProcessEntry32.szExeFile);  
    NewItem.subItems.Add(IntToHex(aProcessEntry32.th32ProcessID, 4));  
    NewItem.subItems.Add(aProcessEntry32.szExeFile);  
    bContinue := Process32Next(aSnapshotHandle, aProcessEntry32);  
  end;  
  CloseHandle(aSnapshotHandle);  
end;  


procedure TForm1.ListView1DblClick(Sender: TObject);  
var  
  Ret: BOOL;  
  PrID: Integer; // processidentifier  
  Ph: THandle;   // processhandle  
begin  
  with ListView1 do  
  begin  
    if MessageDlg('Do you want to Terminate "' + ItemFocused.Caption + '"?' + ^J +  
                  'It''s possible the system becames instable or out of' + ^J +  
                  'control......',  
        mtConfirmation, [mbYes, mbNo], 0) = mrYes then  
     begin  
       PrID := StrToInt('$' + ItemFocused.SubItems[0]);  
       Ph := OpenProcess(1, BOOL(0), PrID);  
       Ret := TerminateProcess(Ph, 0);  
       if Integer(Ret) = 0 then  
         MessageDlg('Cannot terminate "' + ItemFocused.Caption + '"',  
                     mtInformation, [mbOK], 0)  
       else  
         ItemFocused.Delete;  
     end;  
   end;  
end;  


procedure TForm1.FormCreate(Sender: TObject);  
begin  
  {  
    ListView1.Columns.Add;  
    ListView1.Columns.Add;  
    ListView1.Columns.Add;  
    ListView1.ViewStyle := vsReport;  
  }  
  BtnRefresh.Click;  
end;  


my question: How extract associated Icons of processes then show in Listview1.

Thank you.
0
Comment
Question by:z27
[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
2 Comments
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 80 total points
ID: 9670438
you can extract icons from the exefilename, but to do this you need also the full path (you know that aProcessEntry32.szExeFile returns just the filename without the full path).
But don't worry, here's a solution:
Firts put on your for a TImageList (that will contain the extracted icons)...and link it to the TListView (small, large and state)

then add to uses the ShellApi unit (needed to use SHGetFileInfo).

Then replace the btnrefresh.onclick code with this;

procedure TForm1.BtnRefreshClick(Sender: TObject);
var  
  i: Integer;  
  bContinue: BOOL;  
  NewItem: TListItem;
  Icon: TIcon;
  FileInfo: SHFILEINFO;
  c : cardinal;
  ME : TModuleEntry32;
begin
  imagelist1.Clear;      
  ListView1.Items.Clear;
  aSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  
  aProcessEntry32.dwSize := SizeOf(aProcessEntry32);  
  bContinue := Process32First(aSnapshotHandle, aProcessEntry32);  
  while Integer(bContinue) <> 0 do  
  begin
   NewItem := ListView1.Items.Add;
        Icon := TIcon.Create;
        try
    // Get the Module for this ProcessID
       c := CreateToolhelp32Snapshot(TH32CS_SnapModule,
                                                                    aProcessEntry32.th32ProcessID);
               me.dwSize := sizeof(me);
               Module32First(c, me);
       //now extract the icon from the file    
           SHGetFileInfo(me.szExePath, 0, FileInfo, SizeOf(FileInfo), SHGFI_ICON);
           CloseHandle(c);
           icon.Handle := FileInfo.hIcon;
       //and add it to the image list
           newitem.ImageIndex := imagelist1.AddIcon(icon);
    finally
           icon.Free;
    end;
    NewItem.Caption := ExtractFileName(aProcessEntry32.szExeFile);  
    NewItem.subItems.Add(IntToHex(aProcessEntry32.th32ProcessID, 4));  
    NewItem.subItems.Add(aProcessEntry32.szExeFile);  
    bContinue := Process32Next(aSnapshotHandle, aProcessEntry32);
  end;  
  CloseHandle(aSnapshotHandle);  
end;

F68 ;-)
0
 

Author Comment

by:z27
ID: 9698104
OK ,It works well...
  Thank you very much.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month6 days, left to enroll

626 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