Getting the position of the caret

  • Thread starter Thread starter kurotsuke
  • Start date Start date
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.
 
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!
 
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);
 
[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?
 
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.
 
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.
 
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);
}
 
Back
Top