public HitTestMetrics[] HitTestTextRange(int textPosition, int textLength, float originX, float originY) { HitTestTextRange(textPosition, textLength, originX, originY, null, out int actualHitTestMetricsCount); var hitTestMetrics = new HitTestMetrics[actualHitTestMetricsCount]; if (actualHitTestMetricsCount > 0) { HitTestTextRange(textPosition, textLength, originX, originY, hitTestMetrics, out _).CheckError(); } return(hitTestMetrics); }
public int GetCaretIndex(Point p) { SharpDX.Bool isTrailingHit; SharpDX.Bool isInside; HitTestMetrics result = this.DirectWriteTextLayout.HitTestPoint( (float)p.X, (float)p.Y, out isTrailingHit, out isInside); return(result.TextPosition + (isTrailingHit ? 1 : 0)); }
/// <summary> /// The application calls this function to get a set of hit-test metrics corresponding to a range of text positions. /// One of the main usages is to implement highlight selection of the text string. /// The function returns E_NOT_SUFFICIENT_BUFFER, which is equivalent to HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), /// when the buffer size of hitTestMetrics is too small to hold all the regions calculated by the function. /// In this situation, the function sets the output value *actualHitTestMetricsCount to the number of geometries calculated. /// The application is responsible for allocating a new buffer of greater size and calling the function again. /// A good value to use as an initial value for maxHitTestMetricsCount may be calculated from the following equation: /// maxHitTestMetricsCount = lineCount * maxBidiReorderingDepth where lineCount is obtained from the value of the output argument *actualLineCount (from the function IDWriteTextLayout::GetLineLengths), and the maxBidiReorderingDepth value from the DWRITE_TEXT_METRICS structure of the output argument *textMetrics (from the function IDWriteFactory::CreateTextLayout). /// </summary> /// <param name="textPosition">The first text position of the specified range. </param> /// <param name="textLength">The number of positions of the specified range. </param> /// <param name="originX">The origin pixel location X at the left of the layout box. This offset is added to the hit-test metrics returned. </param> /// <param name="originY">The origin pixel location Y at the top of the layout box. This offset is added to the hit-test metrics returned. </param> /// <returns>a reference to a buffer of the output geometry fully enclosing the specified position range. The buffer must be at least as large as maxHitTestMetricsCount. </returns> /// <unmanaged>HRESULT IDWriteTextLayout::HitTestTextRange([None] int textPosition,[None] int textLength,[None] float originX,[None] float originY,[Out, Buffer, Optional] DWRITE_HIT_TEST_METRICS* hitTestMetrics,[None] int maxHitTestMetricsCount,[Out] int* actualHitTestMetricsCount)</unmanaged> public HitTestMetrics[] HitTestTextRange(int textPosition, int textLength, float originX, float originY) { var hitTestMetrics = new HitTestMetrics[0]; int actualHitTestMetricsCount = 0; HitTestTextRange(textPosition, textLength, originX, originY, hitTestMetrics, 0, out actualHitTestMetricsCount); if (actualHitTestMetricsCount > 0) { hitTestMetrics = new HitTestMetrics[actualHitTestMetricsCount]; HitTestTextRange(textPosition, textLength, originX, originY, hitTestMetrics, actualHitTestMetricsCount, out actualHitTestMetricsCount); } return(hitTestMetrics); }
public int GetCaretIndex(FormattedText text, Point point, Size constraint) { using (TextLayout layout = GetTextLayout(this.factory, text, constraint)) { SharpDX.Bool isTrailingHit; SharpDX.Bool isInside; HitTestMetrics result = layout.HitTestPoint( (float)point.X, (float)point.Y, out isTrailingHit, out isInside); return(result.TextPosition + (isTrailingHit ? 1 : 0)); } }
protected override void OnMouseClick(MouseEventArgs e) { base.OnMouseClick(e); bool isTrailingHit; bool isInside; HitTestMetrics hitTestMetrics = this._textLayout.HitTestPoint(e.X, e.Y, out isTrailingHit, out isInside); if (isInside) { bool underline = _textLayout.GetUnderline((int)hitTestMetrics.TextPosition); _textLayout.SetUnderline(!underline, new TextRange((int)hitTestMetrics.TextPosition, 1)); Render(); } }
/// <summary> /// The application calls this function to get a set of hit-test metrics corresponding to a range of text positions. /// One of the main usages is to implement highlight selection of the text string. /// The function returns E_NOT_SUFFICIENT_BUFFER, which is equivalent to HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), /// when the buffer size of hitTestMetrics is too small to hold all the regions calculated by the function. /// In this situation, the function sets the output value *actualHitTestMetricsCount to the number of geometries calculated. /// The application is responsible for allocating a new buffer of greater size and calling the function again. /// A good value to use as an initial value for maxHitTestMetricsCount may be calculated from the following equation: /// maxHitTestMetricsCount = lineCount * maxBidiReorderingDepth where lineCount is obtained from the value of the output argument *actualLineCount (from the function IDWriteTextLayout::GetLineLengths), and the maxBidiReorderingDepth value from the DWRITE_TEXT_METRICS structure of the output argument *textMetrics (from the function IDWriteFactory::CreateTextLayout). /// </summary> /// <param name="textPosition">The first text position of the specified range. </param> /// <param name="textLength">The number of positions of the specified range. </param> /// <param name="originX">The origin pixel location X at the left of the layout box. This offset is added to the hit-test metrics returned. </param> /// <param name="originY">The origin pixel location Y at the top of the layout box. This offset is added to the hit-test metrics returned. </param> /// <returns>a reference to a buffer of the output geometry fully enclosing the specified position range. The buffer must be at least as large as maxHitTestMetricsCount. </returns> /// <unmanaged>HRESULT IDWriteTextLayout::HitTestTextRange([None] int textPosition,[None] int textLength,[None] float originX,[None] float originY,[Out, Buffer, Optional] DWRITE_HIT_TEST_METRICS* hitTestMetrics,[None] int maxHitTestMetricsCount,[Out] int* actualHitTestMetricsCount)</unmanaged> public HitTestMetrics[] HitTestTextRange(int textPosition, int textLength, float originX, float originY) { var hitTestMetrics = new HitTestMetrics[0]; int actualHitTestMetricsCount = 0; HitTestTextRange(textPosition, textLength, originX, originY, hitTestMetrics, 0, out actualHitTestMetricsCount); if (actualHitTestMetricsCount > 0) { hitTestMetrics = new HitTestMetrics[actualHitTestMetricsCount]; HitTestTextRange(textPosition, textLength, originX, originY, hitTestMetrics, actualHitTestMetricsCount, out actualHitTestMetricsCount); } return hitTestMetrics; }