鼠标点击c#应用程序中的chrome
我正在尝试构建一个简单的应用程序,它所做的只是在Chrome浏览器中点击一个特定的点 我从代码开始这个过程鼠标点击c#应用程序中的chrome,c#,google-chrome,C#,Google Chrome,我正在尝试构建一个简单的应用程序,它所做的只是在Chrome浏览器中点击一个特定的点 我从代码开始这个过程 var proc = Process.Start("chrome.exe", "https://www.youtube.com/results?search_query=" + textBox1.Text); 现在我想用鼠标点击它 这就是我尝试过的 private void buttonGO_Click(object sender, EventArgs e) {
var proc = Process.Start("chrome.exe",
"https://www.youtube.com/results?search_query=" + textBox1.Text);
现在我想用鼠标点击它
这就是我尝试过的
private void buttonGO_Click(object sender, EventArgs e)
{
IntPtr iHandle = FindWindow(null, "Chrome");
if (iHandle != IntPtr.Zero)
{
SetForegroundWindow(iHandle);
Thread.Sleep(2000);
moveToPos(30000, 19500);
Thread.Sleep(500);
performClick(30000, 19500);
}
}
private void performClick(uint x, uint y)
{
SetCursorPos(x, y);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, x, y, 0, UIntPtr.Zero);
Thread.Sleep(200);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, x, y, 0, UIntPtr.Zero);
}
private void moveToPos(uint x, uint y)
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, UIntPtr.Zero);
}
但是它找不到窗户。
有人知道吗?你可以试试这个:
private void buttonGO_Click(object sender, EventArgs e)
{
// Get all chrome processes
Process[] chromeProcesses = Process.GetProcessesByName("chrome");
Process uiProcess = null;
foreach (Process process in chromeProcesses)
{
// Assuming you've opened chrome only once, the UI process will have MainWindowHandle, so get its reference and break out of loop
if (process.MainWindowHandle != IntPtr.Zero)
{
uiProcess = process;
break;
}
}
if (uiProcess == null)
return;
// Do your stuff here
IntPtr iHandle = uiProcess.MainWindowHandle;
if (iHandle != IntPtr.Zero)
{
SetForegroundWindow(iHandle);
Thread.Sleep(2000);
moveToPos(30000, 19500);
Thread.Sleep(500);
performClick(30000, 19500);
}
}
此外,如果您想执行许多自动鼠标单击操作,请尝试AutoIt3库,它非常适合此类操作