Solved

Listview and their associated Icons .

Posted on 2003-11-03
2
326 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
2 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 80 total points
Comment Utility
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
Comment Utility
OK ,It works well...
  Thank you very much.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

10 Experts available now in Live!

Get 1:1 Help Now