我想用SendMessage向"4GB Patch"这个对话框里面的"OK"按钮发送鼠标单击的消息,但是现在的情况是窗口和按钮的句柄都正确,但是按钮就是无法被按下,请各位大神看看是哪里出了问题。感谢!
PS.打开exe程序以后会弹出一个文件选择框,选择完文件以后才会弹出"4GB Patch"的对话框。虽然按道理说可以用遍历打开的程序然后关闭,但是我觉得效率可能在大部分情况下都没有查找窗口句柄来得快,这个程序只是用来测试的,不是我最终的程序,所以不要纠结于实用性和其他代码设计的合理性。
public partial class Form1 : Form
{
public const int WM_LBUTTONDOWN = 0x0201;
public const int WM_LBUTTONUP = 0x0202;
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport("User32.dll")]
public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
[DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = "C:/Users/yangy/Desktop/4gb_patch.exe";
process.Start();
Process process = new Process();
process.StartInfo.FileName = "C:/Users/yangy/Desktop/4gb_patch.exe";
process.Start();
for (int i = 0; i < 1;)
{
if (FindWindow(null, "4GB Patch") != null)
{
IntPtr window = FindWindow(null, "4GB Patch");
IntPtr button = FindWindowEx(window, IntPtr.Zero, "Button", "OK");
SetForegroundWindow(window);
SetForegroundWindow(button);
SendMessage(button, WM_LBUTTONDOWN, IntPtr.Zero, null);
SendMessage(button, WM_LBUTTONUP, IntPtr.Zero, null);
}
else
{
i = 2;
}
}
}
}