C#获取外部程序ListView中的数据
2012-02-17 20:59
573 查看
转自 http://www.zu14.cn/2010/03/18/dotnet-csharp-get-external-listview-data-by-windows-api/trackback/
此文章,主要实现了C#利用Windows Api 获取外部程序中的ListView控件中的数据,也算是C# 使用API的一个高级一些的例子吧。
下面列出API相关的声明部分:
/// <summary>
/// 作者: 三角猫
/// 网址: http://www.zu14.cn/
/// 声明: 转载请务必保留原作者信息
/// </summary>
const uint LVM_FIRST = 0x1000;
const uint HDM_FIRST = 0x1200;
const uint LVM_GETITEMCOUNT = LVM_FIRST + 4;
const uint LVM_GETITEMW = LVM_FIRST + 75;
const uint LVM_GETHEADER = LVM_FIRST + 31;
const uint HDM_GETITEMCOUNT = HDM_FIRST + 0;
[DllImport("user32.DLL")]
static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.DLL")]
static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
[DllImport("user32.DLL")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint dwProcessId);
const uint PROCESS_VM_OPERATION = 0x0008;
const uint PROCESS_VM_READ = 0x0010;
const uint PROCESS_VM_WRITE = 0x0020;
[DllImport("kernel32.dll")]
static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
const uint MEM_COMMIT = 0x1000;
const uint MEM_RELEASE = 0x8000;
const uint MEM_RESERVE = 0x2000;
const uint PAGE_READWRITE = 4;
[DllImport("kernel32.dll")]
static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll")]
static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint dwFreeType);
[DllImport("kernel32.dll")]
static extern bool CloseHandle(IntPtr handle);
[DllImport("kernel32.dll")]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
public struct LVITEM
{
public int mask;
public int iItem;
public int iSubItem;
public int state;
public int stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public IntPtr lParam;
public int iIndent;
public int iGroupId;
public int cColumns;
public IntPtr puColumns;
}
int LVIF_TEXT = 0x0001; 几个操作LISTVIEW的自定义函数/// <summary>
/// 获取 ListView 的行数
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
int ListView_GetItemCount(IntPtr hwnd)
{
return SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
}
/// <summary>
/// 获取 ListView 的标题栏句柄
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
private IntPtr ListView_GetHeader(IntPtr hwnd)
{
return (IntPtr)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
}
/// <summary>
/// 获取 ListView 的标题栏的列数
/// </summary>
/// <param name="header"></param>
/// <returns></returns>
private int Header_GetItemCount(IntPtr header)
{
return SendMessage(header, HDM_GETITEMCOUNT, 0, 0);
}
/// <summary>
/// 获取 ListView 的列数
/// </summary>
/// <param name="listViewHandle"></param>
/// <returns></returns>
int ListViewColumnCount(IntPtr listViewHandle)
{
return Header_GetItemCount(ListView_GetHeader(listViewHandle));
}
代码
此文章,主要实现了C#利用Windows Api 获取外部程序中的ListView控件中的数据,也算是C# 使用API的一个高级一些的例子吧。
下面列出API相关的声明部分:
/// <summary>
/// 作者: 三角猫
/// 网址: http://www.zu14.cn/
/// 声明: 转载请务必保留原作者信息
/// </summary>
const uint LVM_FIRST = 0x1000;
const uint HDM_FIRST = 0x1200;
const uint LVM_GETITEMCOUNT = LVM_FIRST + 4;
const uint LVM_GETITEMW = LVM_FIRST + 75;
const uint LVM_GETHEADER = LVM_FIRST + 31;
const uint HDM_GETITEMCOUNT = HDM_FIRST + 0;
[DllImport("user32.DLL")]
static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.DLL")]
static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
[DllImport("user32.DLL")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint dwProcessId);
const uint PROCESS_VM_OPERATION = 0x0008;
const uint PROCESS_VM_READ = 0x0010;
const uint PROCESS_VM_WRITE = 0x0020;
[DllImport("kernel32.dll")]
static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
const uint MEM_COMMIT = 0x1000;
const uint MEM_RELEASE = 0x8000;
const uint MEM_RESERVE = 0x2000;
const uint PAGE_READWRITE = 4;
[DllImport("kernel32.dll")]
static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll")]
static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint dwFreeType);
[DllImport("kernel32.dll")]
static extern bool CloseHandle(IntPtr handle);
[DllImport("kernel32.dll")]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
public struct LVITEM
{
public int mask;
public int iItem;
public int iSubItem;
public int state;
public int stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public IntPtr lParam;
public int iIndent;
public int iGroupId;
public int cColumns;
public IntPtr puColumns;
}
int LVIF_TEXT = 0x0001; 几个操作LISTVIEW的自定义函数/// <summary>
/// 获取 ListView 的行数
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
int ListView_GetItemCount(IntPtr hwnd)
{
return SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
}
/// <summary>
/// 获取 ListView 的标题栏句柄
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
private IntPtr ListView_GetHeader(IntPtr hwnd)
{
return (IntPtr)SendMessage(hwnd, LVM_GETHEADER, 0, 0);
}
/// <summary>
/// 获取 ListView 的标题栏的列数
/// </summary>
/// <param name="header"></param>
/// <returns></returns>
private int Header_GetItemCount(IntPtr header)
{
return SendMessage(header, HDM_GETITEMCOUNT, 0, 0);
}
/// <summary>
/// 获取 ListView 的列数
/// </summary>
/// <param name="listViewHandle"></param>
/// <returns></returns>
int ListViewColumnCount(IntPtr listViewHandle)
{
return Header_GetItemCount(ListView_GetHeader(listViewHandle));
}
代码
相关文章推荐
- C#通过WIN32 API 获取外部程序sysListview的值和TreeView的值
- C#通过WIN32 API 获取外部程序sysListview的值和TreeView的值
- C#通过WIN32 API 获取外部程序sysListview的值和TreeView的值
- C#怎么取外部程序的listview,treeview的内容?
- C# Socket实现通过公共服务程序获取数据的例子
- c#获取listview当前选中行的数据
- dsp控制DM9000实现802.3数据收发第三篇,调试过程程序第三版;采用外部引脚中断方式获取中断,优化掉帧现象
- abap submit用法(一)调用外部程序(abap内存获取数据) 收藏
- C#中listview允许选中整行,并获取选中行数据
- C#之获取ListView当前选中的行的数据!...
- C#之获取ListView当前选中的行的数据!...
- 在C#获取摄像头数据的程序
- C#调用API向外部程序发送数据
- delphi获取其他外部程序中TDBGridEh控件中的数据
- C#:划词-外部程序获取IE页面选择文本
- 在C#获取摄像头数据的程序
- C# 调用外部程序,并获取输出和错误信息
- abap submit用法(一)调用外部程序(abap内存获取数据)
- C# C/S程序请求网页地址,并获取返回数据代码