【发布时间】:2017-06-13 23:28:10
【问题描述】:
所以我正在编写一个项目,即植物大战僵尸,由纯 c# 制成,没有使用游戏引擎,这里我遇到了图形问题。 我需要在另一个透明图片框上渲染一个透明图片框,我必须定义一个真正透明的新控件,并且透明度方面一切正常,但这里有一个问题: 闪烁:| 由于 RecreateHandle(); 我有这么多。当我更改控件的图像以制作动画以及当它移动以具有真正的透明度时,我使用的方法。 这是我的代码,我想知道是否有人可以提供帮助!
public class TransparentControl : Control
{
private Image _image;
public TransparentControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.SupportsTransparentBackColor, true);
base.BackColor = Color.FromArgb(0, 0, 0, 0);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnMove(EventArgs e)
{
//RecreateHandle();
}
protected override void OnPaint(PaintEventArgs e)
{
if (_image != null)
{
e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//Do not paint background
}
//Hack
public void Redraw()
{
//RecreateHandle();
}
public Image Image
{
get
{
return _image;
}
set
{
_image = value;
//RecreateHandle();
}
}
}
【问题讨论】:
-
DoubleBuffer 绘图容器,不要使用控件。画出一切。
标签: c# winforms visual-studio picturebox