现在的位置: 首页 > 综合 > 正文

C# 枚举窗口

2017年10月09日 ⁄ 综合 ⁄ 共 916字 ⁄ 字号 评论关闭
用C#枚举出所有窗口句柄,再保留有用的。。。



public delegate bool CallBack(int hwnd, int y);

//该函数枚举所有屏幕上的顶层窗口,并将窗口句柄传送给应用程序定义的回调函数。
//回调函数返回FALSE将停止枚举,否则EnumWindows函数继续到所有顶层窗口枚举完为止。
[DllImport(\"user32.dll\")]
public static extern int EnumWindows(CallBack x, int y);

//该函数将指定窗口的标题条文本(如果存在)拷贝到一个缓存区内
[DllImport(\"user32.dll\")]
public static extern int GetWindowText(int hwnd, StringBuilder lptrString, int nMaxCount);

//该函数获得一个指定子窗口的父窗口句柄
[DllImport(\"user32.dll\")]
public static extern int GetParent(int hwnd);

//该函数获得给定窗口的可视状态。
[DllImport(\"user32.dll\")]
public static extern bool IsWindowVisible(int hwnd);



//枚举
public bool Report(int hwnd, int lParam)
{
int pHwnd = GetParent(hwnd);
//如果没有再没有父窗口并且为可是状态的窗口
if (pHwnd == 0 && IsWindowVisible(hwnd) == true)
{
StringBuilder sb = new StringBuilder(256);
GetWindowText(hwnd, sb,sb.Capacity);
if (sb.Length > 0)
{
//窗口名添加到列表框
comboBox1.Items.Add(sb.ToString());
}
}
return true;
}


private void Form1_Load(object sender, EventArgs e)
{

comboBox1.Items.Clear();

EnumWindows(Report, 0);
}


抱歉!评论已关闭.