我目前正在研究一个需要透明面板上的动画的项目。我已经能够创建一个透明面板并对其进行绘制,但是当我刷新面板时,使用钢笔工具绘制的面板不会被重绘为透明。这留下了我以前画在面板上的最后一个位置。C#透明面板刷新问题
我假设有一个简单的方法来重写OnPaint或Refresh来重绘面板上的所有像素为透明的,但我无法在网上找到解决方案或自己弄明白。
这里是我用来做背景透明的代码:
public class TransparentPanel : Panel
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
}
}
这是我最失败的尝试重绘透明背景:
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100,255,255,255)), this.ClientRectangle);
}
这种解决方案的问题是,在两次刷新后,背景变得不透明。
任何人都可以帮我弄清楚如何做到这一点?我对图形和动画非常陌生,我认为这有一个相当简单的答案。提前致谢。
***编辑* ** 按Dyppl的回应,我已经改变了我绘制图形面板的方式。这里是我当前的代码:
public TransparentPanel fighterPanel;
...
fighterPanel.Paint +=new PaintEventHandler(fighterPanel_Paint);
...
fighterPanel = new TransparentPanel();
fighterPanel.Location = new System.Drawing.Point(600, 300);
fighterPanel.Size = new System.Drawing.Size(400, 400);
gameArenaForm.Controls.Add(fighterPanel);
fighterPanel.BringToFront();
...
private void fighterPanel_Paint(object sender, PaintEventArgs e)
{
using (Pen blackPen = new Pen(Color.Black, 3), redPen = new Pen(Color.Red, 3), bluePen = new Pen(Color.Blue, 3))
{
//head
e.Graphics.DrawEllipse
(blackPen,
head.DrawPoint(torso.GetTorsoAngle(), torso.neck.getLocationX(), torso.neck.getLocationY()).X,
head.DrawPoint(torso.GetTorsoAngle(), torso.neck.getLocationX(), torso.neck.getLocationY()).Y,
head.radius * 2,
head.radius * 2
);
//torso
e.Graphics.DrawLine(blackPen, torso.neck.getLocationX(), torso.neck.getLocationY(), torso.shoulders.getLocationX(), torso.shoulders.getLocationY());
e.Graphics.DrawLine(blackPen, torso.shoulders.getLocationX(), torso.shoulders.getLocationY(), torso.hips.getLocationX(), torso.hips.getLocationY());
//right arm
e.Graphics.DrawLine(blackPen, torso.shoulders.getLocationX(), torso.shoulders.getLocationY(), rightArm.elbow.getLocationX(), rightArm.elbow.getLocationY());
e.Graphics.DrawLine(redPen, rightArm.elbow.getLocationX(), rightArm.elbow.getLocationY(), rightArm.attachHand.getLocationX(), rightArm.attachHand.getLocationY());
//left arm
e.Graphics.DrawLine(blackPen, torso.shoulders.getLocationX(), torso.shoulders.getLocationY(), leftArm.elbow.getLocationX(), leftArm.elbow.getLocationY());
e.Graphics.DrawLine(bluePen, leftArm.elbow.getLocationX(), leftArm.elbow.getLocationY(), leftArm.attachHand.getLocationX(), leftArm.attachHand.getLocationY());
//right leg
e.Graphics.DrawLine(blackPen, torso.hips.getLocationX(), torso.hips.getLocationY(), rightLeg.knee.getLocationX(), rightLeg.knee.getLocationY());
e.Graphics.DrawLine(redPen, rightLeg.knee.getLocationX(), rightLeg.knee.getLocationY(), rightLeg.attachFoot.getLocationX(), rightLeg.attachFoot.getLocationY());
//left leg
e.Graphics.DrawLine(blackPen, torso.hips.getLocationX(), torso.hips.getLocationY(), leftLeg.knee.getLocationX(), leftLeg.knee.getLocationY());
e.Graphics.DrawLine(bluePen, leftLeg.knee.getLocationX(), leftLeg.knee.getLocationY(), leftLeg.attachFoot.getLocationX(), leftLeg.attachFoot.getLocationY());
}
}
下面是之前和对象移动后的一些照片,并刷新了几次:
对不起,也不会让我嵌入图像,除非我有10个代表
从一个对图形并不陌生的人来说:当涉及到GDI +和winforms时,透明度从来就不是很简单...... – Dyppl 2011-06-05 08:12:46
Ewe,那对我来说听起来不是很有希望。你对这个问题有什么想法吗? – Alex 2011-06-05 08:15:21
@亚历克斯:是的,现在尝试 – Dyppl 2011-06-05 08:17:39