【发布时间】:2011-12-14 00:28:11
【问题描述】:
我创建了一个透明表布局面板:
class TransTablePanel : TableLayoutPanel
{
public TransTablePanel()
{
}
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
return createParams;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// Do not paint background.
}
}
还有一个透明的图片框(我试过只使用 BackColor = Transparent 但没用)
class TransPicBox : PictureBox
{
public TransPicBox()
{
}
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
return createParams;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// Do not paint background.
}
}
结果如下:
第一个单元格是带有此绘制事件的 PictureBox:
private void picBoxCompass_Paint(object sender, PaintEventArgs e)
{
Bitmap b = Properties.Resources.Compass_Rose;
float rot = PitControl.GetPitbullRotation();
e.Graphics.DrawImage(rotateImage(b, rot), 0, 0, picBoxCompass.Width, picBoxCompass.Height);
e.Graphics.DrawLine(LinePen, picBoxCompass.Width / 2, 0, picBoxCompass.Width / 2, picBoxCompass.Height / 2);
e.Graphics.FillPie(Brushes.Green, picBoxCompass.Width / 2 - 10, picBoxCompass.Height / 2 - 10, 20, 20, 0, 360);
}
你可以看到它不是透明的(黑色背景),第二个单元格是透明的(你可以看到我的表单的背景图片)。
如何使 PictureBox 透明?
【问题讨论】:
-
也许您的 Compass_Rose 缺少一些透明度?
-
Compass_Rose 是具有全透明背景的 PNG :)
-
我不确定,但我认为你不应该重写 OnPaintBackground 方法,如果你指定透明度作为背景,你将不得不绘制它
-
@Tokk 我尝试使用具有透明背景的普通 PictureBox 也没有成功,并且 TranPanel 工作完美。 (比使用透明颜色的普通面板更快)
标签: c# .net winforms transparency picturebox