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