BrightIdeasSoftware.HighlightTextRenderer.DrawGdiTextHighlighting C# (CSharp) Method

DrawGdiTextHighlighting() protected method

Draw the highlighted text using GDI
protected DrawGdiTextHighlighting ( Graphics g, Rectangle r, string txt ) : void
g System.Drawing.Graphics
r System.Drawing.Rectangle
txt string
return void
        protected virtual void DrawGdiTextHighlighting(Graphics g, Rectangle r, string txt)
        {
            TextFormatFlags flags = TextFormatFlags.NoPrefix |
                TextFormatFlags.VerticalCenter | TextFormatFlags.PreserveGraphicsTranslateTransform;

            // TextRenderer puts horizontal padding around the strings, so we need to take
            // that into account when measuring strings
            int paddingAdjustment = 6;

            // Cache the font
            Font f = this.Font;

            foreach (CharacterRange range in this.Filter.FindAllMatchedRanges(txt)) {
                // Measure the text that comes before our substring
                Size precedingTextSize = Size.Empty;
                if (range.First > 0) {
                    string precedingText = txt.Substring(0, range.First);
                    precedingTextSize = TextRenderer.MeasureText(g, precedingText, f, r.Size, flags);
                    precedingTextSize.Width -= paddingAdjustment;
                }

                // Measure the length of our substring (may be different each time due to case differences)
                string highlightText = txt.Substring(range.First, range.Length);
                Size textToHighlightSize = TextRenderer.MeasureText(g, highlightText, f, r.Size, flags);
                textToHighlightSize.Width -= paddingAdjustment;

                float textToHighlightLeft = r.X + precedingTextSize.Width + 1;
                float textToHighlightTop = this.AlignVertically(r, textToHighlightSize.Height);

                // Draw a filled frame around our substring
                this.DrawSubstringFrame(g, textToHighlightLeft, textToHighlightTop, textToHighlightSize.Width, textToHighlightSize.Height);
            }
        }