private const Int32 MAX_WINDOW_TEXT = 1024;
private const Int32 MAX_WINDOW_CLASS = 1024;
private const UInt32 GW_HWNDNEXT = (UInt32)2;
// private const UInt32 GW_HWNDFIRST = 0;
private const UInt32 GW_CHILD = 5;
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();//获得桌面上的句柄
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount);//根据句柄获取caption
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 GetClassName(IntPtr hWnd, StringBuilder lpClassName, Int32 nMaxCount);//根据句柄获取Class
[DllImport("user32.dll")]
private static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);//检索父窗体的句柄
public static String GetWindowCaption(IntPtr handle)
{
StringBuilder builder = new StringBuilder(MAX_WINDOW_TEXT);
if (GetWindowText(handle, builder, MAX_WINDOW_TEXT) > 0)
return builder.ToString();
return String.Empty;
}
public static String GetWindowClass(IntPtr handle)
{
StringBuilder builder = new StringBuilder(MAX_WINDOW_CLASS);
if (GetClassName(handle, builder, MAX_WINDOW_CLASS) > 0)
return builder.ToString();
return String.Empty;
}
设计思路
1,需要调用的api ,在正个桌面窗体中查找到所有窗体的句柄。
2,根据窗体句柄获取窗体的Catpion,
3,根据窗体句柄获取窗体的Class,
4,比对caption和class是否相等,
5,把相等的结果加入到数组中。
6,还可以进行模糊查询,用到string.Contains()
代码文件地址:http://download.csdn.net/detail/elrphant/4043321