C#.NET中滚动控件的最佳实践方法 [英] Best practice approach for scrollable control in C# .NET

查看:233
本文介绍了C#.NET中滚动控件的最佳实践方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计用于语法编辑的Windows窗体控件。我知道已经有很多东西了,例如 Scintilla FastColoredTextBox Actipro语法编辑器 r, Avalon Edit 等。我有设计自己的理由,所以这不是问题。

I am designing a windows forms control for Syntax Editing. I know there are already a lot out there, like Scintilla, FastColoredTextBox, Actipro Syntax Editor, Avalon Edit etc. I have reasons for designing my own, so that's not the issue.

到目前为止,我一直在设计控件的外观。

So far I have been designing the look and feel of the control. It will need to be able to control vertically, and horizontally.

我遇到的选择是:


  1. 我的控件扩展了 ScrollableControl ContainerControl

  2. 我的控件实例化了 HScrollBar VScrollBar 控件,并相应地放置它们

  3. 我的控件使用 ScrollBarRenderer 自定义绘制滚动条

  1. My control extends ScrollableControl or ContainerControl
  2. My control instantiates a HScrollBar and VScrollBar control and places them accordingly
  3. My control uses ScrollBarRenderer to custom draw the scrollbars

我不确定这些选项中的h个将是我控制的最佳实践。

I am not sure which of these options would be best practice for my control.

我尝试使用 ScrollableControl ContainerControl ,但这有一些非常不希望的结果,可能是因为控件 DisplayRectangle 正在滚动...我不想要这样。我想滚动一个自定义绘制的矩形来保存文本。

I tried using ScrollableControl and ContainerControl, but this had some very undesired results, probably because the controls DisplayRectangle was scrolling...I don't want this. I want to scroll a custom drawn rectangle which holds the text.

我尝试实例化HScrollBar和VScrollBar,但这似乎非常有问题,并且在聚焦和缩放方面效果不佳。我无法弄清楚如何正确捕获VScroll和HScroll事件。

I tried instantiating HScrollBar and VScrollBar, but this seemed very buggy, and didn't work well with focus and I couldn't work out how to properly capture VScroll and HScroll events.

我尝试使用 ScrollBarRenderer ,但这似乎很费劲,只是要实现滚动条,采用这种方法,我会仍然必须适当地捕获和处理事件。

I tried using ScrollBarRenderer, but this seems like a hell of a lot of effort just to implement a scrollbar, and with this approach, I would still have to capture and handle the events appropriately.

那么我应该使用哪种方法,或者实际上是我可能忽略了的方法?

So which of these approaches should I be using, or indeed an approach I may have overlooked?

推荐答案

ScrollBarRenderer仅用于绘制滚动条,它实际上不执行任何滚动计算或操作。

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

我个人认为HScrollBar和VScrollBar相当笨拙,无法正常工作。

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

继承从ScrollableControl(或通过Panel获得内置的Windows控件边框)可能是最简单的。您可以使用AutoScrollMinSize属性设置内表面的大小,然后使用AutoScrollPosition属性对图形对象执行TranslateTransform来处理位置以绘制控件的一部分:

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

不过请注意,语法文本编辑器与众不同野兽一个绘图控件。我建议为此使用RichTextBox控件。

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

这篇关于C#.NET中滚动控件的最佳实践方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆