首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

帮小弟我改下代码

2013-08-01 
帮我改下代码。我期望以树装形出当前所有窗口及窗口的所有控件(就像spy++一样)[DllImport(user32.dll, Ch

帮我改下代码。
我期望以树装形出当前所有窗口及窗口的所有控件(就像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);
        }



 
[解决办法]

    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);
            }
        }


[解决办法]
我靠, 一点备注都没有 怎么看啊 
能看累死
[解决办法]
引用:
我靠, 一点备注都没有 怎么看啊 
能看累死
+1

热点排行