|
本帖最后由 ltais 于 2013-4-19 21:11 编辑
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace WindowsFormsApplication10
- {
- public class 类_窗口
- {
- [DllImport("user32.dll")]
- private static extern int EnumWindows(CallBack x, int y); //枚举窗口
- public delegate bool CallBack(int hwnd, int y); //委托
- [DllImport("user32")]
- private static extern int EnumChildWindows(int hwnd,CallBack x,int y); //枚举子窗口
- [DllImport("user32")]
- private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); //取类名
- [DllImport("user32")]
- private static extern int GetWindowText(int hwnd, StringBuilder lptrString, int nMaxCount);//取标题
- [DllImport("user32")]
- private static extern int GetParent(int hwnd);//取父窗口
- [DllImport("user32")]
- private static extern int IsWindowVisible(int hwnd);//窗口是否可见
- [DllImport("user32")]
- private static extern void PostMessage(int hwnd, int a, int b, int c);
- List<int> 句柄数组 = new List<int>();
- bool 过滤窗口;
- public int[] 枚举父窗口(bool 过滤)
- {
- 过滤窗口 = 过滤;
- 句柄数组.Clear();
- EnumWindows(Report,0);
- int[] i = 句柄数组.ToArray<int>();
- return i;
- }
- private bool Report(int hwnd, int lParam)
- {
- int pHwnd = GetParent(hwnd);
- if (hwnd > 0 & pHwnd== 0 )
- {
- if (过滤窗口)
- {
- if (是否可见(hwnd))
- {
- 句柄数组.Add(hwnd);
- }
- }
- else
- {
- 句柄数组.Add(hwnd);
- }
- }
- return true;
- }
- public string 取类名(int hwnd)
- {
- IntPtr a = new IntPtr(hwnd);
- StringBuilder 类名 = new StringBuilder(256);
- GetClassName(a, 类名, 256);
- return Convert .ToString ( 类名);
- }
- public string 取标题(int hwnd)
- {
- StringBuilder a = new StringBuilder(512);
- GetWindowText(hwnd, a, a.Capacity);
- return Convert .ToString ( a);
- }
- public void 销毁(int hwnd)
- {
- PostMessage(hwnd, 16, 0, 0);
- }
- public bool 是否可见(int hwnd)
- {
- if (IsWindowVisible(hwnd)==0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public int[] 枚举子窗口(int hwnd)
- {
- 句柄数组.Clear();
- EnumChildWindows(hwnd,Report1,0);
- int[] i = 句柄数组.ToArray<int>();
- return i;
- }
- private bool Report1(int hwnd, int lParam)
- {
- if (hwnd>0)
- {
- 句柄数组.Add(hwnd);
- }
- return true;
- }
- }
- }
复制代码 |
|