private Graphics objTargetGraphics = null;
private BufferedGraphics objBufGraphics = null;
private void InitDoubleBuffer()
{
    if (objBufGraphics != null)
    {
        objBufGraphics.Dispose();
        objBufGraphics = null;
    }
 
    if (objTargetGraphics != null)
    {
        objTargetGraphics.Dispose();
        objTargetGraphics = null;
    }
 
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
    this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
 
    BufferedGraphicsManager.Current.MaximumBuffer =
        new Size(this.Width + 1, this.Height + 1);
 
    objTargetGraphics = this.CreateGraphics();
 
    try
    {
        objBufGraphics = BufferedGraphicsManager.Current.Allocate(
            objTargetGraphics,
            new Rectangle(0, 0, this.Width, this.Height));
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Exception StackTrace:\r\n" + ex.StackTrace);
    }
}
 
private void FormTest_Paint(object sender, PaintEventArgs e)
{
    objBufGraphics.Graphics.FillRectangle(Brushes.White,this.ClientRectangle);
    objBufGraphics.Graphics.DrawString(
        DateTime.Now.ToString()+"."+DateTime.Now.Millisecond.ToString(),
        SystemFonts.DialogFont, Brushes.Blue, new Point(70, 70));
 
    objBufGraphics.Render(e.Graphics);
}

用GDI+显示的曲线图: 


本文链接地址: (.Net)GDI+ 双缓冲的常用方法
https://blog.qingfengju.com/index.asp?id=300

分类:Win32/C++ 查看次数:7053 发布时间:2012/10/1 10:39:57