Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Listview and their associated Icons .

Posted on 2003-11-03
2
Medium Priority
?
337 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 320 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

721 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