Using GetCursorPos from C# code

  • Thread starter Thread starter Dmitry Shaporenkov
  • Start date Start date
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
 
* "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?
 
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.
 
You don't need to use P/Invoke for this. It's already available through the
..NET Framework. Just read Cursor.Position.
 
Back
Top