110,532
社区成员
发帖
与我相关
我的任务
分享
private void btnControl_Click(object sender, EventArgs e)
{
IntPtr mainWindows = FindMainWindowHandle("Form1",100,25);
IntPtr needbtn = FindWindowEx(mainWindows, IntPtr.Zero,null, "button1"); //获取名字为button1的按钮
if(needbtn==IntPtr.Zero)
throw new Exception("没有找到名字为1的按钮");
else
{
PostMessage(needbtn,0x0201,0,0);
PostMessage(needbtn,0x0202,0,0);
MessageBox.Show("找到控件");
}
//PostMessage(needbtn, 0x0201, 0, 0);
//PostMessage(needbtn, 0x0202, 0, 0);
}
//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption, int delay, int maxTries)
{
IntPtr mwh = IntPtr.Zero;
bool formFound = false;
int attempts = 0;
while (!formFound && attempts < maxTries)
{
if (mwh == IntPtr.Zero)
{
//MessageBox.Show("没有找到目标窗体", "错误提示");
Thread.Sleep(delay);
++attempts;
mwh = FindWindow(null, caption);
}
else
{
formFound = true;
}
}
if (mwh == IntPtr.Zero)
throw new Exception("Could not find main window");
else
return mwh;
}