如何在 Winforms 中使元素的背景透明,如果它放置在 Web 浏览器上?

huangapple go评论57阅读模式
英文:

Winforms, how to make background of element transparent if it is placed on the web Browser?

问题

我是一个初学者。
问题是:
即使BackColor设置为"web-transparent",在某些元素(如WebBrowser)上它实际上并不透明。看起来母元素的颜色被应用了。
有没有办法让它完全透明?
或者在这种情况下,也许我需要使用其他元素?我只需要显示一段文本并能够用鼠标拖动它。

祝你有一个美好的一天!

如何在 Winforms 中使元素的背景透明,如果它放置在 Web 浏览器上?

如果将BackColor设置为透明会以"人类理解"的方式影响它,那将是不错的。

英文:

I am a beginner.
Problem is:
Even if BackColor is set to "web-transparent" it is really not transparent on some element such as a WebBrowser.
Looks like the color of mother element is applied.
Is there a way to make it fully transparent?
Or maybe I need to use some other element in this case? I just need to show a text and be able to drug it with a mouse.

Have a great day!

如何在 Winforms 中使元素的背景透明,如果它放置在 Web 浏览器上?

It will be nice if setting the BackColor to transparent will affect it in "human understanding" way)

答案1

得分: 0

你说得对,WinForms 控件出现透明的原因是因为“透明 Windows Forms 控件的背景由其父级绘制”,正如MS文档中所解释的(链接:https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-give-your-control-a-transparent-background)。这意味着父级控件的背景将被绘制到 Label 中,即使中间还有其他控件(你提到了 web 浏览器)。

解决方法是更改 Label 控件本身的形状,通过分配一个可绘制的 Region,该区域不包括透明像素。

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();

        // 更改 Label 控件的形状,去除透明像素。
        label1.BackColor = Color.Transparent;
        Bitmap bitmap = new Bitmap(label1.Width, label1.Height);
        label1.DrawToBitmap(bitmap, label1.ClientRectangle);
        Region region = new Region(label1.ClientRectangle);
        for (int x = 0; x < label1.Width; x++)
        {
            for (int y = 0; y < label1.Height; y++)
            {
                if (bitmap.GetPixel(x, y).A == 0)
                {
                    region.Exclude(new Rectangle(x, y, 1, 1));
                }
            }
        }
        label1.Region = region;
    }
}

你说你想拖动它,要注意的是,以这种方式编写的控件只会在控件仍然存在的区域上检测鼠标点击,即文本部分。但这里有一个透明的、可拖动的 Label 元素的示例解决方案,你可以随意克隆并进行实验(链接:https://github.com/IVSoftware/transparent-over-web-browser.git)。

英文:

You're correct that the reason a WinForms control appears transparent is that the "the background of a transparent Windows Forms control is painted by its parent" as explained in the MS documentation. This means the parent control's background will be drawn into the Label even if there's another control (you mentioned web browser) in between.

The remedy is to change the shape of the Label control itself, by assigning a drawable Region that excludes the transparent pixels.

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();

        // Change shape of Label control, removing transparent pixels.
        label1.BackColor = Color.Transparent;
        Bitmap bitmap = new Bitmap(label1.Width, label1.Height);
        label1.DrawToBitmap(bitmap, label1.ClientRectangle);
        Region region = new Region(label1.ClientRectangle);
        for (int x = 0; x &lt; label1.Width; x++) for (int y = 0; y &lt; label1.Height; y++)
            {
                if (bitmap.GetPixel(x, y).A == 0)
                {
                    region.Exclude(new Rectangle(x, y, 1, 1));
                }
            }
        label1.Region = region;
    }
}

如何在 Winforms 中使元素的背景透明,如果它放置在 Web 浏览器上?


You say you want to drag it so be advised that a control coded this way will only detect a mouse click on the region of the control that still exists namely the text. But here's a sample solution for a transparent, draggable Label element that you can feel free to clone and experiment with.

huangapple
  • 本文由 发表于 2023年2月16日 09:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定