DoubleBuffered与SetStyle

时间:2016-06-05 11:16:41

标签: c# winforms double-buffering

我的Google搜索结果相互矛盾,我想知道是否可以澄清这一点吗?

我有:

typeof(TableLayoutPanel)
   .GetProperty("DoubleBuffered",
      System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
    .SetValue(tableLayoutPanel, true, null);

typeof(TableLayoutPanel)
    .GetMethod("SetStyle",
      System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
      .Invoke(tableLayoutPanel, new object[] { ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true });

但有人告诉我,如果我将 DoubleBuffered 设置为 true ,我不需要手动设置3种样式,因为系统会在内部执行此操作。目前我正打电话给他们。

1 个答案:

答案 0 :(得分:2)

Reference Source这里DoubleBuffered属性的实现:

protected virtual bool DoubleBuffered {
    get {
        return GetStyle(ControlStyles.OptimizedDoubleBuffer);
    }
    set {
        if (value != DoubleBuffered) {
            if (value) {
                SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, value);
            }
            else {
                SetStyle(ControlStyles.OptimizedDoubleBuffer, value);
            }
        }
    }
}

(请注意,该属性是继承的,因此您必须返回Control类才能找到它。)