代码之家  ›  专栏  ›  技术社区  ›  SLaks

双缓冲列表框

  •  9
  • SLaks  · 技术社区  · 16 年前

    我有一个CheckedLIST(WinForms)控件(它继承自列表框;谷歌搜索显示问题出在列表框上),该控件锚定在其表单的所有四个侧面。当调整表单大小时,列表框会出现难看的闪烁。我尝试继承CheckedListView和设置 DoubleBuffered true

    WS_EX_COMPOSITED CreateParams ,这有所帮助,但会使表单的大小调整速度变慢。

    还有其他方法可以防止这种闪烁吗?

    4 回复  |  直到 16 年前
        1
  •  11
  •   D.Kastier K Yugandhar Reddy    7 年前

    我也遇到了类似的问题,尽管是用一个所有者绘制的列表框。我的解决方案是使用BufferedGraphics对象。如果你的清单不是由车主绘制的,你的里程可能会因这个解决方案而异,但也许它会给你一些灵感。

    我发现除非我提供TextFormatFlags,否则TextRenderer很难渲染到正确的位置。保存图形转换。另一种方法是使用P/Invoke调用BitBlt,直接在图形上下文之间复制像素。我选择了这两件坏事中较小的一件。

    /// <summary>
    /// This class is a double-buffered ListBox for owner drawing.
    /// The double-buffering is accomplished by creating a custom,
    /// off-screen buffer during painting.
    /// </summary>
    public sealed class DoubleBufferedListBox : ListBox
    {
        #region Method Overrides
        /// <summary>
        /// Override OnTemplateListDrawItem to supply an off-screen buffer to event
        /// handlers.
        /// </summary>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
    
            Rectangle newBounds = new Rectangle(0, 0, e.Bounds.Width, e.Bounds.Height);
            using (BufferedGraphics bufferedGraphics = currentContext.Allocate(e.Graphics, newBounds))
            {
                DrawItemEventArgs newArgs = new DrawItemEventArgs(
                    bufferedGraphics.Graphics, e.Font, newBounds, e.Index, e.State, e.ForeColor, e.BackColor);
    
                // Supply the real OnTemplateListDrawItem with the off-screen graphics context
                base.OnDrawItem(newArgs);
    
                // Wrapper around BitBlt
                GDI.CopyGraphics(e.Graphics, e.Bounds, bufferedGraphics.Graphics, new Point(0, 0));
            }
        }
        #endregion
    }
    

    GDI ).

    public static class GDI
    {
        private const UInt32 SRCCOPY = 0x00CC0020;
    
        [DllImport("gdi32.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, UInt32 dwRop);
    
        public static void CopyGraphics(Graphics g, Rectangle bounds, Graphics bufferedGraphics, Point p)
        {
            IntPtr hdc1 = g.GetHdc();
            IntPtr hdc2 = bufferedGraphics.GetHdc();
    
            BitBlt(hdc1, bounds.X, bounds.Y, 
                bounds.Width, bounds.Height, hdc2, p.X, p.Y, SRCCOPY);
    
            g.ReleaseHdc(hdc1);
            bufferedGraphics.ReleaseHdc(hdc2);
        }
    }
    
        2
  •  2
  •   EricLaw    13 年前

    您可以检查切换到带有复选框的ListView控件是否会改善问题。处理起来并不容易(但是,嘿,WinForms列表框也不是天才之举),我发现它的大小调整行为 DoubleBuffered=true 是可以忍受的。

    WM_ERASEBKND 什么都不做,然后回来 TRUE (如果你的控件覆盖了父窗体的整个客户端区域,那也没关系,否则你需要一个更复杂的背景绘制方法。

    我在Win32应用程序中成功地使用了它,但我不知道Forms控件是否添加了一些自己的魔法,使其无法正常工作。

        3
  •  0
  •   Jarett Millard    16 年前

    这过去是通过向控件发送WM_SETREDRAW消息来处理的。

    const int WM_SETREDRAW = 0x0b;
    
    Message m = Message.Create(yourlistbox.Handle, WM_SETREDRAW, (IntPtr) 0, (IntPtr) 0);
    yourform.DefWndProc(ref m);
    
    // do your updating or whatever else causes the flicker
    
    Message m = Message.Create(yourlistbox.Handle, WM_SETREDRAW, (IntPtr) 1, (IntPtr) 0);
    yourform.DefWndProc(ref m);
    

    另请参见: WM_SETREDRAW reference at Microsoft

    如果其他人在下使用过windows消息。NET,请根据需要更新此帖子。

        4
  •  0
  •   AlainD    9 年前

    虽然没有解决闪烁的具体问题,但对于此类问题,一种通常有效的方法是缓存列表框项的最小状态。然后通过对每个项目执行一些计算来确定是否需要重新绘制列表框。仅当至少有一个项目需要更新时才更新列表框(当然,还要将此新状态保存在缓存中以供下一个周期使用)。

    推荐文章