Getting the position of the caret

K

kurotsuke

Hi,

I need to know the position (X, Y cohordinates) of the caret. I'm
using the function:

[DllImport("user32.dll")]
private static extern int GetCaretPos(ref POINT lpPoint);

I'm actually getting a result but I think that refers to the
cohordinates of the Textbox the caret is in and not the screen
cohordinates.

I need a global cohordinate since I want to show a window at that
position.

Moreover, I'd need to find the position of the caret not for my
application window but for the current active application.

Thanks.
 
J

Jeff Gaines

Hi,

I need to know the position (X, Y cohordinates) of the caret. I'm
using the function:

[DllImport("user32.dll")]
private static extern int GetCaretPos(ref POINT lpPoint);

I'm actually getting a result but I think that refers to the
cohordinates of the Textbox the caret is in and not the screen
cohordinates.

I need a global cohordinate since I want to show a window at that
position.

Moreover, I'd need to find the position of the caret not for my
application window but for the current active application.

Thanks.

Have a look in the help for PointToClient and PointToScreen.
I won't try and tell you which one to use as I always get them confused!
 
K

kurotsuke

Hi,

I need to know the position (X, Y cohordinates) of the caret. I'm
using the function:

[DllImport("user32.dll")]
private static extern int GetCaretPos(ref POINT lpPoint);

I'm actually getting a result but I think that refers to the
cohordinates of the Textbox the caret is in and not the screen
cohordinates.

I need a global cohordinate since I want to show a window at that
position.

Moreover, I'd need to find the position of the caret not for my
application window but for the current active application.

Thanks.

Have a look in the help for PointToClient and PointToScreen.
I won't try and tell you which one to use as I always get them confused!

Thanks.
I've tried and come up with the following code. It works OK when my
application has the focus but I would like to display the window near
the caret also when other applications has the focus. How can I do it?
Thanks a lot.

Point mypoint=Point.Empty;
GetCaretPos(ref mypoint);
IntPtr hWnd = GetFocus ();
ClientToScreen (hWnd, ref mypoint);
this.SetDesktopLocation (mypoint.X, mypoint.Y);
ShowWindow(this.Handle, 4);
 
J

Jeff Gaines

[snipped]

Have a look in the help for PointToClient and PointToScreen.
I won't try and tell you which one to use as I always get them
confused!

Thanks.
I've tried and come up with the following code. It works OK when my
application has the focus but I would like to display the window near
the caret also when other applications has the focus. How can I do it?
Thanks a lot.

Point mypoint=Point.Empty;
GetCaretPos(ref mypoint);
IntPtr hWnd = GetFocus ();
ClientToScreen (hWnd, ref mypoint);
this.SetDesktopLocation (mypoint.X, mypoint.Y);
ShowWindow(this.Handle, 4);

That's starting to get complex.

If the user is using another app, i.e. you app does not have focus,
what do you plan to use to trigger your ShowWindow routine?
 
K

kurotsuke

That's starting to get complex.

If the user is using another app, i.e. you app does not have focus,
what do you plan to use to trigger your ShowWindow routine?

The code I posted is just sample code. I was testing it using a timer
that fires every few seconds. In my real application I'm going to use
hooks to detect when certain keys are pressed and plan to show a
window where the user is typing.
Thanks.
 
J

Jeff Gaines

The code I posted is just sample code. I was testing it using a timer
that fires every few seconds. In my real application I'm going to use
hooks to detect when certain keys are pressed and plan to show a
window where the user is typing.
Thanks.

Sorry, that's a bit beyond me. I did set up a system wide keyboard hook
once but had to use C++, I believe you can't do it with .NET.

I'll have to leave it for the gurus.
 
K

kurotsuke

Sorry, that's a bit beyond me. I did set up a system wide keyboard hook
once but had to use C++, I believe you can't do it with .NET.

I'll have to leave it for the gurus.

For those who may be interested I managed to solve the problem with
this code:

IntPtr hFocus = GetFocus();
IntPtr hFore;
uint id=0;

if (hFocus == IntPtr.Zero) {
hFore= GetForegroundWindow ();
AttachThreadInput(GetWindowThreadProcessId(hFore, out id),
GetCurrentThreadId(), true);
hFocus = GetFocus();
GetCaretPos(ref mypoint);
ClientToScreen (hFocus, ref mypoint);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top