C# 使用滚动条向下或向上控制richtextbox内的控件

C# 使用滚动条向下或向上控制richtextbox内的控件,c#,winforms,C#,Winforms,在我的richtextbox中有一个复选框,当向下或向上滚动我的richtextbox时,复选框不会滚动 CheckBox chk = new CheckBox(); chk.Name = "Chk" + i; chk.Location = new Point(80,10+i); chk.Text = "Save"; chk.Size = new System.Drawing.Size(20, 100); richTextBox1.Controls.Add(chk); i++; 你能告诉我怎么

在我的richtextbox中有一个复选框,当向下或向上滚动我的richtextbox时,复选框不会滚动

CheckBox chk = new CheckBox();
chk.Name = "Chk" + i;
chk.Location = new Point(80,10+i);
chk.Text = "Save";
chk.Size = new System.Drawing.Size(20, 100);
richTextBox1.Controls.Add(chk);
i++;

你能告诉我怎么解决吗。

RichTextBox不能包含复选框对象。您已经为检查点的位置设置了静态点

chk.Location = new Point(80,10+i)

,它位于RichTextBox之上,而不是进入该框中,您很可能已经看到了组合框或下拉列表中的复选框。 这是可能的。您可以尝试使用第三方控件,如,或编写自己的代码。这个话题已经在另一个问题上讨论过了,看一看,所以我们不会在这个问题上重新讨论同一个话题

相反,您通过代码获得的只是以图形方式将一个控件放置在另一个控件上,但它们不会相互交互。它们是两个独立的控件。

您可以处理RichTextBox的鼠标滚轮和滚动事件,并使用GetScrollPos win32函数获取RichTextBox滚动条的新位置,然后相应地更新复选框的位置。请注意,当鼠标滚轮升起时,滚动条的位置不会立即更改为新位置,而是会从当前位置平滑地更改为新位置。这就是为什么我们必须使用计时器周期性地重复调用GetScrollPos,直到返回位置相同。我们得到的效果并不完美,因为滚动条移动的平滑度很高,但它接近于这种平滑度,并且比在鼠标滚轮事件处理程序中调用GetScrollPos要好得多

然而,在这段代码中,我想使用NativeWindow钩住消息循环并获取发送到RichTextBox的消息,下面是运行正常的代码,这段代码只是一个演示,只处理垂直滚动。您可以在WM_HSCROLL和GetScrollPos上找到更多信息来处理水平滚动。这很容易,因为它与垂直滚动非常相似:


你也能分享你的代码吗!!RichTextBox不能包含复选框对象。解释你的代码应该做什么。。。。复选框chk=新复选框;chk.Name=chk+i;chk.位置=新点80,10+i;chk.Text=保存;chk.尺寸=新系统图纸尺寸20,100;richTextBox1.Controls.Addchk;i++;用这段代码编辑你的问题,因为你可以看到注释看起来很难看,而且这不是源代码的位置。首先,为什么你需要在richtextbox中设置一个复选框?它不能在外面吗?richTextBox1.Controls.Addchk;仅将其添加到richtextbox中。不是RTF格式,而是控件。你的观点是什么?我不在不同的位置创建复选框,我需要在其中向下或向上滚动richtextbox复选框。
public partial class Form1 : Form
{
    [DllImport("user32")]
    private static extern int GetScrollPos(IntPtr hwnd, int nBar);
    public Form1()
    {
        InitializeComponent();
        chk.Text = ".NET pro";
        chk.Parent = richTextBox1;
        chk.Top = 100;//Notice the initial Top to update the position properly later.            
        nativeRichText.AssignHandle(richTextBox1.Handle);
        //Scroll event handler for the nativeRichText
        nativeRichText.Scroll += (s, e) =>
        {
            chk.Top = 100-e.Y;
        };
        //TextChanged event handler for the richTextBox1
        richTextBox1.TextChanged += (s, e) =>
        {
            chk.Top = 100-GetScrollPos(richTextBox1.Handle, 1);
        };            
    }
    CheckBox chk = new CheckBox();
    NativeRichTextBox nativeRichText = new NativeRichTextBox();        
    public class NativeRichTextBox : NativeWindow
    {
        Timer t = new Timer();
        int y = -1;
        public NativeRichTextBox()
        {
            t.Interval = 30;
            t.Tick += (s, e) =>
            {                                     
                int y2 = Form1.GetScrollPos(Handle, 1);//nBar =1 => Vertical bar
                if (y2 == y) { t.Stop(); return; }
                y = y2;
                if (Scroll != null) Scroll(this, new ScrollEventArgs(0, y));                    
            };
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x115)//WM_VSCROLL = 0x115
            {
                int wp = m.WParam.ToInt32();
                int low = wp & 0x00ff;
                if (low == 4 || low == 5)//SB_THUMBPOSITION = 4   SB_THUMBTRACK = 5
                {
                    if (Scroll != null) Scroll(this, new ScrollEventArgs(0, wp >> 16));
                }
                else t.Start();
            }
            if (m.Msg == 0x20a)//WM_MOUSEWHEEL = 0x20a
            {
                y = -1;
                t.Start();
            }
            base.WndProc(ref m);                
        }
        public class ScrollEventArgs : EventArgs
        {
            public int X { get; set; }
            public int Y { get; set; }
            public ScrollEventArgs(int x, int y)
            {
                X = x;
                Y = y;
            }
        }
        public delegate void ScrollEventHandler(object sender, ScrollEventArgs e);
        public event ScrollEventHandler Scroll;
    }
}