Cmp*_*trb 4 c# transparency winforms
我在一个PictureBox重叠的面板中显示,因为每个PictureBox都用作一个图层.第一次PictureBox定义a并添加到面板时,它的背景颜色是透明的,并且它的图像是空的.
问题是,无法看到底层,透明图像显示面板的地面.例外的是可以看到底部PictureBox的图像.
我已尝试使用其他控件,如label.问题无法解决:(
谢谢.
Sea*_*ley 11
这是因为,如果我没记错的话,设置Transparent的背景颜色(它的实际值是null,对吧?)并不是真的透明.Windows的作用是查看控件的父容器的背景颜色,并将控件背景颜色设置为该颜色.
你可以看到这种情况发生在面板上.没有内容,设置为透明的面板应该让你看到它们后面,对吗?错误.如果您将一个面板放在一堆(例如,文本框控件)上并将面板设置为"透明",您将无法看到它背后的文本框.
相反,要获得真正的透明度,你必须为相关控件重载OnPaintBackground,基本上,什么都不做(不要把base.OnPainBackground称为!)......可能还有更多,但是这里是我们在这里使用的工作TransparentPanel控件的示例:
public class TransparentPanel : System.Windows.Forms.Panel
{
[Browsable(false)]
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// Do Nothing
}
}
Run Code Online (Sandbox Code Playgroud)
我们已成功使用此类在以前的Windows窗体应用程序中创建真正透明的面板.我们用它来修复"右键单击上下文菜单出现在按钮控件之上"问题.
这是我的看法:
class TransPictureBox : Control
{
private Image _image = null;
public Image Image
{
get
{
return _image;
}
set
{
_image = value;
}
}
public TransPictureBox()
{
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
protected override void OnPaint(PaintEventArgs pe)
{
if(Image != null)
pe.Graphics.DrawImage(Image, 0, 0);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
}
Run Code Online (Sandbox Code Playgroud)
您应该根据需要添加一些用于定位图像的逻辑,并相应地编辑OnPaint方法.
| 归档时间: |
|
| 查看次数: |
15615 次 |
| 最近记录: |