Using GetCursorPos from C# code

D

Dmitry Shaporenkov

Hi,

I'd like to display a form at the caret position, so I
call GetCursorPos from user32.dll. The question is how
should I use the coordinates returned by this function in
C# code? I'm trying to pass them to the
Form.SetDesktopLocation but the form is displayed far
from the caret position.

Thanks in advance,
Dmitry
 
H

Herfried K. Wagner [MVP]

* "Dmitry Shaporenkov said:
I'd like to display a form at the caret position, so I
call GetCursorPos from user32.dll. The question is how
should I use the coordinates returned by this function in
C# code? I'm trying to pass them to the
Form.SetDesktopLocation but the form is displayed far
from the caret position.

Are you talking about the cursor or a caret?
 
D

Dmitry Shaporenkov

Hi Herfied,

actually I've already solved the problem. Sorry for confusion, I was
talking about caret. The code that works fine looks like:
...
[DllImport("user32.dll", EntryPoint="GetCaretPos"]
static extern bool GetCaretPos (ref Point lpPoint);

Point p = new Point ();
GetCaretPos (ref p);
IntPtr hWnd = GetFocus ();
ClientToScreen (hWnd, ref p);
form.SetDesktopLocation (p.X, p.Y);
...

I have to use so hard way to obtain the current caret position, because
the window that owns the caret is unmanaged.
 
J

Joe White

You don't need to use P/Invoke for this. It's already available through the
..NET Framework. Just read Cursor.Position.
 

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