我在面板上有一些由分离器分隔的用户控件。包含面板设置为自动滚动。

由于 Splitter 控件在调整其“分割”的控件大小时会考虑其父控件的大小,因此其中的 UserControls 的大小调整受到面板大小的限制。

我希望能够在用户释放鼠标时将拆分器向下移动到鼠标所在的位置(甚至超出容器/表单的边界),并相应地调整容器面板的大小(并在必要时显示滚动条)。

我尝试过各种组合,用不同的面板包裹它,使用 MinSize 等。这是迄今为止我想到的最好的,但这不是我想要的:

alt text

有人有什么想法吗?

有帮助吗?

解决方案

你可以设置一个 鼠标钩 当按下鼠标按钮时,并在释放鼠标按钮时将其解除。在钩子回调中,您可以观察鼠标位置并根据需要调整控件的大小。

编辑:

您可以改为使用一个特殊的控件,用户可以拖动该控件以将滚动位置固定在父容器的右下角。用户可以拖动控件以使区域更大,如果您不使用锚点或停靠设置,则可以手动调整控件的大小以填充父区域。

我不久前为我所做的一个项目实现了类似的东西。我把它做成三角形,看起来类似于 ToolStrip. 。这是一些代码片段 ScrollHolder 控制:

public ScrollHolder()
{
    this.Size = new Size(21, 21);
    this.BackColor = SystemColors.Control;
}

protected override void OnPaint(PaintEventArgs e)
{
    Point bottomLeft = new Point(0, this.Height);
    Point topRight = new Point(this.Width, 0);
    Pen controlDark = SystemPens.ControlDark;
    Pen controlLightLight = SystemPens.ControlLightLight;
    Pen controlDark2Px = new Pen(SystemColors.ControlDark, 2);
    Point bottomRight = new Point(this.Width, this.Height);
    e.Graphics.DrawLine(
        controlLightLight, 
        bottomLeft.X, 
        bottomLeft.Y - 2, 
        bottomRight.X, 
        bottomRight.Y - 2);
    e.Graphics.DrawLine(controlDark, bottomLeft, topRight);
    e.Graphics.DrawLine(
        controlLightLight, 
        bottomLeft.X + 1, 
        bottomLeft.Y, 
        topRight.X, 
        topRight.Y + 1);
    e.Graphics.DrawLine(controlDark2Px, bottomLeft, bottomRight);
    e.Graphics.DrawLine(controlDark2Px, bottomRight, topRight);
    int xNumberOfGripDots = this.Width / 4;
    for (int x = 1; x < xNumberOfGripDots; x++)
    {
        for (int y = 1; y < 5 - x; y++)
        {
            DrawGripDot(e.Graphics, new Point(
                this.Width - (y * 4), this.Height - (x * 4) - 1));
        }
    }
}

private static void DrawGripDot(Graphics g, Point location)
{
    g.FillRectangle(
        SystemBrushes.ControlLightLight, location.X + 1, location.Y + 1, 2, 2);
    g.FillRectangle(SystemBrushes.ControlDark, location.X, location.Y, 2, 2);
}

protected override void OnResize(EventArgs e)
{
    this.SetRegion();
    base.OnResize(e);
}

private void SetRegion()
{
    GraphicsPath path = new GraphicsPath();
    path.AddPolygon(new Point[] 
    { 
        new Point(this.Width, 0), 
        new Point(this.Width, this.Height),
        new Point(0, this.Height) 
    });
    this.Region = new Region(path);
}

就实际行为实现而言,您可能想要:

  • 当滚动条移出可见区域时滚动到滚动条:
  • 当滚动到滚动支架时,通过调用减慢速度 Thread.Sleep 较短的时间(例如 50 毫秒)。

其他提示

您需要将 Splitter MinExtra 属性设置为较大的负数。虽然属性本身不允许这样做,但您可以通过反射来改变字段:

typeof(Splitter).GetField("minExtra", Reflection.BindingFlags.Instance | Reflection.BindingFlags.NonPublic).SetValue(mySplitter, -10000);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top