private void listView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { Brush brush; Color backColor; Color foreColor; if (e.Item.Selected) { if (listView.Focused) { brush = SystemBrushes.Highlight; backColor = SystemColors.Highlight; foreColor = SystemColors.HighlightText; } else { // We use the same colors here as TreeViewMS does for // drawing unfocused selected rows. brush = Brushes.LightGray; backColor = Color.LightGray; foreColor = SystemColors.WindowText; } } else { brush = SystemBrushes.Window; backColor = SystemColors.Window; foreColor = SystemColors.WindowText; } if (e.ColumnIndex == 0) { // Erase the entire background when drawing the first sub-item e.Graphics.FillRectangle(brush, e.Item.Bounds); } if (e.ColumnIndex == colHdrDisplayText.Index) { var findResult = (FindResult) e.Item.Tag; var textRendererHelper = new TextRendererHelper { ForeColor = foreColor, BackColor = backColor, Font = e.Item.Font, HighlightFont = new Font(e.Item.Font, FontStyle.Bold), }; textRendererHelper.DrawHighlightedText(e.Graphics, e.SubItem.Bounds, findResult.FindMatch); } else { e.SubItem.ForeColor = foreColor; e.DrawText(); } if (e.ColumnIndex == 0) { e.DrawFocusRectangle(e.Bounds); } }
private void DrawWithHighlighting(String description, String textToHighlight, Graphics graphics, Rectangle descriptionBounds, Color textColor, Color backColor) { var findMatch = new FindMatch(description); int ichHighlightBegin = description.ToLower().IndexOf(textToHighlight.ToLower(), StringComparison.Ordinal); // A very short search only matches at the front of a word if ((textToHighlight.Length < ProteinMatchQuery.MIN_FREE_SEARCH_LENGTH) && (ichHighlightBegin > 0)) { // Insist on a leading space for match ichHighlightBegin = description.ToLower().IndexOf(" "+textToHighlight.ToLower(), StringComparison.Ordinal); // Not L10N if (ichHighlightBegin > 0) ichHighlightBegin++; // Don't really want to highlight the space } if (ichHighlightBegin >= 0) { findMatch = findMatch.ChangeRange(ichHighlightBegin, ichHighlightBegin + textToHighlight.Length); } else { findMatch = findMatch.ChangeRange(0,0); // No highlighting } var textRendererHelper = new TextRendererHelper { Font = ListView.Font, HighlightFont = new Font(ListView.Font, FontStyle.Bold), ForeColor = textColor, BackColor = backColor, }; textRendererHelper.DrawHighlightedText(graphics, descriptionBounds, findMatch); }