如果形状的高度大于窗体的高度,则显示滚动条 [英] Show Scrollbars if the height of a shape is greater than the Form's height

查看:156
本文介绍了如果形状的高度大于窗体的高度,则显示滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just need to show scrollbars on forms if my shape height is greater than the form height. That way, when the user scrolls down, it can show the end of the shape.

这是我的代码:

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
    }
}

推荐答案

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

You need to enable the AutoScroll property of the form, use the AutoScrollPosition coordinates in drawings, and set the AutoScrollMinSize property to contain your shapes:

在构造函数中添加:

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);
    }
}

祝你好运.

这篇关于如果形状的高度大于窗体的高度,则显示滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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