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事件
}
}
这个简单的例子实现了用鼠标拖动窗口中矩形,利用双缓冲技术使动画过程不会产生闪烁.
以下内容,对于想要使用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事件
}
}
这个简单的例子实现了用鼠标拖动窗口中矩形,利用双缓冲技术使动画过程不会产生闪烁.
相关文章推荐
- C# 解决panel或者其他控件叠加时,此控件背景透明,显示的背景色为窗体背景色问题
- CStatic子类化解决背景透明,文本重叠,刷新闪烁问题
- c# 窗体的背景图片在加载时,背景图片闪烁问题 已解决
- C#解决动态添加控件时,控件闪烁的问题
- CStatic子类化解决背景透明,文本重叠,刷新闪烁问题
- 如何使STATIC控件的背景透明以及动态改变控件文本内容时闪烁的问题
- c#解决窗口背景贴图刷新,放大后图像闪烁问题
- 在IE和FireFox中实现Flash背景透明和解决绝对定位元素被Flash遮住的问题
- IE6下PNG背景透明的显示问题解决方案
- C#中保存GIF文件后透明背景问题的一个解决方法
- C#中保存GIF文件后透明背景问题的一个解决方法
- 如何使STATIC控件的背景透明以及动态改变控件文本内容时闪烁的问题
- 使用DD_belatedPNG让IE6支持PNG透明图片 及IE6下背景图闪烁问题的解决方法
- Android GridView之添加分隔线,动态设置高度,实现高度自适应,并解决第一个item不显示的问题
- [转]C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- [转]C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- C# 绘图中的图片闪烁问题解决方法的使用结果
- 仿MFC实现c++按名动态创建对象之续(瘦身以及解决其在dll中使用的问题)
- 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)