C# 自动在屏幕上单击鼠标
我正在寻找一种方法来创建一个程序,将执行鼠标点击,它发现屏幕上的某种颜色 例如,如果屏幕上有一个红色框,我希望程序单击它中间的红色框C# 自动在屏幕上单击鼠标,c#,.net,windows,automation,C#,.net,Windows,Automation,我正在寻找一种方法来创建一个程序,将执行鼠标点击,它发现屏幕上的某种颜色 例如,如果屏幕上有一个红色框,我希望程序单击它中间的红色框 我如何才能在C#中实现这一点?因为您只需要一个通用的方法,我并没有真正做到完美,但这里有一个想法: 有拍摄屏幕快照的方法: public Bitmap ScreenShot() { var screenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
我如何才能在C#中实现这一点?因为您只需要一个通用的方法,我并没有真正做到完美,但这里有一个想法: 有拍摄屏幕快照的方法:
public Bitmap ScreenShot()
{
var screenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(screenShot))
{
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
}
return screenShot;
}
以及在位图中查找特定颜色的方法:
请注意,使用不安全代码和锁位(读和写)可以极大地改进此实现
最后,总结一下:
public bool ClickOnFirstPixel(Color color)
{
var pt = GetFirstPixel(ScreenShot(), color);
if (pt.HasValue)
{
Click(pt.Value);
}
// return whether found pixel and clicked it
return pt.HasValue;
}
那么,用法将是:
if (ClickOnFirstPixel(Color.Red))
{
Console.WriteLine("Found a red pixel and clicked it!");
}
else
{
Console.WriteLine("Didn't find a red pixel, didn't click.");
}
很抱歉我一个字也听不懂。你需要进一步澄清你的意思。你是要告诉它在哪里点击,还是它一定要找到红色的盒子?如果有两个红色的盒子呢?一个“盒子”由多少像素组成etc@GazTheDestroyer我不是在寻找一个解决方案,只是一种实现它的方法,因为在这种情况下,屏幕上只有一个红色框screen@JamesTeare所以从技术上讲,你只需在屏幕上找到一个红色像素,点击它,然后停止?你的应用程序是否会处理鼠标点击?
public bool ClickOnFirstPixel(Color color)
{
var pt = GetFirstPixel(ScreenShot(), color);
if (pt.HasValue)
{
Click(pt.Value);
}
// return whether found pixel and clicked it
return pt.HasValue;
}
if (ClickOnFirstPixel(Color.Red))
{
Console.WriteLine("Found a red pixel and clicked it!");
}
else
{
Console.WriteLine("Didn't find a red pixel, didn't click.");
}