Link to home
Start Free TrialLog in
Avatar of Ragine
Ragine

asked on

Find x/y position of another program

Hello Experts, this might be an easy question but I want to find the x/y of a position on my form and/or another application.

Form mouse position:
- Lets say I want to find a button on my form and/or a random part on my form. If I find it manually lets say its x/y is 234/1123 well once I move the form the x/y will change. I want to find that exact spot and get its x/y even if it moves.

Another Application position:
- I want to find the x/y position of a button on that application, even if it moves I want to find that exact position.

Thank you for your time.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ragine
Ragine

ASKER

Wouldn't the handle change each time it starts? Wouldn't findwindow be best? How can I implement findwindow to the following code?  

http://www.pinvoke.net/default.aspx/user32.getwindowrect

I gave it a simple try but with no luck.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ragine

ASKER

Alright sounds good. All I did was change this

IntPtr Class = FindWindow("#32770 (Dialog)", null);//Class-Caption
IntPtr Caption = FindWindow(null, "Windows Task Manager");//Class-Caption

 if (!GetWindowRect(new HandleRef(Class, Caption), out rct))
{
       MessageBox.Show("ERROR");
       return;
}

Open in new window


from the code provided above and it seems to work. I'll keep this open for a bit longer just incase something else comes up.
Avatar of Ragine

ASKER

What about finding child windows. Like for instance TaskManager and its "New Task" button, could the same method apply? Because I tried the following code and it didn't work.

[DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent,
             IntPtr hwndChildAfter,
             string lpszClass,
            string lpszWindow
        );

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
        }

        Rectangle myRect = new Rectangle();

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                RECT rct;

                IntPtr MainWindow = FindWindow("#32770 (Dialog)", "Windows Task Manager");//Class-Caption
                IntPtr NewTask = FindWindowEx(MainWindow, IntPtr.Zero, "Button", "&New Task...");

                //IntPtr Class = FindWindowEx("Button", null);//Class-Caption
                //IntPtr Caption = FindWindowEx(null, "Button");//Class-Caption

                GetWindowRect(NewTask, out rct);

                myRect.X = rct.Left;
                myRect.Y = rct.Top;
                myRect.Width = rct.Right - rct.Left + 1;
                myRect.Height = rct.Bottom - rct.Top + 1;

                MessageBox.Show("X:  " + myRect.X.ToString() + "    Y:  " + myRect.Y.ToString());
            }
            catch { }
        }

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ragine

ASKER

Yeah looked back into Spy++ and I got something wrong but still the same issue.

IntPtr MainWindow = FindWindow("#32770 (Dialog)", "Windows Task Manager");//Class-Caption
IntPtr Applications = FindWindowEx(MainWindow, IntPtr.Zero, "#32770 (Dialog)", "Applications");
IntPtr NewTask = FindWindowEx(Applications, IntPtr.Zero, "Button", "&New Task...");

Open in new window

Not sure...I'm running Win 8 which has a different task manager so I can't check it right now.
Avatar of Ragine

ASKER

Nevermind, the code works but for some reason not for taskmanager. Anyways luckily I don't need to use taskmanager for anything. I'll keep this open once again just incase.