static CaretLocation GetCaretLocation(DTE2 application) { // dark magic goes here IVsUIHierarchy uiHierarchy; uint itemID; IVsWindowFrame windowFrame; if (!VsShellUtilities.IsDocumentOpen(ServiceCache.ServiceProvider, application.ActiveDocument.FullName, Guid.Empty, out uiHierarchy, out itemID, out windowFrame)) { return(null); } IVsTextView vsTextView = Microsoft.VisualStudio.Shell.VsShellUtilities.GetTextView(windowFrame); IVsUserData userData = vsTextView as IVsUserData; if (userData == null) { return(null); } object holder; Guid guidViewHost = DefGuidList.guidIWpfTextViewHost; userData.GetData(ref guidViewHost, out holder); IWpfTextViewHost viewHost = holder as IWpfTextViewHost; if (viewHost == null) { return(null); } IWpfTextView wpfTextView = viewHost.TextView; System.Windows.UIElement uiElement = wpfTextView as System.Windows.UIElement; if (uiElement == null) { return(null); } var caretPos = wpfTextView.Caret.Position.BufferPosition; var bounds = wpfTextView.GetTextViewLineContainingBufferPosition(caretPos).GetCharacterBounds(caretPos); double zoomMultiplier = wpfTextView.ZoomLevel / 100.0; double left = (bounds.Left - wpfTextView.ViewportLeft) * zoomMultiplier; double top = (bounds.Top - wpfTextView.ViewportTop) * zoomMultiplier; double bottom = (bounds.Bottom - wpfTextView.ViewportTop) * zoomMultiplier; System.Windows.Point topPoint = new System.Windows.Point(left, top); System.Windows.Point bottomPoint = new System.Windows.Point(left, bottom); topPoint = uiElement.PointToScreen(topPoint); bottomPoint = uiElement.PointToScreen(bottomPoint); return(new CaretLocation( Convert.ToInt32(topPoint.X), Convert.ToInt32(topPoint.Y), Convert.ToInt32(bottomPoint.Y) )); }
//location func public static System.Windows.Point ElementPointToScreenPoint(System.Windows.UIElement element, System.Windows.Point pointoscreen) { return(element.PointToScreen(pointoscreen)); }