📄 listviewitemicon.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace LayerControl
{
class ListViewItemIcon
{
ListView listView;
public ListViewItemIcon(ListView listView)
{
this.listView = listView;
SendMessage(listView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, (IntPtr)LVS_EX_SUBITEMIMAGES, (IntPtr)LVS_EX_SUBITEMIMAGES);
}
public void SetItemImage(int rowIndex,int columnIndex, int imageIndex)
{
//Make sure that we have handle of the header
if (listView.Handle == IntPtr.Zero)
throw new Exception("Handle of header does not exist.");
//Create HDITEM and populate with values
LV_ITEM lvi = new LV_ITEM();
// Row of the item
lvi.iItem = rowIndex;
// Column of the item
lvi.iSubItem = columnIndex;
lvi.mask = LVIF_IMAGE;
lvi.iImage = imageIndex;
lvi.state = 0;
lvi.stateMask = 0;
lvi.lParam = (IntPtr)0;
//lvi.pszText = Marshal.StringToBSTR("5");
//Sending HDM_SETITEM message
SendMessage(listView.Handle, LVM_SETITEMW, (IntPtr)0, ref lvi);
}
struct LV_ITEM
{
public uint mask;
public int iItem;
public int iSubItem;
public uint state;
public uint stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public IntPtr lParam;
public int iIndent;
}
[DllImport("coredll.dll")]
static extern int SendMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);
[DllImport("coredll.dll")]
static extern bool SendMessage(
IntPtr hWnd, // handle to destination window
uint msg, // message
IntPtr wParam,
ref LV_ITEM lParam);// pointer to struct of LV_ITEM
public const Int32 LVM_FIRST = 0x1000;
public const Int32 LVM_GETITEM = LVM_FIRST + 5;
public const Int32 LVM_SETITEM = LVM_FIRST + 6;
public const Int32 LVIF_TEXT = 0x0001;
public const Int32 LVIF_IMAGE = 0x0002;
//public const Int32 LVM_SETEXTENDEDLISTVIEWSTYLE =(LVM_FIRST + 54);
public const int LVW_FIRST = 0x1000;
//public const int LVM_GETEXTENDEDLISTVIEWSTYLE = LVW_FIRST + 54;
public const int LVM_GETHEADER = (LVM_FIRST + 31);
public const int LVM_SETICONSPACING = (LVM_FIRST + 53);
public const int LVM_GETSUBITEMRECT = (LVM_FIRST + 56);
public const int LVM_GETITEMSTATE = (LVM_FIRST + 44);
public const int LVM_GETITEMTEXTW = (LVM_FIRST + 115);
public const int LVM_INSERTITEMA = (LVM_FIRST + 7);
public const int LVM_INSERTITEMW = (LVM_FIRST + 77);
public const int LVM_INSERTCOLUMNA = (LVM_FIRST + 27);
public const int LVM_INSERTCOLUMNW = (LVM_FIRST + 97);
public const int LVM_DELETECOLUMN = (LVM_FIRST + 28);
public const int LVM_GETCOLUMNA = (LVM_FIRST + 25);
public const int LVM_GETCOLUMNW = (LVM_FIRST + 95);
public const int LVM_SETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 54);
public const int LVM_SETITEMA = (LVM_FIRST + 6);
public const int LVM_SETITEMW = (LVM_FIRST + 76);
public const int LVM_EDITLABELA = (LVM_FIRST + 23);
public const int LVM_EDITLABELW = (LVM_FIRST + 118);
public const int LVM_DELETEITEM = (LVM_FIRST + 8);
public const int LVM_SETBKCOLOR = (LVM_FIRST + 1);
public const int LVM_GETBKCOLOR = (LVM_FIRST + 0);
public const int LVM_GETTEXTBKCOLOR = (LVM_FIRST + 37);
public const int LVM_SETTEXTBKCOLOR = (LVM_FIRST + 38);
public const int LVM_DELETEALLITEMS = (LVM_FIRST + 9);
public const int LVM_GETNEXTITEM = (LVM_FIRST + 12);
public const int LVM_SETITEMCOUNT = (LVM_FIRST + 47);
public const int LVM_GETITEMCOUNT = (LVM_FIRST + 4);
public const int LVM_SETCOLUMNWIDTH = (LVM_FIRST + 30);
public const int LVM_GETITEMRECT = (LVM_FIRST + 14);
public const int LVM_EDITLABEL = (LVM_FIRST + 23);
public const int LVS_EX_GRIDLINES = 0x00000001;
public const int LVS_EX_SUBITEMIMAGES = 0x00000002;
public const int LVS_EX_CHECKBOXES = 0x00000004;
public const int LVS_EX_TRACKSELECT = 0x00000008;
public const int LVS_EX_HEADERDRAGDROP = 0x00000010;
public const int LVS_EX_FULLROWSELECT = 0x00000020; // applies to report mode only
public const int LVS_EX_ONECLICKACTIVATE = 0x00000040;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -