博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 实现PNG文件的背景透明显示,解决动态显示闪烁问题 【转】
阅读量:7171 次
发布时间:2019-06-29

本文共 1405 字,大约阅读时间需要 4 分钟。

  http://blog.sina.com.cn/s/blog_402c071e0102x4rl.html

   以下内容,对于想要使用C#实现PNG图片背景透明显示,同时动态显示时无闪烁问题的人来说,是非常有帮助的。网络上很难找到完整的解决方案。以下是我搜集到,并加以验证过的完整解决方案。
文章一:
《How to Use Transparent Images and Labels in Windows Forms
《在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(PaintEventArgs e)
        {
            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事件
        }
     }
这个简单的例子实现了用鼠标拖动窗口中矩形,利用双缓冲技术使动画过程不会产生闪烁.

转载地址:http://wmbzm.baihongyu.com/

你可能感兴趣的文章
POJ2195 Going Home 【最小费用流】+【最佳匹配图二部】
查看>>
Swift - 给表格UITableView添加索引功能(快速定位)
查看>>
fixed的left:50%,漂浮
查看>>
浅谈JAVA集合框架
查看>>
iOS_8_键盘操作简单
查看>>
Android数据缓存(转)
查看>>
用 windows GDI 实现软光栅化渲染器--gdi3d(开源)
查看>>
Jenkins快速上手
查看>>
Android 工程目录结构简介
查看>>
HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
查看>>
ubuntu解决arm-linux-gcc no such file的问题
查看>>
Activity设置singleTask无法通过Intent获取值的问题
查看>>
python之模块copy,了解概念即可
查看>>
还是回文(dp)
查看>>
oracle创建表
查看>>
解决 Eclipse build workspace 慢,validation javascript 更慢的问题
查看>>
jquery ajax验证用户名是否存在(后台spring mvc)
查看>>
WPF控件--利用Winform库中的NotifyIcon实现托盘小程序
查看>>
动物统计加强版(贪心,字典序)
查看>>
LeetCode - 15. 3Sum
查看>>