LISTVIEW往HEADER里加图片的东东

博客给出了在类中声明SendMessage函数的代码,还创建了SetHeaderImageList和SetHeaderImage两个函数。前者用于获取标题控件并设置图像列表,后者用于将图像放入标题栏。最后提示使用这些函数设置图像列表和图像。
  // Declare this structure first
 [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)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值