c#如何取外部自绘listview内容
时间: 2023-10-10 07:13:03 浏览: 163
如果你想在 C# 中获取外部自绘的 ListView 的内容,可以使用以下步骤:
1. 使用 P/Invoke 调用 FindWindowEx 函数查找外部窗口中的 ListView 控件的句柄。
2. 使用 SendMessage 函数发送 LVM_GETITEMCOUNT 消息获取 ListView 中项的数量。
3. 使用 SendMessage 函数发送 LVM_GETITEMTEXT 消息获取每个项的文本内容。
以下是示例代码:
```
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, StringBuilder lParam);
const int LVM_GETITEMCOUNT = 0x1000 + 4;
const int LVM_GETITEMTEXT = 0x1000 + 45;
IntPtr hwndListView = FindWindowEx(hwndParent, IntPtr.Zero, "SysListView32", null);
int itemCount = SendMessage(hwndListView, LVM_GETITEMCOUNT, 0, 0);
for (int i = 0; i < itemCount; i++)
{
StringBuilder sb = new StringBuilder(256);
SendMessage(hwndListView, LVM_GETITEMTEXT, i, sb);
string itemText = sb.ToString();
// 处理获取到的项文本内容
}
```
其中,hwndParent 是外部窗口的句柄。注意,这种方法只适用于自绘的 ListView 控件,对于系统默认的 ListView 控件可能会有兼容性问题。
阅读全文
相关推荐


















