We help IT Professionals succeed at work.

Get screen position of caret in an external application

jlach01 asked
Last Modified: 2013-12-16

I'm trying to get the screen position of an external application that I have no control over (ie: Notepad). I then want to show a form at this position. I have code that is working, partially. The method get's called on every keystroke in the window (using an IME/TSF to capture these strokes). The form shows and moves to the to the right (the Left property changes) as I type, the problem is the window is no where near the caret. Here's the code I have:

        // Win32 Calls
        public struct RECT {
            public uint Left;
            public uint Top;
            public uint Right;
            public uint Bottom;
        public struct GUITHREADINFO {
            public uint cbSize;
            public uint flags;
            public IntPtr hwndActive;
            public IntPtr hwndFocus;
            public IntPtr hwndCapture;
            public IntPtr hwndMenuOwner;
            public IntPtr hwndMoveSize;
            public IntPtr hwndCaret;
            public RECT rcCaret;
        [DllImport("user32.dll", EntryPoint = "GetGUIThreadInfo")]
        public static extern bool GetGUIThreadInfo(uint tId, out GUITHREADINFO threadInfo);
        // Get the caret position
       public static RECT GetCaretPosition() {
            GUITHREADINFO gInfo = new GUITHREADINFO();
            gInfo.cbSize = (uint)Marshal.SizeOf(gInfo);
            GetGUIThreadInfo(0, out gInfo);
            return gInfo.rcCaret;

As I said, setting the Form.Left and Form.Top properties to the gInfo.rcCaret.Left/Top properties causes the form to show far away from the actual caret position on screen. The Left property of the form does update as I type, however.

Any thoughts as to how I can get this to work?
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.