C#RichTextBox的高亮线 [英] C# RichTextBox Highlight Line

查看:136
本文介绍了C#RichTextBox的高亮线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经上传了什么,我想才达到...

因此​​,大家可以看到,我想强调这一行我点击[和更新上_textchanged活动!
是否有任何颜色这样做的任何可能的方式...不必须是黄色的。我已经搜索了很多,但我无法理解如何获得首发长度和长度结束和所有的。

据很多困惑我,我不要;T理解和需要一些帮助。
感谢您的是在这个线程提供的所有帮助。此外,它是Windows窗体。林作出记事本程序像记事本++或其他一些记事本程序... .NET Windows窗体C#RichTextBox的


解决方案

您需要创建表单上的控制自己的控制,从RichTextBox的继承和使用。由于RichTextBox的不支持业主绘图,你将不得不监听WM_PAINT消息,然后做你的工作在那里。这里是工作得相当好例子,但该行的高度是很难codeD现在:

 公共类HighlightableRTB:的RichTextBox
 {
     //你或许应该找到一种方法来计算这一点,因为每行可以有不同的高度。
     私人INT lineHeight是= 15;
     公共HighlightableRTB()
     {
         HighlightColor = Color.Yellow;
     }    [类别(自定义),
    说明(指定高亮颜色。)
     众彩HighlightColor {搞定;组; }     保护覆盖无效OnSelectionChanged(EventArgs的发送)
     {
         base.OnSelectionChanged(E);
         this.Invalidate();
     }     私人const int的WM_PAINT = 15;     保护覆盖无效的WndProc(参考消息M)
     {
         如果(m.Msg == WM_PAINT)
         {
             VAR selectLength = this.SelectionLength;
             VAR选择开始= this.SelectionStart;             this.Invalidate();
             base.WndProc(REF米);             如果(selectLength大于0)回报; //隐藏高亮如果用户选择的东西             使用(图形G = Graphics.FromHwnd(this.Handle))
             {
                 刷B =新SolidBrush(Color.FromArgb(50 HighlightColor));
                 VAR线= this.GetLineFromCharIndex(选择开始);
                 变种LOC = this.GetPositionFromCharIndex(this.GetFirstCharIndexFromLine(线));                 g.FillRectangle(二,新的Rectangle(LOC,新的大小(this.Width,lineHeight是)));
             }
         }
         其他
         {
             base.WndProc(REF米);
         }
     }
 }

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

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

解决方案

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);
         }
     }
 }

这篇关于C#RichTextBox的高亮线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆