gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:59:20 25 4
gpt4 key购买 nike

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

这是我的代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com