gpt4 book ai didi

c# - 自定义控件和滚动,帮助?

转载 作者:行者123 更新时间:2023-11-30 12:18:22 25 4
gpt4 key购买 nike

我无法在我的自定义控件中进行滚动。目前它绘制自己的内容(目前只是网格),应该被视为有一个很大的工作区域。但是,我无法滚动工作。

启用 AutoScroll 后,我无法让它从工作区域的中心开始。

关闭 AutoScroll 后,无论我在代码中设置它们还是您实际滚动,我都无法让滚动条的值保持不变。

最重要的是,当使用单独的 HScroll 和 VScroll 控件时,根本不会显示滚动条。

代码的当前状态是在关闭 AutoScroll 的情况下尝试,但我承认我无法在没有帮助的情况下让它做我想做的事情。

所以请帮忙!

这是目前的代码:

public partial class TwoDimensionViewport : ScrollableControl
{
private readonly Point MinOffset = new Point(-4000000, -4000000);
private readonly Point MaxOffset = new Point(4000000, 4000000);

private Point viewOffset;

public TwoDimensionViewport()
{
InitializeComponent();
DoubleBuffered = true;
GridSize = 16;
AutoScroll = false;
AdjustFormScrollbars(true);
}

protected override void AdjustFormScrollbars(bool displayScrollbars)
{
VerticalScroll.Minimum = 0;
VerticalScroll.Maximum = 8000000;
HorizontalScroll.Minimum = 0;
HorizontalScroll.Maximum = 8000000;
HorizontalScroll.Enabled = true;
VerticalScroll.Enabled = true;
HorizontalScroll.Visible = true;
VerticalScroll.Visible = true;

HorizontalScroll.Value = viewOffset.X + 4000000;
VerticalScroll.Value = viewOffset.Y + 4000000;
}

private ViewSettingsProvider viewSettingsProvider;
public ViewSettingsProvider ViewSettingsProvider
{
get
{
return viewSettingsProvider;
}
set
{
UnbindViewSettingsProvider();
viewSettingsProvider = value;
BindViewSettingsProvider();
}
}

public int GridSize { get; private set; }

protected override void OnScroll(ScrollEventArgs se)
{
base.OnScroll(se);

if (se.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
viewOffset.X = se.NewValue - 4000000;
}
else if (se.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
viewOffset.Y = se.NewValue - 4000000;
}

Invalidate();
}

protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.FillRectangle(Brushes.Black, pe.ClipRectangle);

DrawGrid(pe.Graphics);

base.OnPaint(pe);
}

private void DrawGrid(Graphics graphics)
{
for (int i = 0, count = 0; i < ClientRectangle.Width; i++)
{
bool increaseCount = false;

if ((i - viewOffset.X) % GridSize == 0)
{
graphics.DrawLine(
count % 5 == 0 ? Pens.White : Pens.DarkGray,
i, 0,
i, ClientRectangle.Height);

increaseCount = true;
}

if ((i - viewOffset.Y) % GridSize == 0)
{
graphics.DrawLine(
count % 5 == 0 ? Pens.White : Pens.DarkGray,
0, i,
ClientRectangle.Width, i);

increaseCount = true;
}

if (increaseCount)
count++;
}
}

private void BindViewSettingsProvider()
{

}

private void UnbindViewSettingsProvider()
{

}
}

最佳答案

是的,这看起来很麻烦。改为从 Panel 派生您的类(class)。将其 AutoScroll 设置为 true,将 AutoMinSize 属性设置为可滚动网格的大小。这会自动使滚动条出现。使用 OnPaintBackground() 方法绘制背景和网格。您需要通过 AutoScrollPosition 属性来偏移网格的绘制:

  protected override void OnPaintBackground(PaintEventArgs e) {
e.Graphics.FillRectangle(Brushes.Black, this.ClientRectangle);
e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
for (int x = 0; x < this.AutoScrollMinSize.Width; x += GridSize)
e.Graphics.DrawLine(Pens.White, x, 0, x, this.AutoScrollMinSize.Height);
for (int y = 0; y < this.AutoScrollMinSize.Height; y += GridSize)
e.Graphics.DrawLine(Pens.White, 0, y, this.AutoScrollMinSize.Width, y);
}

关于c# - 自定义控件和滚动,帮助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2306902/

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