469,141 Members | 1,172 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,141 developers. It's quick & easy.

Getting the position of the caret

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.
Nov 16 '05 #1
6 7501
On 26/12/2004 kurotsuke wrote:
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!

--
Jeff Gaines
Posted with XanaNews 1.17.1.2 http://www.wilsonc.demon.co.uk/delphi.htm
Nov 16 '05 #2
On Sun, 26 Dec 2004 07:46:01 -0800, "Jeff Gaines"
<wh*********@newsgroup.nospam> wrote:
On 26/12/2004 kurotsuke wrote:
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);
Nov 16 '05 #3
On 26/12/2004 kurotsuke wrote:
On Sun, 26 Dec 2004 07:46:01 -0800, "Jeff Gaines"
<wh*********@newsgroup.nospam> wrote:
On 26/12/2004 kurotsuke wrote:
Hi,

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

[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?

--
Jeff Gaines
Posted with XanaNews 1.17.1.2 http://www.wilsonc.demon.co.uk/delphi.htm
Nov 16 '05 #4
On Sun, 26 Dec 2004 11:40:15 -0800, "Jeff Gaines"
<wh*********@newsgroup.nospam> wrote:

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.
Nov 16 '05 #5
On 26/12/2004 kurotsuke wrote:
On Sun, 26 Dec 2004 11:40:15 -0800, "Jeff Gaines"
<wh*********@newsgroup.nospam> wrote:

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.


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.

--
Jeff Gaines
Posted with XanaNews 1.17.1.2 http://www.wilsonc.demon.co.uk/delphi.htm
Nov 16 '05 #6
On Sun, 26 Dec 2004 13:06:56 -0800, "Jeff Gaines"
<wh*********@newsgroup.nospam> wrote:
On 26/12/2004 kurotsuke wrote:
On Sun, 26 Dec 2004 11:40:15 -0800, "Jeff Gaines"
<wh*********@newsgroup.nospam> wrote:
>
> 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.


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);
}
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by - ions | last post: by
4 posts views Thread by cc | last post: by
1 post views Thread by ryanmhuc | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.