且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WinCE 4.2(.net精简框架集)下的图形双缓存处理

更新时间:2022-09-21 20:25:15

      在上位机Windows操作平台( .net 2.0框架集)下实现图形双缓存有很多办法,每种办法在上位机都有很好的效果,但是很不幸,在WinCE4.2操作系统(.net 2.0精简框架集)下这些方法要么不支持,要么效果很差(还是闪烁的厉害)。

在上位机中,一般都有这么几种办法:

1、应用程序中使用双缓冲的最简便的方法是使用 .NET Framework 为窗体和控件提供的默认双缓冲,通过将 DoubleBuffered 属性设置为True即可。

2、通过SetStyle修改控件属性。

 public void EnableDoubleBuffering()

{

   // Set the value of the double-buffering style bits to true.
   this.SetStyle(ControlStyles.DoubleBuffer |
      ControlStyles.UserPaint |
      ControlStyles.AllPaintingInWmPaint,
      true);
   this.UpdateStyles();

}

3、比较经典的一种如下:重载OnPaint函数

protected override void OnPaint(PaintEventArgs e)
  {

   System.Drawing.Bitmap b = new Bitmap(this.Width,this.Height);//
双缓冲技术,先将所有要画的画在该图片中,再调用GS画出该图片
.
   Graphics g = Graphics.FromImage((System.Drawing.Image)b);
   //
在这里画你所需要画的

   //-------------------
   e.Graphics.DrawImage((System.Drawing.Image)b,0,0);
   g.Dispose();

   base.OnPaint (e);   
  }

     前两种办法在WinCE下都不支持,第三种办法是可以使用的,但效果不佳,还是有明显的闪烁现象。经过上网查资料和测试,发现在第三种办法基础上要进行如下处理,效果才非常完美。

   由于我是在Panel上绘的图,所以我派生了一个类EmbedPanel,重载了OnPaintBackground函数,并且置该函数为空。

    public class EmbedPanel : Panel
    {
        protected override void OnPaintBackground(PaintEventArgs paintg)
        {           
            //不绘制背景
        }
    } 

    private void pb_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                pb_Graphics.FillRectangle(new SolidBrush(BackColor), Rect);  //绘制背景
                //--------------------------------------------------------

                ... ... 具体的绘图代码

                //--------------------------------------------------------
                e.Graphics.DrawImage((System.Drawing.Image)pb_Bitmap, 0, 0);
            }
            catch (Exception err)
            {
                ShowInfo(3000, 2, err.Message, "pb_Paint");
            }
        }

    //注:在WINCE测试时发现,在OnPaint中的写System.Drawing.Bitmap b = new Bitmap(this.Width,this.Height)代码,程序运行不长时间便会导致WinCE死机。所以我是在类的初始化中执行该代码的。

     这样一来,就很完美的在.net精简框架集下解决了绘图闪烁问题。