How to discover the screen coordinates of "another"/external form.

Posted on 2006-03-31
Last Modified: 2010-04-16

I am doing some "ghost" controlling of external applications from my app. but these rely on exact screen locations for the button clicks.

How can I find out the screen coordinates of another window that is not attached to my application.  I can retrieve the handle but how do I then use that to find the x,y for the top left and bottom right (for example).  With this information I can then work out where to click.

Many thanks.

Question by:JAMES
    LVL 4

    Accepted Solution

            public static extern int FindWindow(string lpClassName, string lpWindowName);
            public static extern int GetWindowRect(int hwnd, ref Rectangle lprect);

    I assume youve used FindWindow allready to get the handle of the window you want to 'remote control'
    Getwindowrect will give us the rectangle of that screens position if called as follows:

    Rectangle TargetWindow = new Rectangle();
    int hwnd = FindWindow("CabinetWClass", "My Computer");
    GetWindowRect(hwnd, ref TargetWindow) ;

    in this case ive just got a handle to the my computer window, which is of class 'CabintWClass'.
    If you need to find out the class of a window, use Spy++.

    Any more questions?

    Author Comment

    Sorry for the delay - that's perfect.

    Thanks very much.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now