jlach01
asked on
Get screen position of caret in an external application
Hi,
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
[StructLayout(LayoutKind.S equential) ]
public struct RECT {
public uint Left;
public uint Top;
public uint Right;
public uint Bottom;
};
[StructLayout(LayoutKind.S equential) ]
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?
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
[StructLayout(LayoutKind.S
public struct RECT {
public uint Left;
public uint Top;
public uint Right;
public uint Bottom;
};
[StructLayout(LayoutKind.S
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.