c# - 如果形状的高度大于表单的高度,则显示滚动条

标签 c# graphics scrollbar shapes paintevent

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

这是我的代码:

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包含形状的属性:

在构造函数中添加:

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

祝你好运。

关于c# - 如果形状的高度大于表单的高度,则显示滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58931515/

相关文章:

c# - WCF 服务代理未设置 "FieldSpecified"属性

graphics - 在 Mathematica 中绘制钟面(寻找更好的解决方案)

css - 强制水平滚动条到首页

android - 在 Android 中从 RectF 转换为 Rect 的最佳方法是什么?

android - 可扩展的 ListView 在 android 操作系统中非常慢

c# - 隐藏 webBrowser 滚动条 wpf

c# - 数组实例化错误

c# - 有没有一种方法可以搜索用户从文件中加载的文本中的特定单词并为其着色?

c# - WCF SSL 负载均衡器。负载均衡器更改 ssl 端口

java - 在 Java 图形中使用缓冲区