C# 另一个普通面板顶部的透明面板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/816076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:19:06  来源:igfitidea点击:

Transparent Panel on top of another normal panel

c#winforms

提问by Prithis

I want to show a transparent panel on top of another panel, both the panels have child controls like labels, text boxes etc. Transparency works fine if the transparent panel is child control of the other panel but if not then the label and text box of the normal panel appears on top of the transparent panel. Transparency for rest of the area works fine.

我想在另一个面板的顶部显示一个透明面板,两个面板都有子控件,如标签、文本框等。如果透明面板是另一个面板的子控件,则透明度工作正常,但如果不是,则标签和文本框普通面板出现在透明面板的顶部。该区域其余部分的透明度工作正常。

Any ideas ???

有任何想法吗 ???

I have tried bring the transparent panel to the front but did not help. Perhaps I need to specify the order in which the controls should be drawn ?? If yes how do I do that ?

我曾尝试将透明面板放在前面,但没有帮助。也许我需要指定绘制控件的顺序??如果是,我该怎么做?

Interestingly if I move the application below the task bar and bring it up. It achives the right result. ( Reprinting resolves the issue !! but why??). However when I minimize it and restore it does not fix it!

有趣的是,如果我将应用程序移到任务栏下方并将其调出。它达到了正确的结果。(重印解决了问题!!但为什么呢??)。但是,当我最小化它并恢复它时并不能修复它!

Thanks,

谢谢,

回答by STW

If all your panels/labels/controls are part of a single UserControl then you are probably right that you just need to set the Z-order (the front-to-back order). You can do this using the Document Outline inside of Visual Studio's Designer under View > Other Windows > Document Outline.

如果您的所有面板/标签/控件都是单个 UserControl 的一部分,那么您可能只需要设置 Z 顺序(从前到后的顺序)。您可以使用 Visual Studio 设计器中“视图”>“其他窗口”>“文档大纲”下的“文档大纲”来执行此操作。

If you're doing it programatically then you can play around with calling Control.BringToFront() or Control.SendToBack() to change their z-order. One possible way to do this is like (assuming ctl1 is intended to be at the back and ctl4 is intended to be front-mode.

如果您以编程方式执行此操作,则可以调用 Control.BringToFront() 或 Control.SendToBack() 来更改它们的 z 顺序。一种可能的方法是(假设 ctl1 打算在后面,而 ctl4 打算在前面模式。

SuspendLayout()
ctl1.BringToFront()
ctl2.BringToFront() ' Bring ctl2 in front of ctl1
ctl3.BringToFront() ' 3 in front of 2 (and in turn 1)
ctl4.BringToFront() ' 4 in front of the rest
ResumeLayout()

The Suspend/Resume Layout calls prevent the UI from updating while you rearrange things.

Suspend/Resume Layout 调用会阻止 UI 在您重新排列时更新。

回答by Paul Alexander

Transparency in Windows.Forms is implemented by relational hierarchy rather than visual hierarchy. When a transparent control is painted, .NET basically calls up the Parent tree asking each parent control to paint itself, then paints the actual control content itself.

Windows.Forms 中的透明度是由关系层次结构而不是视觉层次结构实现的。绘制透明控件时,.NET 基本上会调用父树,要求每个父控件绘制自身,然后绘制实际的控件内容本身。

Two siblings in the same control will paint over each other.

同一控件中的两个兄弟姐妹将相互覆盖。

So to answer the question, the topmost panel/control needs to be a child of the control you want to paint on top of.

因此,要回答这个问题,最上面的面板/控件需要是您要在其上绘制的控件的子项。

回答by Cameron Stone

If you want a transparent control that won't obscure its siblings, according to Bob Powellyou can do it by overriding the CreateParams method to add true transparency. Read the link for a full description.

如果您想要一个不会遮挡其兄弟的透明控件,根据Bob Powell 的说法,您可以通过覆盖 CreateParams 方法来添加真正的透明度来实现。阅读完整说明的链接。