C#中Overlapped PictureBox的透明度问题 [英] Transparency issue by Overlapped PictureBox's at C#

查看:96
本文介绍了C#中Overlapped PictureBox的透明度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在重叠显示的面板中显示 PictureBox ,因为使用了每个 PictureBox 作为一层.第一次定义 PictureBox 并将其添加到面板中时,它是背景色是透明的,并且图像为空.

I'm displaying in a panel PictureBox overlapped, because each PictureBox is used as a layer. First time a PictureBox is defined and added to panel it's background color is transparent, and it's images are empty.

问题是,底层看不到,透明的图像,显​​示了面板的地面.唯一可以看到的是 PictureBox 底部的图像.

The problem is, the bottom layer can not be seen, the transparent image, shows the panel's ground. Excepted is that the bottom PictureBox's image is seen.

我已经尝试过使用其他控件(例如标签).该问题无法解决:(

I have tried to it with other controls like label. The problem could not be solved :(

谢谢.

推荐答案

这是因为,如果我没记错的话,将背景色设置为透明"(其实际值为null,对吗?)不是真正地透明.Windows要做的是查看控件的父容器的背景色,并将控件的背景色设置为此.

This is because, if I remember correctly, setting a background color of Transparent (its actual value is null, right?) isn't really transparent. What Windows does is it looks at the control's parent container's background color and sets the controls background color to that.

您可以看到这种情况的发生,尤其是在面板上.如果没有内容,设置为透明"的面板应该让您在它们后面看到,对吗?错误的.如果将面板放置在许多文本框控件的顶部,并将该面板设置为透明",则将无法看到其后面的文本框.

You can see this happen especially with panels. Without contents, panels set to Transparent should let you see behind them, right? Wrong. If you put a panel on top of a bunch of, say, textbox controls and set the panel to Transparent, you won't be able to see the textboxes behind it.

相反,要获得真正的透明度,您必须为有问题的控件重载OnPaintBackground,并且从根本上讲,什么也不做(也不要调用base.OnPainBackground!)...可能还不止这些,但是这是我们在此处使用的工作中的TransparentPanel控件的示例:

Instead, to get real transparency, you have to overload OnPaintBackground for the control in question and, essentially, do absolutely nothing (DONT'T call the base.OnPainBackground either!)... There's more to it than that, probably, but here is an example of a working TransparentPanel control we use here:

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
    }
}

我们已经成功使用该类在过去的Windows Forms应用程序中创建了真正透明的面板.我们将其作为一种技巧来解决右键单击上下文菜单出现在按钮控件顶部"的问题.

We've used this class successfully to create truly transparent panels in past Windows Forms apps. We used it as a hack to fix the "right-click context menu appears on top of button controls" problem.

这篇关于C#中Overlapped PictureBox的透明度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆