前言
之前在做一个小玩意,用于控制外部程序的某些控件,所以首先要通过FindWindowEx
和FindWindow
两个函数来检索外部程序的窗口句柄,再进行其他的操作。但是由于我是第一次用这两函数,就出现了点问题,不知道如何检索同类名下无名或重名的控件句柄。好在经过一番搜索得知可通过索引来解决这个问题,故记录之。
实现
static IntPtr FindWindowByIndex(IntPtr hwndParent, int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hwndParent, result, null, null);
if (result != IntPtr.Zero)
{
++ct;
}
} while (ct < index && result != IntPtr.Zero);
return result;
}
}