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

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

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

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

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

 

 
 
  1. public void EnableDoubleBuffering()  
  2.  
  3. {  
  4.  
  5.    // Set the value of the double-buffering style bits to true.  
  6.    this.SetStyle(ControlStyles.DoubleBuffer |   
  7.       ControlStyles.UserPaint |   
  8.       ControlStyles.AllPaintingInWmPaint,  
  9.       true);  
  10.    this.UpdateStyles();  
  11. }  
  12.  

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


 

 
 
  1. protected override void OnPaint(PaintEventArgs e)  
  2.   {  
  3.  
  4.    System.Drawing.Bitmap b = new Bitmap(this.Width,this.Height);//双缓冲技术,先将所有要画的画在该图片中,再调用GS画出该图片.  
  5.    Graphics g = Graphics.FromImage((System.Drawing.Image)b);  
  6.    //在这里画你所需要画的  
  7.  
  8.    //-------------------  
  9.    e.Graphics.DrawImage((System.Drawing.Image)b,0,0);  
  10.    g.Dispose();  
  11.  
  12.    base.OnPaint (e);     
  13.   }  
  14.  

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

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


   

 
 
  1. public class EmbedPanel : Panel  
  2.    {  
  3.        protected override void OnPaintBackground(PaintEventArgs paintg)  
  4.        {              
  5.            //不绘制背景  
  6.        }  
  7.    }    
  8.  
  9.    private void pb_Paint(object sender, PaintEventArgs e)  
  10.        {  
  11.            try 
  12.            {  
  13.                pb_Graphics.FillRectangle(new SolidBrush(BackColor), Rect);  //绘制背景  
  14.                //--------------------------------------------------------  
  15.  
  16.                ... ... 具体的绘图代码  
  17.  
  18.                //--------------------------------------------------------  
  19.                e.Graphics.DrawImage((System.Drawing.Image)pb_Bitmap, 0, 0);  
  20.            }  
  21.            catch (Exception err)  
  22.            {  
  23.                ShowInfo(3000, 2, err.Message, "pb_Paint");  
  24.            }  
  25.        }  

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

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

 



















本文转自yefanqiu51CTO博客,原文链接:http://blog.51cto.com/yfsoft/323439,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值