I want to make sure that the window is open and shown on the screen (with support to more than one monitor of course), because I'm ordering Windows to fake a mouse click on the form so Google Translate textbox in the browser gets selected (I've tried a lot to select it but it doesn't work as not always it gets selected), so I don't want the mouse click to click on something else, I have made a 3x3 square of colors, and I want to check on the screen if these 9 colors are really on the screen then fast order the fake mouse click, I don't get the right colors when I test with this code:
What I have tried:
private void FormMain_Paint(object sender, PaintEventArgs e)
{
ClassSetGetPixel.SetPixel(this, pictureBoxSettings.Right + 50, 1, Color.Gray);
ClassSetGetPixel.SetPixel(this, pictureBoxSettings.Right + 50 + 1,1, Color.DarkGray);
ClassSetGetPixel.SetPixel(this, pictureBoxSettings.Right + 50 + 2, 1, Color.Brown);
ClassSetGetPixel.SetPixel(this, pictureBoxSettings.Right + 50, 2, Color.RosyBrown);
ClassSetGetPixel.SetPixel(this, pictureBoxSettings.Right + 50 + 1, 2, Color.DarkSlateGray);
ClassSetGetPixel.SetPixel(this, pictureBoxSettings.Right + 50 + 2, 2, Color.DarkGray);
ClassSetGetPixel.SetPixel(this, pictureBoxSettings.Right + 50, 3, Color.SlateGray);
ClassSetGetPixel.SetPixel(this, pictureBoxSettings.Right + 50 + 1, 3, Color.LightSlateGray);
ClassSetGetPixel.SetPixel(this, pictureBoxSettings.Right + 50 + 2, 3, Color.LightGray);
}
if (formMain.WindowState != FormWindowState.Minimized)
{
if ((GetPixel(formMain.PointToScreen(new Point(formMain.pictureBoxSettings.Right + 50, 1))) == Color.Gray) &&
(GetPixel(formMain.PointToScreen(new Point(formMain.pictureBoxSettings.Right + 50 + 1, 1))) == Color.DarkGray) &&
(GetPixel(formMain.PointToScreen(new Point(formMain.pictureBoxSettings.Right + 50 + 2, 1))) == Color.Brown) &&
(GetPixel(formMain.PointToScreen(new Point(formMain.pictureBoxSettings.Right + 50, 2))) == Color.RosyBrown) &&
(GetPixel(formMain.PointToScreen(new Point(formMain.pictureBoxSettings.Right + 50 + 1,2))) == Color.DarkSlateGray) &&
(GetPixel(formMain.PointToScreen(new Point(formMain.pictureBoxSettings.Right + 50 + 2, 2))) == Color.DarkGray) &&
(GetPixel(formMain.PointToScreen(new Point(formMain.pictureBoxSettings.Right + 50, 3))) == Color.SlateGray) &&
(GetPixel(formMain.PointToScreen(new Point(formMain.pictureBoxSettings.Right + 50 + 1, 3))) == Color.LightSlateGray) &&
(GetPixel(formMain.PointToScreen(new Point(formMain.pictureBoxSettings.Right + 50 + 2, 3))) == Color.LightGray))
{
Program.DoMouseClick(formMain.Location.X + formMain.pictureBoxSettings.Right + 1, formMain.Location.Y + formMain.pictureBoxSettings.Top + 100);
}
}
static public void SetPixel(Control control, int x, int y, Color color)
{
if (control != null)
{
IntPtr hDC = GetDC(control.Handle);
int argb = color.ToArgb();
int colorRef =
(int)((argb & 0x00FF0000) >> 16) |
(int)(argb & 0x0000FF00) |
(int)((argb & 0x000000FF) << 16);
SetPixel(hDC, x, y, colorRef);
ReleaseDC(control.Handle, hDC);
}
}
static Color GetPixel(Point p)
{
using (var bitmap = new Bitmap(1, 1))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(p, new Point(0, 0), new Size(1, 1));
}
return bitmap.GetPixel(0, 0);
}
}