Native C++ VS2010 Addin - Caret position screen coordinates RRS feed

  • Question

  • Hello,
    I need to get screen coordinates of caret position within text editor in VS 2010.
    Note, that is it not a .NET project (it is ATL), so it would be fine if it would work without need to create interop library.

    Does anything accessible from native C++ Addin provide a way to get information I need?

    Seems that WPF does not cooperate with Win32 API :(
    I have tried following Win32 API calls without success:

    HWND hwnd = ::GetForegroundWindow();
    DWORD remoteThreadId = ::GetWindowThreadProcessId(hwnd, NULL);
    DWORD currentThreadId = ::GetCurrentThreadId();
    ::AttachThreadInput(remoteThreadId, currentThreadId, TRUE);
    HWND focused = ::GetFocus();
    POINT pnt;
    BOOL caret = ::GetCaretPos(&pnt) && ::ClientToScreen(focused, &pnt);
    ::AttachThreadInput(remoteThreadId, currentThreadId, FALSE);

    and also:

    HWND hwnd = ::GetForegroundWindow();
    DWORD remoteThreadId = ::GetWindowThreadProcessId(hwnd, NULL);
    GUITHREADINFO thr_info;thr_info.cbSize = sizeof(thr_info);
    if (GetGUIThreadInfo(remoteThreadId, &thr_info))

    Thanks in advance.

    Saturday, January 25, 2014 5:36 PM

All replies

  • Hi Dušan Paulovič,

    Thank you for posting here!

    This forum is to discuss and ask questions about Visual Studio Extensibility (VSX), using the Extension Manager, building VSIX containers for deployment, the Visual Studio SDK, Text Templates (T4), add-ins, macros, and more.

    According to the information you mentioned above, I’m not clear that what visual studio extension you want to realize. Could you please clarify it? Thank you!

    Best regards,


    Tuesday, January 28, 2014 3:32 AM
  • Hi Sunny, thanks for response, I think that all is somehow told in my post, but I will pack it little bit.

    I am working on Visual Studio 2010 old style ATL/COM add-in (not a .NET one).

    What I need to know, is how I can from within ATL add-in get screen coordinates of a caret in text editor. 

    In .NET project I could perhaps use Microsoft.VisualStudio.Shell.ServiceProvider and Microsoft.VisualStudio.TextManager.Interop.IVsTextManager, but I do not know equivalents for those interfaces in unmanaged, COM based environment. Currently my tool uses only interfaces from namespace EnvDTE80.


    Tuesday, January 28, 2014 6:43 PM
  • Hi,

    I'm unfamiliar with those ALT interfaces you mention, but maybe the following helps you anyway:There is a method "GetWindowRect(IntPtr hwnd, out RECT rect)" in "user32.dll" which returns the rectangle (in screen corrdinates/pixels) of the given window. So you at least have the position of the current window.

    Maybe it's as simple as adding the coordinates of the returned top/left corner to the result of your call to "ClientToScreen()" (although I honestly doublt it, would be too simple). Other than that you could perhaps get the pixel coordinates manually. A code-editor hopefully always uses a monospace typefont, so you could calculate the position by determining the width and height of a single monospace character and apply correction for the current scroll position inside the window. But then again, I have no idea if you have any means to get the state of the scrollbars, not to speak of the width and height of a character...


    • Edited by f_max Monday, February 3, 2014 12:33 PM
    Monday, February 3, 2014 12:32 PM