c#取外部自绘listview内容
时间: 2023-10-28 19:33:00 浏览: 78
如果您想获取外部自绘的ListView控件的内容,可以使用以下代码:
```csharp
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int LVM_GETITEMCOUNT = 0x1000 + 4;
private const int LVM_GETITEMTEXT = 0x1000 + 45;
public List<string> GetListViewItems(IntPtr listViewHandle)
{
List<string> items = new List<string>();
int count = (int)SendMessage(listViewHandle, LVM_GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero);
for (int i = 0; i < count; i++)
{
StringBuilder sb = new StringBuilder(256);
IntPtr result = SendMessage(listViewHandle, LVM_GETITEMTEXT, (IntPtr)i, sb);
if (result.ToInt32() > 0)
{
items.Add(sb.ToString());
}
}
return items;
}
```
其中,`listViewHandle`是外部ListView控件的句柄。调用`GetListViewItems`方法后,将返回一个包含ListView中所有项的字符串列表。
阅读全文