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

x
?
Solved

system icons in tListview

Posted on 1999-07-17
6
Medium Priority
?
302 Views
Last Modified: 2010-04-06
Hi!

I want to show files with their associated icon in a TListview component.
Any idea how to do this?

Thanks
-PhrAtoR
0
Comment
Question by:PhrAtoR
[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
  • 3
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
simonet earned 40 total points
ID: 1389323
HEre it goes:

You'll need to drop the following components on your form:
TImageList = imagelist1
TOpenDialog = opendialog1
TListView = listview1
TImage = imageicon

You'll also have to add the ShellAPI and ShlObj units to the uses clause of the unit you call this sample routine from.

procedure TForm1.btAttachClick(Sender: TObject);
var
  HI                : THandle;
  MyIconIndex       : word;
  IconItem                     : TListItem;
  AttachFileName    : string;
  X                 : word;
begin
  // This routine shows how to implement dynamic ImageLists with icons.
  // The ListView is bound to the hidden ImageList, which in turn gets its
  // icons from the hidden TImage. The hidden TImage gets an icon handle from
  // ExtractAssociatedIcon. That's all it takes !

  if ListView1.Items.Count>=MAX_FILES_ATTACHED then
  begin
     application.messagebox('Attach file', 'Maximum number of file attachments reached.', MB_ICONEXCLAMATION + MB_OK);
     exit;
  end;
  x := 0;
  if OpenDialog1.execute then
  begin
     AttachFileName := expandfilename(OpenDialog1.Filename);
     HI := ExtractAssociatedIcon(hInstance, pchar(AttachFileName), x); // Part 1 of the trick
     ImageIcon.Picture.Icon.Handle := HI;
     MyIconIndex := ImageList1.addIcon(ImageIcon.picture.icon); // MyIconIndex is Part 2 of the trick
     IconItem := ListView1.Items.Add;          
     IconItem.caption := ExtractFileName(AttachFileName);
     IconItem.SubItems.add(AttachFileName);
     IconItem.imageindex := MyIconIndex; // Here! The magic is done!
  end;
end;


Yours,

Alex
Athena's Place: http://www.bhnet.com.br/~simonet
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389324
That is a great answer!!! everything explained, sample code, just great :)
0
 
LVL 15

Expert Comment

by:simonet
ID: 1389325
Thanks, Viktor! Thank you very much!

Yours,
Alex
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:viktornet
ID: 1389326
It goes right into my database the way it is.. no additional comments needed ;))
0
 
LVL 15

Expert Comment

by:simonet
ID: 1389327
The code above was actually taken from an application you probably already have: the sample application I wrote for sending mails and faxes from a Delphi/BCB application. It's one of the How-To Projects in my site.

Just an additional last comment:

MAX_FILES_ATTACHED is the maximum number of files you want your ListView to accept. If you want no limit, you can safely remove the if..then clause (and the next 3 lines that belongs to that test) . Otherwise set it (as a const) to any value greater than 0.

Yours,

Alex
0
 

Author Comment

by:PhrAtoR
ID: 1389328
As Victornet says: great answer :-)

Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

662 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