c#SendMessage取不到外部自绘listview内容
时间: 2024-03-28 22:39:00 浏览: 24
您好,如果您使用C#的SendMessage函数无法取到外部自绘的ListView控件内容,可能是因为自绘ListView的绘制并没有将数据保存到ListView中。
您可以尝试使用其他方法来获取ListView的内容,比如使用ListView控件自带的遍历方法,或者使用Windows API函数来获取ListView的内容。另外,您可以检查一下自绘ListView的代码,看看是否有保存数据到ListView中的逻辑。
相关问题
c#取外部自绘listview内容SendMessage怎么用
在C#中,使用SendMessage函数获取外部自绘ListView控件的内容,需要先获取ListView控件的句柄,然后通过SendMessage函数发送LVM_GETITEMCOUNT、LVM_GETITEM等消息来获取ListView控件中的数据。
以下是一个示例代码,演示了如何使用SendMessage函数获取ListView控件中的数据:
```csharp
// 获取ListView控件句柄
IntPtr listViewHandle = FindWindow(null, "ListView窗口标题");
// 获取ListView控件中item的数量
int itemCount = (int)SendMessage(listViewHandle, LVM_GETITEMCOUNT, 0, 0);
// 遍历ListView控件中的item,获取每个item的数据
for (int i = 0; i < itemCount; i++)
{
LVITEM lvItem = new LVITEM();
lvItem.mask = LVIF_TEXT;
lvItem.iItem = i;
lvItem.iSubItem = 0;
lvItem.cchTextMax = 256;
lvItem.pszText = Marshal.AllocHGlobal(256);
SendMessage(listViewHandle, LVM_GETITEM, 0, ref lvItem);
string itemText = Marshal.PtrToStringAuto(lvItem.pszText);
// 处理获取到的item数据
// ...
}
```
需要注意的是,以上代码中的LVITEM结构体需要自己定义,可参考ListView控件的文档来进行定义。另外,SendMessage函数的第一个参数为目标窗口的句柄,第二个参数为消息类型,后面的参数为消息所需的参数。
c#如何取外部自绘listview内容
如果你想在 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 控件可能会有兼容性问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)