[System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct HDITEM
{
public uint mask;
public int cxy;
public IntPtr pszText; // !!!!!! If anybody should know how i can pass a string here instead of an IntPtr (which is a hack), please let me know!!!!!!!
public IntPtr hbm;
public int cchTextMax;
public int fmt;
public IntPtr lParam;
public int iImage;
public int iOrder;
public uint type;
public IntPtr pvFilter;
}
// Put this declaration in your class
[DllImport("USER32.DLL", EntryPoint= "SendMessage";)]
public static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
// Make a SetHeaderImageList function
private void SetHeaderImageList(ListView lv, ImageList il)
{
// Get the header ctrl
int LVM_GETHEADER = 0x1000 + 31;
IntPtr hwndHeader = SendMessage(lv.Handle, LVM_GETHEADER, new IntPtr(0), new IntPtr(0));
// Set the imagelist of the header ctrl
int HDM_SETIMAGELIST = 0x1200 + 8;
SendMessage(hwndHeader, HDM_SETIMAGELIST, new IntPtr(0), il.Handle);
}
// Make a SetHeaderImage function
unsafe private void SetHeaderImage(ColumnHeader ch, int imageIndex)
{
// Get the header ctrl
int LVM_GETHEADER = 0x1000 + 31;
IntPtr hwndHeader = SendMessage(ch.ListView.Handle, LVM_GETHEADER, new IntPtr(0), new IntPtr(0));
// Put the image in the header
uint HDI_IMAGE = 0x0020;
uint HDI_FORMAT = 0x0004;
int HDF_LEFT = 0;
int HDF_STRING = 0x4000;
int HDF_IMAGE = 0x0800;
int HDM_SETITEM = 0x1200 + 4;
HDITEM hdItem = new HDITEM();
hdItem.mask = HDI_IMAGE | HDI_FORMAT;
hdItem.fmt = HDF_LEFT | HDF_STRING | HDF_IMAGE;
hdItem.iImage = imageIndex;
SendMessage(hwndHeader, HDM_SETITEM, new IntPtr(0), new IntPtr(&hdItem));
}
Now use these functions to set the imagelist, and then to set the image (set
the imagelist only once)