.NET Framework - Icon or image in first column of ListView in Details view

Asked By Rob
17-Mar-10 03:54 PM
Is there a simple way to display a selected icon or small image in the
first column of a ListView in Details view?  I cannot use
StateImageList cause I need a few different types of images.

An example would be displaying a list of programs, capturing their
icons, and displaying them in the left hand column while displaying
the program name to the right.

This is a tough one to search for, since most hits end up referring to
other ListView modes, or to StateImageList (two states only for the
entire ListView).  I need separate images for each ListViewItem, and
it needs to work in Details view, not one of the Icon views.
UnmanagedType.ByValTStr
(1)
ListViewItem
(1)
LayoutKind.Sequential
(1)
Icon.FromHandle
(1)
Marshal.SizeOf
(1)
SHGetFileInfo
(1)
UnmanagedType
(1)
LayoutKind
(1)
  Jeff Johnson replied to Rob
17-Mar-10 04:33 PM
The MSDN documentation is extremely bad regarding this issue. You simply
need to use the SmallImageList property on the list view, but the docs only
explicitly state that this property is used in SmallIcon view. That is
incorrect. It is also used for List and Details view. I guess it is implied
by this statement:

the control."

But it is virtually rescinded by a later statement:

to SmallIcon."

That should really read

to SmallIcon, List, or Details."
  Rob replied to Jeff Johnson
17-Mar-10 05:05 PM
Thanks for your reply, Jeff.  I think I am missing something though.  I
thought that the MSDN docs were referring to an image list that was
set for the entire listview control.  I need different icons for each
ListViewItem.

I did manage to decode MSDN far enough to understand that I could
toggle between two images via the 'checkbox' in each ListViewItem, but
I do not see any way to index into a longer list.  Otherwise I could
build a list of icons that would match up with each separate
ListViewItem, then just index into that list when the individual LVI's
were added.  That sounds like a convoluted solution, but it would
work... if only there was a way to index for each ListViewItem.

The more direct way would be to just set an icon for each
ListViewItem.  I have seen this done in custom controls, but that is way
more overhead that should be necessary.

Did I overlook something obvious?
  Jeff Johnson replied to Rob
17-Mar-10 05:35 PM
The image list IS for the entire control, but its purpose is to provide
images for the list view items*. Just add as many images to the image list
as you need and then reference them. Am *I* missing something?



*And maybe for column headers, but the list view items are the primary
consumers of the images.
  Rob replied to Jeff Johnson
19-Mar-10 03:17 PM
Nope, I missed that initially. Thrown by my interpretation of comments
in MSDN (I usually do not have any problem with MSDN docs, presuming
that they cover a given subject).

Anyway, I did get the code working, and got icons to display for
shortcuts, albeit with annoying little tiny arrows.  Then found a
variant on the pInvoke'd icon extractor that returns icons without the
arrows.  If anyone needs that later:

public const uint SHGFI_ICON	  = 0x100;
public const uint SHGFI_LARGEICON = 0x00;	 // large icon
public const uint SHGFI_SMALLICON = 0x01;	 // small icon
public const uint SHGFI_SYSICONINDEX = 0x000004000;
public const int  ILD_TRANSPARENT = 0x1;

[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};

private static SHFILEINFO shinfo = new SHFILEINFO();

[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string filePath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbSizeFileInfo,
uint uFlags);

[DllImport("comctl32.dll", SetLastError = true)]
public static extern IntPtr ImageList_GetIcon(IntPtr himl,
int i, int flags);


public static Icon GetIconWithoutArrow(string filePath)
{
IntPtr PtrIconList = SHGetFileInfo(
filePath, 0,
ref shinfo,
(uint)Marshal.SizeOf(shinfo),
SHGFI_SYSICONINDEX);

IntPtr PtrIcon = ImageList_GetIcon(PtrIconList,
shinfo.iIcon.ToInt32(), ILD_TRANSPARENT);
Icon icon = Icon.FromHandle(PtrIcon);
return icon;
}

(I think that is everything)


In my searches, I did notice some code on CodeProject that put icons
in column headers.  I do not need that at the moment though.

Thanks, Jeff.
Create New Account
help
static extern int RasEnumConnections([In, Out] RASCONN[] lprasconn, ref int lpcb, ref int lpcConnections); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct RASENTRYNAME { public int dwSize; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)] public string szEntryName; public int dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH + 1)] public string szPhonebook; } [StructLayout LayoutKind.Sequential, CharSet = CharSet.Auto)] struct RASCONN { public int dwSize; public IntPtr hrasconn; [MarshalAs(UnmanagedType.ByValTStr, SizeConst
to convert this? C# Discussions TMainForm.SaveJobsToArray (1) TMainForm.SaveJobToFile (1) ExtListView1.Items.Count (1) UnmanagedType.ByValTStr (1) Marshal.PtrToStructure (1) Marshal.AllocHGlobal (1) Marshal.FreeHGlobal (1) ExtListView1.Items (1) You want and combine data together. Here is the code I tried revising with no luck: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] struct TRebarStruct { private byte RDlen; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)] public string ReceiveDate; private byte JNlen; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 150)] public string JobName; private byte COlen; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)] public string
RAS_MaxFacilities = 200, RAS_MaxUserData = 200, RAS_MaxReplyMessage = 1024, RAS_MaxDnsSuffix = 256, UNLEN = 256, PWLEN = 256, DNLEN = 15 } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct RASDIALPARAMS { public int dwSize; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)RasFieldSizeConstants.RAS_MaxEntryName +1)] public string szEntryName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)RasFieldSizeConstants.RAS_MaxPhoneNumber +1)] public string szPhoneNumber; [MarshalAs UnmanagedType.ByValTStr, SizeConst = (int)RasFieldSizeConstants.RAS_MaxCallbackNumber +1)] public string szCallbackNumber; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)RasFieldSizeConstants.UNLEN
Framework " / > Hallo, ich will alle selektierten Eintr?ge in einem ListView in eine Liste ?bernehmen. ListViewItem listViewItem; listViewItem = new ListViewItem(); listViewItem.Text = "Eintrag 1"; listView1.Items.Add(listViewItem); listViewItem = new ListViewItem(); listViewItem.Text = "Eintrag 2"; listView1.Items.Add(listViewItem); List<ListViewItem> listItem = new List<ListViewItem> (); listItem