c# 获取外部程序中控件内容

public delegate bool CallBack(int hwnd, int lParam); [DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string className, string windowName); [DllImport("user32.dll")] public stati...
摘要由CSDN通过智能技术生成

   public delegate bool CallBack(int hwnd, int lParam);
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string className, string windowName);
        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);
        [DllImport("user32.dll")]
        static extern int GetWindowText(int hWnd, StringBuilder text, int count);
        [DllImport("user32.dll", EntryPoint = "GetDlgCtrlID")]
        private static extern int GetDlgCtrlID(IntPtr hDlg);

        public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess);
        static StringBuilder reader;
        public static bool ChildWindowProcess(int hwnd, int lParam)
        {
            int id = GetDlgCtrlID(new IntPtr(hwnd));
         

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Windows API函数来获取外部ListView控件内容。具体步骤如下: 1. 获取外部程序的窗口句柄,可以使用FindWindow或FindWindowEx等函数; 2. 获取ListView控件的句柄,可以使用FindWindowEx、GetDlgItem、GetTopWindow等函数; 3. 使用ListView_GetItemCount函数获取ListView控件项的数量; 4. 使用ListView_GetItemText函数获取ListView控件每一项的文本内容。 以下是一个示例代码: ```csharp [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] 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); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr GetTopWindow(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int ListView_GetItemCount(IntPtr hwnd); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int ListView_GetItemText(IntPtr hwnd, int i, int iSubItem, StringBuilder pszText, int cchTextMax); public static string GetListViewItemText(IntPtr hwndListView, int index, int subIndex) { int pid; GetWindowThreadProcessId(hwndListView, out pid); IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, pid); int itemCount = ListView_GetItemCount(hwndListView); if (index >= itemCount) return ""; IntPtr result = IntPtr.Zero; IntPtr pszText = IntPtr.Zero; try { pszText = Marshal.AllocHGlobal(256 * 2); SendMessage(hwndListView, 0x1000 + 75, index, 0); // LVM_ENSUREVISIBLE SendMessage(hwndListView, 0x1000 + 19, index, 0x0001); // LVM_SELECTITEM SendMessage(hwndListView, 0x1000 + 15, index, 0); // LVM_GETITEMRECT SendMessage(hwndListView, 0x1000 + 58, 0, MakeLParam(0, 0x0001)); // LVM_GETSUBITEMRECT SendMessage(hwndListView, 0x1000 + 187, index * 0x10000 + subIndex, 0); // LVM_GETITEMTEXT SendMessage(hwndListView, 0x1000 + 14, 0, MakeLParam(0, 0)); // LVM_SCROLL int cch = SendMessage(hwndListView, 0x1000 + 76, index, pszText); if (cch > 0) { StringBuilder text = new StringBuilder(cch + 1); SendMessage(hwndListView, 0x1000 + 45, index, text); return text.ToString(); } } finally { if (pszText != IntPtr.Zero) Marshal.FreeHGlobal(pszText); CloseHandle(hProcess); } return ""; } public static IntPtr MakeLParam(int loWord, int hiWord) { return (IntPtr)(((ushort)loWord) | (hiWord << 16)); } // Usage IntPtr hwnd = FindWindow("Notepad", null); IntPtr hwndEdit = FindWindowEx(hwnd, IntPtr.Zero, "Edit", null); IntPtr hwndList = GetDlgItem(hwnd, 0x3E9); string text = GetListViewItemText(hwndList, 0, 1); ``` 这里的示例代码是用于获取Notepad的ListView控件的第一行第二列的文本内容。你需要根据具体情况修改代码的窗口类名、控件ID等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值