c# 中调用EnumChildWindow
来源:互联网 发布:禹卫书法行书简体 mac 编辑:程序博客网 时间:2024/06/08 15:45
c# 中调用windows api函数
//原函数定义BOOL WINAPI EnumChildWindows( _In_opt_ HWND hWndParent, _In_ WNDENUMPROC lpEnumFunc, _In_ LPARAM lParam);
//C#定义 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] //BOOL WINAPI EnumChildWindows( _In_opt_ HWND hWndParent, _In_ WNDENUMPROC lpEnumFunc, _In_ LPARAM lParam); public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsCallBack lpfn, int lParam); //HWND WINAPI GetParent( _In_ HWND hWnd); public extern static IntPtr GetParent(IntPtr hWnd);一开始认为该函数只返回第一级child节点。处理函数如下:
//c#调用 public void ReadAllWnd() { foreach (WindowBase item in WindowsOpt.Instance.Windows) { windowsStack.Push(item); } while (windowsStack.Count > 0) { WindowBase curOpt = windowsStack.Peek(); Help.DebugWrite(this, "EnumChildWindows... {0}", Help.IntPtrToString(curOpt.HWnd)); int result = Win32Api.EnumChildWindows(curOpt.HWnd, (wPara, lPara) => { Help.DebugWrite(this, "Win32Api.EnumChildWindows back hWnd {0}", wPara.ToString()); WindowBase childWindow = WindowBase.CreateWindow(wPara); childWindow.ParentWindow = curOpt; curOpt.ChildNodes.Add(childWindow); return true; }, 0); windowsStack.Pop(); foreach (WindowBase item in curOpt.ChildNodes) { windowsStack.Push(item); } } }
但在开发中发现,该函数会返回所有子级的所有子节点。因些使用以下代码构建windows树,每次callback需要使用GetParent()得到parent句柄,根据parent句柄找到对应window。
public void ReadAllChild() { Dictionary<IntPtr, WindowBase> dictChildWindows = new Dictionary<IntPtr, WindowBase>(); foreach (WindowBase RootWindow in WindowsOpt.Instance.Windows) { Win32Api.EnumChildWindows(RootWindow.HWnd, (wPara, pPara) => { WindowBase childWindow = WindowBase.CreateWindow(wPara); dictChildWindows.Add(wPara, childWindow); return true; }, 0); foreach (KeyValuePair<IntPtr, WindowBase> item in dictChildWindows) { IntPtr parentHWnd = this.RunApi(() => Win32Api.GetParent(item.Value.HWnd)); if (dictChildWindows.ContainsKey(parentHWnd)) { item.Value.ParentWindow = dictChildWindows[parentHWnd]; } else { item.Value.ParentWindow = Windows.First((w) => { return w.HWnd == parentHWnd; }); } item.Value.ParentWindow.ChildNodes.Add(item.Value); } dictChildWindows.Clear(); } }
阅读全文
0 0
- c# 中调用EnumChildWindow
- mfc enumchildwindow使用
- C#中调用API
- C#中调用API
- C#中调用API
- C#中调用Javascript
- C#中调用API
- c#中调用DTS
- C#中调用DLL
- C#中调用API
- C# 中调用DLL
- C#中调用API
- C#中调用API
- C#中调用API
- C#中调用dll
- C#中调用API
- C#中调用DLL
- c# 中调用windowsAPI
- java基础8:io流对象之缓冲流
- Eclipse 配置远程开发PHP
- 数据库时间转换
- 蒟蒻爆炸记:noip2017提高组总结
- Android关于事件冲突的简单解决方案
- c# 中调用EnumChildWindow
- 二级联动
- 【css】常用知识点小结
- 大数据初学者应该知道的知识
- spring Junit单元测试事务管理报错:java.lang.IllegalStateException: Failed to load ApplicationContext解决办法
- 关于ScrollView嵌套RecyclerView时RecyclerView不显示的问题
- CentOS-7.0./linux中安装与配置Tomcat-7的方法
- hdu4046(树状数组)
- 存款问题