我想将限制滚动设置为可滚动面板 [英] I want to set limit scrolling to a scrollable panel

查看:26
本文介绍了我想将限制滚动设置为可滚动面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个这样的可滚动面板:

I made a scrollable panel like this:

private void button3_Click(object sender, EventArgs e)
{
    Form f2 = new Form();
    f2.Size = new Size(400, 300);
    f2.AutoScroll = false;
    Panel pan = new Panel();
    pan.Size = new Size(600, 100);
    pan.AutoScroll = false;
    for (int i = 1; i <= 10; i++)
    {
        Button b = new Button();
        b.Text = "B" + (i);
        b.Name = "button_" + (i);
        b.Left = (b.Width + 12) * (i - 1);
        b.Parent = pan;
        pan.Parent = f2;
        f2.Show();
    }
}

private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
        Form2 frm = new Form2();
        panel1.Top += e.Delta > 0 ? 10 : -10;
        if (panel1.Top > 0) 
            panel1.Top = 0;
        else if (panel1.Top <= panel1.Parent.Height) 
            panel1.Top = panel1.Parent.Height;
        Console.WriteLine("panel2.top:" + panel1.Top);

    }

这是那个面板的完整代码,panel1 = pan...

This is the full code of that panel, panel1 = pan...

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    pPt = e.Location;
}
public void panel1_MouseMove(object sender, MouseEventArgs e)
{
    Console.WriteLine("panel2.top:" + panel1.Top);
    if (e.Button.HasFlag(MouseButtons.Left))
    {
        Form2 frm = new Form2();
        panel1.Top += e.Y - pPt.Y;
        if (panel1.Top > 0) 
            panel1.Top = 0;
        else if (panel1.Top <= panel1.Parent.Height) 
            panel1.Top = panel1.Parent.Height;
    }
}

您可以通过用鼠标拖动面板来滚动它,但问题是它看起来像这样:

And you can scroll it by dragging the panel with the mouse but the problem is it looks like this:

而且我不想高于按钮 1 或低于最后一个按钮.

And I want to don't go higher than button1 or lower than the last button.

推荐答案

调整此方法:您需要固定"面板,使其不会在顶部下方和底部上方移动 - 因为鼠标滚轮增量是您的事件会不断收到.您必须手动决定何时忽略它们

Tweak this method: you need to "pin" the panel so it doesn't move below the top and above the bottom - because mouse wheel deltas are events you will continuously receive. You have to manually decide when to ignore them

private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
        Form2 frm = new Form2();
        panel1.Top += e.Delta > 0 ? 10 : -10;

        // tweak this
        if (panel1.Top > 0) panel1.Top = 0;
        else if (panel1.Bottom <= panel1.Parent.Height) panel1.Bottom = panel1.Parent.Height;

        Console.WriteLine("panel2.top:" + panel1.Top);

}

此外,当您滚动的面板比视口(表单本身)高"时,上述内容也将起作用.当面板小于表单时,您可能需要进一步调整 - 所以只需测试几个案例.

Also, the above will work when the panel you are scrolling is "Taller" than the viewport (the form itself). You might need to tweak further when the panel is smaller than the form - so just test a few cases.

您还需要注意 Resize 事件,以便在有人展开容器表单时您的面板具有正确的 Top 属性.

You also need to pay attention to the Resize event so your panel has the correct Top property when someone expands the container form.

这篇关于我想将限制滚动设置为可滚动面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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