Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
    }
}
Posted
Updated 28-Mar-20 6:17am
v4
Comments
Richard MacCutchan 27-Mar-20 5:47am    
What colours do you get? Are you sure that your pixel addresses are correct?
john1990_1 27-Mar-20 5:50am    
I made a Color c=GetColor(); of the first address and it returned a random and unnamed color (as "Named color" was false), however when I do a mouse click on the first location it presses correctly on the location of the drawn pixels... maybe it's because of blurr from the bar of the form (window) and I should get the pixels a little down to run away from the blurr.
john1990_1 27-Mar-20 5:54am    
Putting the pixels at location Y=20,21,22 didn't work.
Richard MacCutchan 27-Mar-20 6:00am    
On of the problems with this may be that Windows controls the physical screen and often colours will be merged, so that what you paint into a form is actually modified when it shows on the screen.
john1990_1 27-Mar-20 6:01am    
I need to be sure, so I give up if that's true, how can we know for sure whether this is the problem?

1 solution

Visual Studio comes with a tool called "Spy++". Use that to find controls in a given window. To manipulate a control in a window in an external app, you're going to need to use System.Runtime.InteropServices.

You're gonna need to search for the tool because VS no longer puts a shortcut to it in the windows Start menu. I found it and added a custom item under the Tools menu in VS.
 
Share this answer
 
Comments
john1990_1 28-Mar-20 8:28am    
Does this way make sure the window isn't in lag and is in fact shown on the screen with colors? I mean is this the same as if one called GetPixel on the display it detects the colors shown on the window and it isn't opening or in lag?
#realJSOP 28-Mar-20 10:03am    
Has nothing to do with colors - just allows you to find windows and controls that are currently active in windows.Z However, once you find the desired control window, you can discover whatever window properties you might want. This is going to require some googling on your part. You can use Spy++ to inspect a given window to see if you can get a lead on what to look for. Caveat, there's no "fast way" to find out what to do, unless you happen to stumble across a fairly complete assembly that contains all of the necessary InteropServices calls.
john1990_1 28-Mar-20 10:07am    
I want to invoke a mouse click on an empty point on the form to select input to Google Translate's source textbox, I want to make sure the form is shown, I want to checks colors on the display that are in the form, so the click doesn't go to liking a post on FB or something, how do I make sure the window is not in lag and its colors are actually on the display? does the method you're suggesting make sure the click doesn't go to another window if my program is in lag for being displayed on the display?
#realJSOP 28-Mar-20 10:32am    
You can use InteropServices to send a window an event. I don't know (or care) what you want to do, but it can (and probably requires) be done with interopservices since it involves another window in an external app. Like I mentioned before, google is your friend.

BTW, a good "empty point" is the top/left pixel of the window's client area. There's almost never a control there. The color of the pixel at that point is inconsequential.
john1990_1 28-Mar-20 10:37am    
You don't understand, I have a Windows Forms program, in this program there's a web browser control, in this web browser control Google Translate is sometimes navigated to, I want to focus on the "source" textbox in the web browser control, I tried many way and I didn't find a way that always does select the input on the source textbox, I fake a mouse click from my program on my program, on an empty place in my program (not on the browser control), I want this click from my program to my program, I want to make sure that my program is shown on the display and isn't lagging to open, because if my program thinks my program is shown on the display and it does a click thinking my program is there to catch the click, the click my be clicked on "Like" on FB if my window of my program wasn't shown on the display!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900