Cursor Position

Does anyone know how to get cursor position (screen coordinates) from the editor I need to show custom popup window that needs to be shown below the cursor (like intelisense menu).
Thanks.


Answer this question

Cursor Position

  • smaynard123

    Werdna,

    I'm not sure it's accessible from an add-in - using VSIP here.

    Best luck!

  • diwakar_pal11

    Werdna,

    Haven't tried it myself, but I can't see why it shouldn't work. Call Win32's GetCaretPos() to get the client rect coords. Win32 GetFocus() should give you the HWND of the window with the focus (which is where the caret is), and from there you can convert to screen coords. You can double-check it with the window handle returned by IVsTextView::GetWindowHandle() to make sure you are not messing anything.

    Best luck!

  • greg_burns

    yep, missed that part about "screen coordinates" in the original post.
    and about "Your x,y should be line,column actually." , that's just picky.
    but no hard feelings, i've even tryed to help with your question (look at your post) as much as i could.

  • JoelSSIS

    What namespace does IVsTextView reside in
    I'm doing this as on AddOn (no VSIP package installed).

  • szembek

    //if youv'e got the DTE object and it's called "m_dte", then this c# code does it for
    // me:
     

    IVsTextView
    activeView = null;

    ServiceProvider sp = new

    ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)m_dte);

    IVsTextManager vsTextMgr =

    (IVsTextManager)GetService(typeof(SVsTextManager));

    vsTextMgr.GetActiveView(1, null, out activeView);

    int x, y;

    activeView.GetCaretPos(out x, out y);

    // x and y will hold the coordinates 


  • Marcas

    Eran,

    That should buy you the coordinates as a line and column within the text buffer. It's what you need in order to insert some text, etc... But they are not the "screen coordinates" as the OP requested. Given that there may be folding, word-wrap, and what not, their relationship to screen coordinates is very, very indirect. AFAIK, there is no way to get screen coordinates from the TextView - and getting it from Win32 would be the only way. Let me re-check...

    I just checked again. Actually, there is a way w/o messing with Win32 IVsTextView::GetPointOfLineColumn() will do it from the line & column you obtained above. So it would just be necessary to add something like:

    int line, column;

    activeView.GetCaretPos(out line, out column);

    POINT pt;
    activeView.GetPointOfLineColumn(line, col, out pt);

    Your x,y should be line,column actually.

    I use C++, not C#, so I'm a bit unsure about my syntax, but else it should be ok.

    I'm only hoping someone will be able to answer my question about how to make VS open standard files with my editor when debugging and jumping to compile errors!!!

  • ihsanakin

    Sorry about the pickiness :) To try to redeem myself: I wouldn't have said anything if you had called the line "y" and the column "x".

    Anyway, thanks so much for your answer to my question. I rushed to check it, pity it didn't work, but I'm still very grateful for your tip.

    If it had worked or led me to a solution I would have loved you immensely. Honest.

    Thanks again

  • Jigar Lakhani

    This thread has been super helpful in helping me cross the threshhold from an add-in programmer to a vsip programmer, so thanks!

    For future copy/paste, here's a corrected C# code sequence, which requires the following VSIP assemblies to be referenced: microsoft.visualstudio.ole.interop; microsoft.visualstudio.shell; microsoft.visualstudio.textmanager.interop.

    ServiceProvider sp = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider) this.applicationObject);

    IVsTextManager vsTextMgr = (IVsTextManager) sp.GetService(typeof(SVsTextManager));

    IVsTextView activeView = null;

    vsTextMgr.GetActiveView(1, null, out activeView);

    int line, col;

    activeView.GetCaretPos(out line, out col);

    Microsoft.VisualStudio.OLE.Interop.POINT[] pt = new Microsoft.VisualStudio.OLE.Interop.POINT[1];

    activeView.GetPointOfLineColumn(line, col, pt);

    Console.WriteLine("{0},{1}", pt[0].x, pt[0].y);


  • Cursor Position