自定义控制和滚动,帮助?

时间:2010-02-21 18:28:29

标签: c# winforms scroll scrollbar

我无法在自定义控件中使用滚动功能。目前它绘制了自己的内容(目前只是网格),应该被视为有大面积的工作。但是,我无法滚动工作。

启用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()
    {

    }
}

1 个答案:

答案 0 :(得分:3)

是的,这看起来很麻烦。从Panel中派生你的班级。将其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);
  }