Best practice approach for scrollable control in C# .NET

我是研究僧i 提交于 2019-12-06 13:21:10

A ScrollBarRenderer is only used to draw a scrollbar, it doesn't actually perform any scroll calculations nor actions.

Personally, I find the HScrollBar and VScrollBar rather clumsy controls to work.

Inheriting from the ScrollableControl (or Panel to get a built-in windows control border) is probably the easiest. You use the AutoScrollMinSize property to set the size of the interior surface, and then perform a TranslateTransform on the graphics object using the AutoScrollPosition property to handle the "where" to draw part of the control:

public class ScrollControl : ScrollableControl {

  public ScrollControl() {
    this.DoubleBuffered = true;
    this.ResizeRedraw = true;
    this.AutoScrollMinSize = new Size(0, 600);
  }

  protected override void OnPaint(PaintEventArgs e) {
    base.OnPaint(e);

    e.Graphics.Clear(Color.White);
    e.Graphics.TranslateTransform(this.AutoScrollPosition.X, 
                                  this.AutoScrollPosition.Y);
    e.Graphics.FillRectangle(Brushes.Red, new Rectangle(16, 32, 64, 32));
  }
}

Be careful though, a syntax text editor is a different beast that a drawing control. I would advise using a RichTextBox control for that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!