如何使表单透明,同时保持控件完全可见?

发布于 2024-08-31 13:31:28 字数 245 浏览 7 评论 0原文

我想要一个表单,其中表单上的控件完全可见,但表单本身不可见。如果我更改表单的不透明度,这会使表单及其上的控件都是半透明的,因此这不起作用。

我无法通过设置表单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛装女皇 2024-09-07 13:31:29

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.

ˉ厌 2024-09-07 13:31:29

需要注意的是,我从未使用过它,只是遇到过一次,认为“太棒了!”并继续...

查看系统。 Drawing.Drawing2D.GraphicsPath 并设置表单的 Region 属性。我向基本 Windows 窗体应用程序添加了两个按钮:

public Form1()
{
    InitializeComponent();

    Rectangle r1 = new Rectangle(button1.Location, button1.Size);
    Rectangle r2 = new Rectangle(button2.Location, button2.Size);

    GraphicsPath gp = new GraphicsPath();
    gp.AddRectangle(r1);
    gp.AddRectangle(r2);

    this.Region = new Region(gp);
}

我用矩形近似了按钮的形状;使用此代码,您可以在按钮的角落看到表单背景颜色。您需要计算出每个控件的封闭路径,并将它们单独添加到路径中。您需要考虑表单标题栏或边框样式引入的任何偏移。

更新:我做了一些调查,并有几种可能的方法来解决该问题:

  • 使用 GraphicsPath 方法,将 pictureBox.Visible 设置为 False if没有加载图像。
  • 当您将图像加载到图片框中时,分析该图像以获取其中所有颜色的列表,然后随机生成一个不是的颜色。设置表单的 BackColorTransparencyKey 属性以匹配此新颜色,汉斯·帕桑特的答案。

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:

public Form1()
{
    InitializeComponent();

    Rectangle r1 = new Rectangle(button1.Location, button1.Size);
    Rectangle r2 = new Rectangle(button2.Location, button2.Size);

    GraphicsPath gp = new GraphicsPath();
    gp.AddRectangle(r1);
    gp.AddRectangle(r2);

    this.Region = new Region(gp);
}

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:

  • Using the GraphicsPath method, set pictureBox.Visible to False if there is no image loaded.
  • When you load an image into the picture box, analyze the image to get a list of all the colors in it, then randomly generate one that isn't. Set the form's BackColor and TransparencyKey properties to match this new color, Hans Passant's answer.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文