英文:
Winforms, how to make background of element transparent if it is placed on the web Browser?
问题
我是一个初学者。
问题是:
即使BackColor设置为"web-transparent",在某些元素(如WebBrowser)上它实际上并不透明。看起来母元素的颜色被应用了。
有没有办法让它完全透明?
或者在这种情况下,也许我需要使用其他元素?我只需要显示一段文本并能够用鼠标拖动它。
祝你有一个美好的一天!
如果将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!
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 < 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;
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论