Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

C# RichTextBox Highlight Line

I have uploaded a image of what i want to achive... enter image description here

So as you can see i want to highlight the line i click on [And update it on _textchanged event! Is there any possible way of doing this in any colour... doesnt have to be yellow. I have searched alot but i cant understand how to get the starting length and end length and all of that.

It has confused me alot and i don;'t understand and would require some help. Thanks for all help that is given in this thread. Also it is windows form. Im making a notepad app like notepad++ or some other notepad app... .NET Windows Form C# RichTextBox

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You'll need to create your own control that inherits from RichTextBox and use that control on your form. Since the RichTextBox does not support owner drawing, you will have to listen for the WM_PAINT message and then do your work in there. Here is an example that works fairly well, though the line height is hard coded right now:

 public class HighlightableRTB : RichTextBox
 {
     // You should probably find a way to calculate this, as each line could have a different height.
     private int LineHeight = 15; 
     public HighlightableRTB()
     {
         HighlightColor = Color.Yellow;
     }

    [Category("Custom"),
    Description("Specifies the highlight color.")]
     public Color HighlightColor { get; set; }

     protected override void OnSelectionChanged(EventArgs e)
     {
         base.OnSelectionChanged(e);
         this.Invalidate();
     }

     private const int WM_PAINT = 15;

     protected override void WndProc(ref Message m)
     {
         if (m.Msg == WM_PAINT)
         {
             var selectLength = this.SelectionLength;
             var selectStart = this.SelectionStart;

             this.Invalidate();
             base.WndProc(ref m);

             if (selectLength > 0) return;   // Hides the highlight if the user is selecting something

             using (Graphics g = Graphics.FromHwnd(this.Handle))
             {
                 Brush b = new SolidBrush(Color.FromArgb(50, HighlightColor));
                 var line = this.GetLineFromCharIndex(selectStart);
                 var loc = this.GetPositionFromCharIndex(this.GetFirstCharIndexFromLine(line));

                 g.FillRectangle(b, new Rectangle(loc, new Size(this.Width, LineHeight)));
             }
         }
         else
         {
             base.WndProc(ref m);
         }
     }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...