2

我想创建一个类似控件的表格,其中滚动时一些顶行保持在原位。我在 OnPaint 方法的覆盖中绘制控件。

滚动时会自动发生一些部分重绘,但这会产生伪影并且看起来很奇怪。为了解决这个问题,我调用 Invalidate(); 或刷新();在附加到滚动事件的事件处理程序中,并在绘制应该滚动进/出视图的部分时考虑滚动位置。但是,当我这样做时,应该保持原位的部分会被滚动出去并一次又一次地发回。(所以它有点闪烁)我正在努力使这项工作顺利进行而没有闪烁。

为了简化问题,我编写了以下代码,在其中绘制了一条应该在滚动时保持原位的线。如您所见,它在滚动时开始闪烁。

我已经想到了双缓冲,就像你在代码中看到的那样。

有谁知道如何使线路保持在原位?提前致谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ColumnReaderUITryOut
{
    public partial class CustomPaintScrollTest : UserControl
    {
        public CustomPaintScrollTest()
        {
            InitializeComponent();
            AutoScrollMinSize = new Size(500, 600);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            ResizeRedraw = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawLine(Pens.Black, 0, 0, Width, Height);
        }

        private void CustomPaintScrollTest_Scroll(object sender, ScrollEventArgs e)
        {
            Invalidate();
        }
    }
}
4

0 回答 0