我期望以树装形出当前所有窗口及窗口的所有控件(就像spy++一样)
         [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowTextLength(HandleRef hWnd);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
        [DllImport("user32.dll", ExactSpelling = true)]
        private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);
        private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
        private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam, TreeNode parentNode);        private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
        {
            int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
            StringBuilder builder1 = new StringBuilder(num1);
            GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);
            Application.DoEvents();
            listBox1.Items.Add(string.Format("父:{0} Title: {1}", handle, builder1.ToString()));
            TreeNode RootNode = new TreeNode(builder1.ToString());
            this.treeView1.Nodes.Add(RootNode );            EnumChildWindows(new HandleRef(this, handle), new EnumChildrenCallback(EnumChildWindowsCallback), new HandleRef(null, IntPtr.Zero));
            return true;
        }
        private bool EnumChildWindowsCallback(IntPtr handle, IntPtr lparam, TreeNode parentNode)
        {
            int num1 = GetWindowTextLength(new HandleRef(this, handle)) * 2;
            StringBuilder builder1 = new StringBuilder(num1);
            GetWindowText(new HandleRef(this, handle), builder1, builder1.Capacity);      
            listBox1.Items.Add(string.Format("SubWnd:{0} Title: {1}", handle, builder1.ToString()));
           TreeNode subNode = new TreeNode(builder1.ToString());
           parentNode.Nodes.Add(subNode);
            return true;
        }
        private void button1_Click(object sender, EventArgs e)
        {   EnumThreadWindowsCallback callback1 = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
            EnumWindows(callback1, IntPtr.Zero);
        } 

解决方案 »

  1.   


        private void MainForm_Load(object sender, EventArgs e)
            {
                TreeNode node = new TreeNode(this.Text);
                treeView1.Nodes.Add(node);
                AddCtls(node, this);
            }        public void AddCtls(TreeNode node, Control pctl)
            {
              
                foreach (Control ctl in pctl.Controls)
                {
                    TreeNode curNode = new TreeNode(ctl.Text + " - " + ctl.GetType().ToString());
                    node.Nodes.Add(curNode);
                    AddCtls(curNode, ctl);
                }
            }