C# Winform 已知窗体句柄,如何遍历出所有控件句柄

c# windform 已知窗体句柄,如何遍历出所有控件句柄

        public delegate bool CallBack(int hwnd, int lParam);
        public delegate bool EnumWindowsProc(int hWnd, int lParam); 
        List<string> list = new List<string>();

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
 
        [DllImport("user32.dll")]
        public static extern int GetWindowText(int hWnd, IntPtr lpString, int nMaxCount);
 
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            list.Clear();
            EnumChildWindows(this.Handle, this.EnumWindowsMethod, IntPtr.Zero);
            //这里得到了所有的子窗口list.Count;
                        //this.Handle改成你已得到的窗体句柄
        }
 
        private bool EnumWindowsMethod(int hWnd, int lParam)
        {
            IntPtr lpString = Marshal.AllocHGlobal(200);
            GetWindowText(hWnd, lpString, 200);//这里获得控件text
            var text = Marshal.PtrToStringAnsi(lpString);
            if (!string.IsNullOrWhiteSpace(text))
                list.Add(text);//添加到list,如果要获得句柄就新建list添加hWnd
            return true;
        }
 

//public delegate bool CallBack(int hwnd, int lParam);
//public delegate bool EnumWindowsProc(int hWnd, int lParam);
//[DllImport("User32.dll", EntryPoint = "FindWindow", SetLastError = false)]
//public static extern int FindWindow(string lpClassName, string lpWindowName);

//[DllImport("user32.dll")]
//[return: MarshalAs(UnmanagedType.Bool)]
//public static extern bool Enumchildwindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
//[DllImport("user32.dll")]
//public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
//[DllImport("user32.dll")]
//public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);
//[DllImport("user32.dll")]
//public static extern int EnumWindows(CallBack lpfn, int lParam);
//[DllImport("user32.dll")]
//public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);

参考链接:百度安全验证

                  c#已知窗体句柄,如何遍历出所有控件句柄_微软技术-CSDN问答

相关推荐

  1. 泄露(handle leakage)

    2024-06-05 20:38:09       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-05 20:38:09       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-05 20:38:09       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-05 20:38:09       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-05 20:38:09       18 阅读

热门阅读

  1. 广东省青少年编程预选赛:挑战与机遇并存

    2024-06-05 20:38:09       9 阅读
  2. 多角度剖析事务和事件的区别

    2024-06-05 20:38:09       7 阅读
  3. 添加pip源,ubuntu

    2024-06-05 20:38:09       8 阅读
  4. leetcode 802.找到最终的安全状态

    2024-06-05 20:38:09       6 阅读
  5. 摸鱼大数据——Hive调优10-12

    2024-06-05 20:38:09       9 阅读
  6. mysql-集群-二进制部署

    2024-06-05 20:38:09       4 阅读
  7. 45、Flink 的 Process Function 详解

    2024-06-05 20:38:09       8 阅读
  8. SpringBoot历史版本信息

    2024-06-05 20:38:09       8 阅读
  9. 【实用技巧】Unity的Text组件实用技巧

    2024-06-05 20:38:09       8 阅读
  10. GPT-4o:人工智能新纪元的启航者

    2024-06-05 20:38:09       8 阅读
  11. 如何评价GPT-4o?(要点精简)

    2024-06-05 20:38:09       7 阅读