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事件
        }
     }
這個簡單的例子實現了用鼠標拖動窗口中矩形,利用雙緩沖技術使動畫過程不會產生閃爍.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM