資源簡介
通過窗口名獲取程序所有控件句柄,并繪制句柄結(jié)構(gòu)樹。
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.Runtime.InteropServices;
namespace?spyTree
{
????public?partial?class?Form1?:?Form
????{
????????[DllImport(“User32.dll“?EntryPoint?=?“FindWindow“)]
????????private?static?extern?int?FindWindow(string?lpClassName?string?lpWindowName);
????????[DllImport(“user32.dll“)]//在窗口列表中尋找與指定條件相符的第一個子窗口
????????private?static?extern?int?FindWindowEx(
??????????????????????????????????????????????int?hwndParent?//?handle?to?parent?window
????????????????????????????????????????????int?hwndChildAfter?//?handle?to?child?window
??????????????????????????????????????????????string?className?//窗口類名????????????
??????????????????????????????????????????????string?windowName?//?窗口標(biāo)題
????????);
????????[DllImport(“User32.dll“?CharSet?=?CharSet.Auto)]
????????public?static?extern?int?GetClassName(IntPtr?hWnd?StringBuilder?ClassName?int?nMaxCount);
????????[DllImport(“user32.dll“)]
????????public?static?extern?int?GetWindowText(int?hwnd?StringBuilder?lpString?int?nMaxCount);//獲取窗體標(biāo)題名稱
????????[DllImport(“user32.dll“?EntryPoint?=?“SendMessageA“)]
????????public?static?extern?int?SendMessage(int?hwnd?int?wMsg?int?wParam?string?lParam);
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????int?WM_GETTEXT?=?0x0D;
????????private?void?getChildHwd(int?parent_hwdTreeNode?treeNode)?{
????????????int?past_hwd?=?0;
????????????int?i?=?1;
????????????List?list?=?new?List();
????????????while(past_hwd!=0||i==1)
????????????{
????????????????i++;
????????????????past_hwd?=?FindWindowEx(parent_hwd?past_hwd?null?null);
????????????????if?(past_hwd?>?0)
????????????????{
????????????????????StringBuilder?sb?=?new?StringBuilder();
????????????????????int?maxcount=0;
????????????????????IntPtr?intsb?=?new?IntPtr(past_hwd);
????????????????????GetClassName(intsbsb?255);
????????????????????getChildHwd(past_hwd?treeNode.Nodes.Add(past_hwd?+?“|“?+?sb.ToString()));
????????????????}
????????????}
????????????//return?list;
????????}
????????private?void?Button1_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????int?intPtr?=?FindWindow(null?window_title_text.Text.Trim());
????????????????if?(intPtr?==?0)
????????????????{
????????????????????MessageBox.Show(“此標(biāo)題窗口不存在!!!“?“錯誤“);
????????????????}
????????????????else
????????????????{
????????????????????treeView1.Nodes.Clear();
????????????????????getChildHwd(intPtr?treeView1.Nodes.Add(intPtr?+?“|“?+?window_title_text.Text.Trim()));
????????????????????//;
????????????????}
????????????}
????????????catch?(Exception?ex)?{
????????????????throw?ex;
????????????}
????????}
????????private?int?select_node_hwnd?=?0;
????????private?void?TreeView1_NodeMouseDouble
評論
共有 條評論