问题1:项目需要查找PPT句柄,通过PPT标题精准匹配有时会查找不到
原因:PPT的标题会跟据power point 版本有差异,同时ppt本身打开后标题有时会增加一些内容,比如PPT制作完成后,保存为97——2003版,当我们使用高版本的power point打开标题会出现[兼容模式]字样,这样会导致精确匹配比较难做
能不能做到模糊匹配从而获取PPT句柄呢,答案是可以的
解决方案:使用API :EnumWindows枚举所有窗体,获取标题,再去匹配需要查找的字符串,从而取到句柄
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hwnd, int wMsg, int wParam, Byte[] lParam);
///
/// 模糊匹配控件标题
///
/// 模糊匹配字符串
///
public static IntPtr FindWindowExByDimStrIntoWindow(string dimStr)
{
IntPtr iResult = IntPtr.Zero;
string controlTitle = ""; //控件完全标题
// 枚举子窗体,查找控件句柄
int i = EnumWindows(
(h, l) =>
{
int cTxtLen;
if (IsWindowVisible(h))
{
//对每一个枚举窗口的处理
cTxtLen = SendMessage(h, WM_GETTEXTLENGTH, 0, 0); //获取内容长度
Byte[] byt = new Byte[cTxtLen];
SendMessage((int)h, WM_GETTEXT, cTxtLen + 1, byt); //获取内容
string str = Encoding.Default.GetString(byt);
if (str.ToString().Contains(dimStr))
{
iResult = h;
controlTitle = str.ToString();
return false;
}
else
return true;
}
else
return true;
},
0);
// 返回查找结果
return iResult;
}
问题二:在指定窗体中,查找子窗口的文本内容
///
/// 遍历子窗体,寻找文本框内容包含指定字符串的完全内容
///
/// 父窗体句柄
/// 模糊匹配字符串
///
public static string FindWindowExByDimStrIntoChildWindow2(IntPtr hwnd, string dimStr)
{
Log.WriteLog("进入函数:FindWindowExByDimStrIntoChildWindow \n");
IntPtr iResult = IntPtr.Zero;
string controlTitle = ""; //控件完全标题
// 枚举子窗体,查找控件句柄
int i = EnumChildWindows(
hwnd,
(h, l) =>
{
int TextLen;
TextLen = SendMessage(h, WM_GETTEXTLENGTH, 0, 0); //获取内容长度
Byte[] byt = new Byte[TextLen];
SendMessage((int)h, WM_GETTEXT, TextLen + 1, byt); //获取内容
string str = Encoding.Default.GetString(byt);
// Log.WriteLog(string.Format("Edit控件内容{0}\n", str));
if (str.ToString().Contains(dimStr))
{
iResult = h;
controlTitle = str.ToString();
Log.WriteLog(string.Format("模糊匹配找到的路径{0}\n", controlTitle));
return false;
}
else
return true;
},
0);
// 返回查找结果
return controlTitle;
}
如果知道控件类型为Edit,可以使用以下函数
///
/// 模糊匹配控件标题
///
/// 父窗体句柄
/// 模糊匹配字符串
///
public static string FindWindowExByDimStrIntoChildWindow(IntPtr hwnd, string dimStr)
{
Log.WriteLog("进入函数:FindWindowExByDimStrIntoChildWindow \n");
IntPtr iResult = IntPtr.Zero;
string controlTitle = ""; //控件完全标题
// 枚举子窗体,查找控件句柄
int i = EnumChildWindows(
hwnd,
(h, l) =>
{
IntPtr HwndStatue = FindWindowEx(h, 0, "Edit", null); //文本框句柄
StringBuilder title1 = new StringBuilder(200);
int len1;
len1 = GetWindowText(h, title1, 200);
if (HwndStatue != IntPtr.Zero)
{
int TextLen;
TextLen = SendMessage(HwndStatue, WM_GETTEXTLENGTH, 0, 0); //获取内容长度
Byte[] byt = new Byte[TextLen];
SendMessage((int)HwndStatue, WM_GETTEXT, TextLen + 1, byt); //获取内容
string str = Encoding.Default.GetString(byt);
// Log.WriteLog(string.Format("Edit控件内容{0}\n", str));
if (str.ToString().Contains(dimStr))
{
iResult = h;
controlTitle = str.ToString();
Log.WriteLog(string.Format("模糊匹配找到的路径{0}\n", controlTitle));
return false;
}
else
return true;
}
else
return true;
},
0);
// 返回查找结果
return controlTitle;
}