How can I extract some part of multiline text using GDI?

I use Graphics.DrawStringusercontrol to draw text as follows:

protected override void OnPaint(PaintEventArgs e)
{
    RectangleF bounds = DisplayRectangle;
    bounds.Inflate(-4, -4); // Padding
    StringFormat format = new StringFormat();
    format.Alignment = StringAlignment.Near;
    format.LineAlignment = StringAlignment.Near;
    format.Trimming = StringTrimming.None;
    using (Brush bFore = new SolidBrush(ForeColor))
    {
        g.DrawString(Text, Font, bFore, bounds, format);
    }
}

If the control is Textwider than DisplayRectangle, DrawStringit beautifully breaks Textinto several lines at the boundaries of words.

Now I want to emphasize some words from Text, but I could not solve it. I tried to split Text, then the MeasureStringline just before the underlined part begins, the DrawStringnormal part, then the DrawStringunderlined part. But this only works if it Textis single-line.

, LinkLabel RichTextBox , , . ?

+3
1

, , , , , ( ). , , , . , , .

Dim fntNormal As New Font(myFontFamily, myFontSize, FontStyle.Regular, GraphicsUnit.Pixel)
  Dim fntUnderline As New Font(myFontFamily, myFontSize, FontStyle.Underline, GraphicsUnit.Pixel)

  g.DrawString("This is ", fntNormal, Brushes.Black, rctTextArea)
  w1 = g.MeasureString("This is ", fntNormal).Width
  w2 = g.MeasureString("underlined", fntUnderline).Width
  If w1 + w2 > rctTextArea.Width Then
     yPos = rctTextArea.Y + g.MeasureString("This is ", fntNormal).Height + 5
     xPos = rctTextArea.X
  Else
     yPos = rctTextArea.Y
     xPos = 0
  End If

  g.DrawString("underlined", fntUnderline, Brushes.Black, xPos, yPos)

  w1 = g.MeasureString("underlined", fntUnderline).Width
  w2 = g.MeasureString(", and this is not.", fntNormal).Width

  If w1 + w2 > rctTextArea.Width Then
     yPos += g.MeasureString("underlined", fntUnderline).Height + 5
     xPos = rctTextArea.X
  Else
     xPos = 0
  End If


  g.DrawString(", and this is not.", fntNormal, Brushes.Black, xPos, yPos)

, .

.

VB, , #.

+3

Source: https://habr.com/ru/post/1721506/


All Articles