C# 实现PNG文件的背景透明显示,解决动态显示闪烁问题 【转】

 以下内容,对于想要使用C#实现PNG图片背景透明显示,同时动态显示时无闪烁问题的人来说,是非常有帮助的。网络上很难找到完整的解决方案。以下是我搜集到,并加以验证过的完整解决方案。
文章一:
《How to Use Transparent Images and Labels in WindowsForms
《在Windows Forms 中怎样使用透明图片和透明标签
       这篇文章,提供了C#例程,讲解非常清楚,代码非常好。
 
文章二:
  《C#画图解决闪烁问题 》之《使用 GDI+ 双缓冲 解决绘图闪烁问题
 
以下是文章部分内容:
使用 GDI+ 双缓冲 解决绘图闪烁问题
现在的问题是很多人不知道怎么怎么使用GDI+ 双缓冲
 
public partial class Form1 : Form
    {
       //记录矩形位置的变量
       Point p = Point .Empty ;
       Point location = new Point(0, 0);
       int x = 0;
       int y = 0;
 
       public Form1()
       {
          InitializeComponent();
          //采用双缓冲技术的控件必需的设置
          this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
          this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
          this.SetStyle(ControlStyles.UserPaint, true);
       }
       protected override void OnPaint(PaintEventArgse)
       {
          base.OnPaint(e);
           Graphics g= e.Graphics;
          g.FillRectangle(Brushes.Black, x, y, 200, 200);
       }
       private void Form1_MouseDown(object sender,MouseEventArgs e)
       {
           if(e.Button == MouseButtons.Right) return;
           p =e.Location;
       }
       private void Form1_MouseUp(object sender,MouseEventArgs e)
       {
           if(e.Button == MouseButtons.Right) return;
           location.X+= e.X - p.X;
           location.Y+= e.Y - p.Y;
           p =Point.Empty;
       }
       private void Form1_MouseMove(object sender,MouseEventArgs e)
       {
        if (p == Point.Empty)return;
           x = e.X -p.X + location.X;
           y = e.Y -p.Y + location.Y;
          this.Invalidate(true);//触发Paint事件
       }
    }
这个简单的例子实现了用鼠标拖动窗口中矩形,利用双缓冲技术使动画过程不会产生闪烁.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值