鼠标点击chrome从c#应用程序
本文关键字:应用程序 chrome 鼠标 | 更新日期: 2023-09-27 18:02:09
我正在尝试构建一个简单的应用程序,它所做的就是点击Chrome浏览器中的特定位置。
我从代码
开始这个过程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库,它是完美的这样的事情。