I can't get the scroll to work in my custom control. Currently, he draws his own content (currently only a grid) and should be considered as having a large area for work. However, I cannot get the scroll to work.
With AutoScroll enabled, I cannot start it in the center of the workspace.
With AutoScroll turned off, I can't get the scroll values ββto set whether I install their code or actually scroll.
To top it all off, when using separate HScroll and VScroll controls, no scrollbars are displayed at all.
The current state that the code is trying to disable AutoScroll, but Iβm at a point where I admit that I canβt make me do what I want without help.
So please help!
Here is the code:
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() { } }
source share