我已经成功编写了一个应用程序来按下另一个应用程序中的按钮。现在我试图循环重复按下按钮,我的应用程序挂起,但我不明白为什么。
语境
我有一个对我很有帮助的应用程序,但开发它的人并没有考虑到所有问题。在应用程序中的某个位置,会打开一个对话框,要求确认是否用上传的数据替换现有数据。我需要点击OK同意,但问题是我将大量数据上传到此应用程序并且它没有“适用于所有”复选框。所以我必须OK反复点击。因此,我正在开发一个应用程序,它将OK为我按下按钮,直到对话框停止出现。
代码
单击按钮一次的代码(这有效)...
private void btnOKloop_Click(object sender, System.EventArgs e)
{
int hwnd=0;
IntPtr hwndChild = IntPtr.Zero;
//Get a handle for the Application main window
hwnd = FindWindow(null, "Desired MessageBox");
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "OK");
//send system message
if (hwnd != 0)
{
SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);
}
else
{
MessageBox.Show("Button Could Not Be Found!", "Warning", MessageBoxButtons.OK);
}
}
在循环中单击按钮的代码(这挂起)...
private void btnOKloop_Click(object sender, System.EventArgs e)
{
int hwnd=0;
IntPtr hwndChild = IntPtr.Zero;
hwnd = FindWindow(null, "Desired MessageBox");
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "OK");
do
{
SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);
} while (hwnd != 0);