您的位置:首页 > 编程语言 > C#

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

2017-12-13 17:45 513 查看
http://blog.sina.com.cn/s/blog_402c071e0102x4rl.html

以下内容,对于想要使用C#实现PNG图片背景透明显示,同时动态显示时无闪烁问题的人来说,是非常有帮助的。网络上很难找到完整的解决方案。以下是我搜集到,并加以验证过的完整解决方案。
文章一:
《How to Use Transparent Images and Labels in Windows
Forms》
《在Windows Forms 中怎样使用透明图片和透明标签》

文章网址:https://www.codeproject.com/Articles/25048/How-to-Use-Transparent-Images-and-Labels-in-Window

这篇文章,提供了C#例程,讲解非常清楚,代码非常好。

文章二:
《C#画图解决闪烁问题》之《使用 GDI+ 双缓冲 解决绘图闪烁问题》

文章网址:http://www.cnblogs.com/1175429393wljblog/p/5676741.html

以下是文章部分内容:
使用 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事件

}

}
这个简单的例子实现了用鼠标拖动窗口中矩形,利用双缓冲技术使动画过程不会产生闪烁.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐