2010-02-21 90 views
4

我無法在自定義控件中進行滾動操作。目前它繪製自己的內容(目前只是網格),應該被視爲有大面積工作。但是,我無法滾動工作。自定義控制和滾動,幫助?

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

    } 
} 

回答

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