[#如果形状高度大于表单高度,则C#显示滚动条

问题描述 投票:0回答:1

如果我的形状高度大于表单高度,我只需要在表单上显示滚动条。这样,当用户向下滚动时,它可以显示形状的末端。

这是我的代码:

public partial class Form1: Form {
    public Form1() {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {
    }

    private void Form1_Paint(object sender, PaintEventArgs e) {
        e.Graphics.DrawLine(new Pen(Color.Black, 2), 100, 50, 100, 1000);
        //if line height > form height then show scroll bars
    }
}
c# height scrollbar shapes paintevent
1个回答
0
投票

您需要启用表单的AutoScroll属性,在工程图中使用AutoScrollPosition坐标,并设置AutoScrollMinSize属性以包含您的形状:

在构造函数中添加:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        AutoScroll = true;
    }
}

和绘画例程:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    using (Matrix m = new Matrix(1, 0, 0, 1, AutoScrollPosition.X, AutoScrollPosition.Y))
    {
        var sY = VerticalScroll.Value;
        var sH = ClientRectangle.Y;
        var w = ClientRectangle.Width - 2 - (VerticalScroll.Visible ? SystemInformation.VerticalScrollBarWidth : 0);
        var h = ClientRectangle.Height;                
        var paintRect = new Rectangle(0, sY, w, h); //This will be your painting rectangle.
        var g = e.Graphics;

        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.Transform = m;
        g.Clear(BackColor);

        using (Pen pn = new Pen(Color.Black, 2))
            e.Graphics.DrawLine(pn, 100, 50, 100, 1000);

        sH += 1050; //Your line.y + line.height
        //Likewise, you can increase the w to show the HorizontalScroll if you need that.
        AutoScrollMinSize = new Size(w, sH);
    }
}

祝你好运。>>

© www.soinside.com 2019 - 2024. All rights reserved.