vb.net 如何将表单设置为具有透明背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/518020/
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-09-09 14:03:01  来源:igfitidea点击:

How can I set a form to have a transparent background

vb.netwinforms

提问by Dean

I am struggling to get my form to have a transparent background in vb.net

我正在努力让我的表单在 vb.net 中具有透明背景

Currently in the form New I set

目前在形式新我设置

Me.SetStyle(ControlStyles.SupportsTransparentBackColor, true) 

But still the form shows up as having the default grey background

但是表单仍然显示为具有默认的灰色背景

Can anyone help??

有人可以帮忙吗??

EDIT: I need the controls on the form to be visible so I don't think setting the opacity to 0 will work

编辑:我需要表单上的控件可见,所以我不认为将不透明度设置为 0 会起作用

EDIT: I tried the transparency key solution but it doesn't work. I have a circular image with a black background. OnPaint I set the transparency key to the img pixel at 0,0, this then leaves me with circular image (which I want ) It hides the black background but I am still left with the default grey rectangle of the form.

编辑:我尝试了透明键解决方案,但它不起作用。我有一个黑色背景的圆形图像。OnPaint 我将 img 像素的透明度键设置为 0,0,然后给我留下圆形图像(我想要的)它隐藏了黑色背景,但我仍然保留表单的默认灰色矩形。

below is the code I have -

下面是我的代码 -

Public Sub New()

    Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    Me.BackColor = Color.Transparent
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Me.Timer1.Start()
End Sub

Private Sub frmWoll_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Dim img As Bitmap = CType(Me.BackgroundImage, Bitmap)

    img.MakeTransparent(img.GetPixel(2, 2))
    Me.TransparencyKey = img.GetPixel(2, 2)
End Sub

回答by Sachin Chavan

Use TransparencyKey for transparent form.

将 TransparencyKey 用于透明表单。

eg.

例如。

TransparencyKey = Color.Red
Button1.BackColor = Color.Red

Now run the form you will find that the button1 has a hole in it.

现在运行该表单,您会发现 button1 上有一个洞。

So using this method you can create a mask image in paint for which part has to be transparent and apply that image to form and voila the form is now transparent.

因此,使用这种方法,您可以在油漆中创建一个蒙版图像,其中部分必须是透明的,然后将该图像应用于表单,瞧,表单现在是透明的。

Edit: Sorry for late reply.

编辑:抱歉回复晚了。

Following is your code modified to suit your requirement

以下是您修改后的代码以满足您的要求

Public Sub New()

    Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    Me.BackColor = Color.Transparent

    ' This call is required by the Windows Form Designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    Dim img As Bitmap = CType(Me.BackgroundImage, Bitmap)

    'img.MakeTransparent(img.GetPixel(2, 2))
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    Me.TransparencyKey = img.GetPixel(2, 2)
End Sub

回答by Hiren H Patel

Set Form's TransparencyKey color property same as form's Background color property

将表单的 TransparencyKey 颜色属性设置为与表单的背景颜色属性相同

回答by David Anderson

There are a few methods you could use.

您可以使用几种方法。

  • Use the forms TransparencyKey
  • Override OnPaintBackground (WM_ERASEBKGND)
  • Override WndProc and handle the paint messages (WM_NCPAINT, WM_PAINT, etc)
  • 使用表格 TransparencyKey
  • 覆盖 OnPaintBackground (WM_ERASEBKGND)
  • 覆盖 WndProc 并处理绘制消息(WM_NCPAINT、WM_PAINT 等)

I recommend overriding the window procedure to get optimal results.

我建议覆盖窗口过程以获得最佳结果。

回答by Joel Coehoorn

Me.Opacity = 0

Be warned that:

请注意:

  1. This is for the entire form, rather than just the background. There are work-arounds to make certain parts more opague.
  2. It's only psuedo-transparency where it takes a snapshot of what's behind it. It's smart enough to know when you move the form, but not when you move other transparent objects on top of the form.
  1. 这适用于整个表格,而不仅仅是背景。有一些变通方法可以使某些部分更加不透明。
  2. 它只是伪透明,可以对其背后的内容进行快照。知道何时移动表单是很聪明的,但不知道何时移动表单顶部的其他透明对象。