Выделить текст переноса в winformsC#

Место общения программистов C#
Ответить
Anonymous
 Выделить текст переноса в winforms

Сообщение Anonymous »


I am trying to achieve highlighting a wrapped text in the given bounds. I can achieve the expected behavior. If the search text is in single line and i am facing issue if the search text is rendered in two lines (wrapped) like below,


Изображение


I have used character range and MeasureCharacterRanges method to find the regions of the text area and exlcuded the text regions that doesn't match the given search text, but i couldn't find the region of the empty area that doesn't have text rendered, so empty area also highlighted along with given search text.

StringFormat stringFormat = new StringFormat(StringFormat.GenericDefault); Font font = new Font("Segoe UI", 9F, FontStyle.Regular); var searchText = string.IsNullOrEmpty(SearchText) ? string.Empty : SearchText; int matchIndex = Text.IndexOf(searchText); if (matchIndex != -1 && searchText.Length > 0) { CharacterRange[] characterRange = new CharacterRange[] { new CharacterRange(0, matchIndex), new CharacterRange(matchIndex, searchText.Length), new CharacterRange(matchIndex + searchText.Length , Text.Length - matchIndex - searchText.Length), }; //Set the range of characters to be measured. stringFormat.SetMeasurableCharacterRanges(characterRange); //Gets the regions of the measurable characters applied to the string format for the given text with in the text bounds. Region[] regions = e.Graphics.MeasureCharacterRanges(Text, font, e.ClipRectangle, stringFormat); var clipBounds = e.Graphics.ClipBounds; for (int i = 0; i < regions.Length; i++) { RectangleF bound = regions.GetBounds(e.Graphics); e.Graphics.SetClip(Rectangle.Ceiling(bound)); for (int j = 0; j < regions.Length; j++) { if (j != i) e.Graphics.ExcludeClip(regions[j]); } Rectangle rbound = new Rectangle((int)bound.X, (int)bound.Y, (int)bound.Width, (int)bound.Height); Brush brush = new SolidBrush(Color.Yellow); if (i == 1) e.Graphics.FillRectangle(brush, rbound); } e.Graphics.SetClip(clipBounds); } // Paints the text. e.Graphics.DrawString(Text, font, Brushes.Black, e.ClipRectangle); Above is the code used to highlight the search text.

I couldn't find a way to exclude the empty region with MeasureCharacterRanges concept. Can anyone share idea to exclude the remaining empty region?


Источник: https://stackoverflow.com/questions/650 ... n-winforms
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»