如何使表单透明,同时保持控件完全可见?
我想要一个表单,其中表单上的控件完全可见,但表单本身不可见。如果我更改表单的不透明度,这会使表单及其上的控件都是半透明的,因此这不起作用。
我无法通过设置表单的 TransparencyKey
来完成此操作,因为表单上有一个 PictureBox
。如果 PictureBox 中的图像恰好包含与 TransparencyKey
匹配的像素,它们会在表单中显示为开口,这是我不希望的。
I would like to have a form in which the controls on the form are fully visible but the form itself is invisible. If I change the form's Opacity
, this makes both the form and the controls on it semi-transparent, so this doesn't work.
I can't do this by setting the form's TransparencyKey
, since I have a PictureBox
on the form. If the image in the PictureBox happens to contain pixels that match the TransparencyKey
, they appear as openings in the form, which I don't want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TransparencyKey 是获得此信息的唯一方法。选择正确的颜色。 Color.Fuchsia 有着作为首选颜色的悠久传统,可以追溯到 Win32 开发的早期。用它攻击你的眼睛,看看它的优点。
TransparencyKey is the only way to get this. Pick the right color. Color.Fuchsia has a long tradition of being the color of choice, going back to the early days of Win32 development. Assault your eye with it to see its merits.
需要注意的是,我从未使用过它,只是遇到过一次,认为“太棒了!”并继续...
查看系统。 Drawing.Drawing2D.GraphicsPath 并设置表单的
Region
属性。我向基本 Windows 窗体应用程序添加了两个按钮:我用矩形近似了按钮的形状;使用此代码,您可以在按钮的角落看到表单背景颜色。您需要计算出每个控件的封闭路径,并将它们单独添加到路径中。您需要考虑表单标题栏或边框样式引入的任何偏移。
更新:我做了一些调查,并有几种可能的方法来解决该问题:
GraphicsPath
方法,将pictureBox.Visible
设置为False
if没有加载图像。BackColor
和TransparencyKey
属性以匹配此新颜色,汉斯·帕桑特的答案。With the caveat that I've never used it, just ran across it once, thought "neat!" and moved on...
Look into System.Drawing.Drawing2D.GraphicsPath and setting the form's
Region
property. I added two buttons to the basic Windows forms application:I've approximated the shape of the button with a rectangle; with this code, you can see the form background color at the buttons' corners. You'll need to work out the enclosing path for each of your controls and add them to the path individually. You'll need to take into account any offset introduced by the form title bar or border style.
Update: I did some investigation and have a couple of possible approaches for the problem:
GraphicsPath
method, setpictureBox.Visible
toFalse
if there is no image loaded.BackColor
andTransparencyKey
properties to match this new color, Hans Passant's answer.