如何将表单设置为具有透明背景 [英] How can I set a form to have a transparent background

查看:64
本文介绍了如何将表单设置为具有透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当前在表单新我设置

Me.SetStyle(ControlStyles.SupportsTransparentBackColor, true) 

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

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

有人可以帮忙吗??

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

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

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

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.

下面是我的代码 -

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

推荐答案

对透明表单使用 TransparencyKey.

Use TransparencyKey for transparent form.

例如

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

现在运行表单,你会发现 button1 上有一个洞.

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

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

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.

抱歉回复晚了.

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

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

这篇关于如何将表单设置为具有透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆