@Valleze , Welcome to Microsoft Q&A, based on my research, I find a solution to do it.
Please install following nuget-package:
Then you could try the following code to move mouse cursor to button location.
Code:
public int X { get; set; }
public int Y { get; set; }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(keyData.ToString()=="K")
{
Cursor.Position = new Point(X, Y);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void Form1_Load(object sender, EventArgs e)
{
Bitmap bitmap1 = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bitmap1, this.ClientRectangle);
bitmap1 = Example.ConvertToFormat((System.Drawing.Image)bitmap1, PixelFormat.Format24bppRgb);
System.Drawing.Bitmap template = Example.ConvertToFormat(System.Drawing.Image.FromFile("1.png"), PixelFormat.Format24bppRgb);
// create template matching algorithm's instance
// (set similarity threshold to 90%)
ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.9f);
// find all matchings with specified above similarity
TemplateMatch[] matchings = tm.ProcessImage(bitmap1, template);
// highlight found matchings
BitmapData data = bitmap1.LockBits(
new Rectangle(0, 0, bitmap1.Width, bitmap1.Height),
ImageLockMode.ReadWrite, bitmap1.PixelFormat);
foreach (TemplateMatch m in matchings)
{
Drawing.Rectangle(data, m.Rectangle, Color.White);
Console.WriteLine(m.Rectangle.Location.ToString());
// do something else with matching
X = m.Rectangle.X;
Y = m.Rectangle.Y;
}
}
public static class Example
{
public static Bitmap ConvertToFormat(this System.Drawing.Image image, PixelFormat format)
{
Bitmap copy = new Bitmap(image.Width, image.Height, format);
using (Graphics gr = Graphics.FromImage(copy))
{
gr.DrawImage(image, new Rectangle(0, 0, copy.Width, copy.Height));
}
return copy;
}
}
If you press K, the mouse cursor will move to button1's place.
Note: It is necessary for you to Maximize Window otherwise the posistion is not correct.
Result:
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.