In Visual Studio C# WinForms.Net, how to know the color of a pixel in my program's window as seen in screen?

  • Thread starter Thread starter Tam1988
  • Start date Start date
T

Tam1988

Guest
I want to invoke a fake mouse click on an empty place in my WinForm, how to know if the form is shown and isn't lagging because I don't want the click be on something else, I made a 3x3 square with 9 pixels of different colors and I want to check if the screen has these 9 colors in place so the fake click gets clicked immediately!





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);
}
}

Continue reading...
 
Back
Top