zoukankan      html  css  js  c++  java
  • C# 实现PNG文件的背景透明显示,解决动态显示闪烁问题 【转】

      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事件
            }
         }
    这个简单的例子实现了用鼠标拖动窗口中矩形,利用双缓冲技术使动画过程不会产生闪烁.
  • 相关阅读:
    ubuntu搭建GitLab
    inline,inline-block,block元素的区别
    ES6入门学习
    webpack配置
    angular6之路由
    angular6之Http服务
    angular6之http请求拦截器
    Spring Cloud(6.2):搭建OAuth2 Client
    Spring Cloud(6.1):搭建OAuth2 Authorization Server
    keytool命令的使用
  • 原文地址:https://www.cnblogs.com/mazhenyu/p/8034163.html
Copyright © 2011-2022 走看看