locked
How to get line and column values from screen coordinates - VS2017 RRS feed

  • Question

  • I am working on a Visual Studio extension and need a way to convert screen coordinates to the line and column values for the file open in the VS editor at that point. Are there any methods to achieve this directly or known workarounds to get this information?

    I have found some older forum posts that mention the relevant APIs for this information were not publicly exposed as of VS2008 and a workaround using methods in IWpfTextView existed in VS2010.  Has this changed as of VS2017?
    Tuesday, April 17, 2018 7:36 PM

All replies

  • Hi cabryant20,

    >>I have found some older forum posts that mention the relevant APIs for this information were not publicly exposed as of VS2008 and a workaround using methods in IWpfTextView existed in VS2010.  Has this changed as of VS2017?

    From the following document, I could not find related properties or method to get line and column values from screen coordinates.

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.text.editor.iwpftextview?view=visualstudiosdk-2017

    In addition, you could refer the following code, which write a custom method to get ITextViewLine via YCoordinate.

    private ITextViewLine GetTextViewLine(double y)
    {
         IWpfTextView textView = _textViewHost.TextView;
         ITextViewLine textViewLineContainingYCoordinate = textView.TextViewLines.GetTextViewLineContainingYCoordinate(y);
         if (textViewLineContainingYCoordinate == null)
              textViewLineContainingYCoordinate = (y <= textView.TextViewLines[0].Top) ? textView.TextViewLines.FirstVisibleLine : textView.TextViewLines.LastVisibleLine;
    
         return textViewLineContainingYCoordinate;
    }

    https://github.com/tunnelvisionlabs/InheritanceMargin/blob/master/Tvl.VisualStudio.InheritanceMargin/InheritanceGlyphMouseHandler.cs

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 18, 2018 5:51 AM
  • Thanks for the response. I will try approaching it this way and followup with results. 

    Thursday, April 19, 2018 3:04 PM