在面板上自动垂直滚动RichTextBox滚动c#

本文关键字:滚动 垂直 RichTextBox | 更新日期: 2023-09-27 18:27:49

我在WinForm的面板内有一个RichTextBox。我想隐藏RichTextBox的垂直滚动条,并将其滚动与容器面板的垂直滚动栏同步;每当文本在textbox中溢出时,面板的滚动条应显示,每当我滚动面板的滚动栏时,textbox应滚动。如何做到这一点?

在面板上自动垂直滚动RichTextBox滚动c#

正如我在评论中所说,我们必须处理win32消息并使用一些破解。我已经用了我在winforms中关于win32消息和控制破解/自定义的所有知识为您制作了这个演示。它并不完整,当然也不会像RichTextBox的标准滚动条那样完美。不足之处在于,如果你一直按住箭头键,滚动条拇指将不会向右移动,但如果你正常按下箭头键,则滚动条拇指会像标准滚动条一样将插入符号移动到视图中

public class Form1 : Form {
   [DllImport("user32")]
   private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
   public Form1(){
      InitializeComponent();
      //initialize some properties for your richTextBox1 (this should be added as a child of your panel1)
      richTextBox1.ScrollBars = RichTextBoxScrollBars.Horizontal;
      richTextBox1.BorderStyle = BorderStyle.None;
      richTextBox1.Dock = DockStyle.Top;
      richTextBox1.MinimumSize = new Size(panel1.Width, panel1.Height - 2);
      //initialize some properties for your panel1
      panel1.AutoScroll = true;
      panel1.BorderStyle = BorderStyle.FixedSingle;       
      //If the size of panel1 is changed, we have to update the MinimumSize of richTextBox1.
      panel1.SizeChanged += (s,e) => {
         richTextBox1.MinimumSize = new Size(panel1.Width, panel1.Height - 2);
      };   
      new NativeRichTextBox() { Parent = panel1 }.AssignHandle(richTextBox1.Handle);
      hidden.Parent = panel1;    
   }
   //hidden control of panel1 is used to scroll the thumb when the KeyUp of richTextBox1 is raised.
   Control hidden = new Control();
   //this is used to hook into the message loop of the richTextBox1
   public class NativeRichTextBox : NativeWindow
    {
        public Panel Parent;
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x20a)//WM_MOUSEWHEEL = 0x20a
            {                    
                if (Parent != null)
                {
                    SendMessage(Parent.Handle, m.Msg, m.WParam, m.LParam);                     
                    return;
                }
            }
            base.WndProc(ref m);                
        }
    }
   //ContentsResized event handler of your richTextBox1
   private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)
    {
        richTextBox1.Height = e.NewRectangle.Height + 5;            
    }
   //KeyUp event handler of your richTextBox1
   private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
    {
            Point p = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);                                
            hidden.Top = panel1.PointToClient(richTextBox1.PointToScreen(p)).Y;
            hidden.Height = (int) richTextBox1.SelectionFont.Height;
            panel1.ScrollControlIntoView(hidden);                
    }
}

注意:您必须使用代码或通过设计器为richTextBox1注册事件处理程序ContentsResizedKeyUp

为了隐藏滚动条,您可以执行

richTextBox1.ScrollBars = RichTextBoxScrollBars.None;

但它的问题在于它会使文本扭曲。所以你也需要

richTextBox1.WordWrap = false;

一旦你做到了,剩下的就不那么容易了。

在面板滚动事件上注册并更改富格文本框上的滚动。问题是,您不能只更改richTextBox的滚动偏移,所以您可以使用Select方法跳到您需要的位置。你可以用这条线的长度来知道scrollBar需要多大。

最后,这将是一项艰苦的工作。祝好运