Vb.net 如何将窗体设置为具有透明背景
我正在努力使我的表单在vb.net中有一个透明的背景 目前以新的我设置的形式Vb.net 如何将窗体设置为具有透明背景,vb.net,winforms,Vb.net,Winforms,我正在努力使我的表单在vb.net中有一个透明的背景 目前以新的我设置的形式 Me.SetStyle(ControlStyles.SupportsTransparentBackColor, true) 但是表单仍然显示为默认的灰色背景 有人能帮忙吗 编辑:我需要窗体上的控件可见,所以我认为将不透明度设置为0不起作用 编辑:我尝试了透明键解决方案,但不起作用。我有一个黑色背景的圆形图像。OnPaint我将img像素的透明度关键点设置为0,0,这就留下了圆形图像(我想要的),它隐藏了黑色背景,但
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, true)
但是表单仍然显示为默认的灰色背景
有人能帮忙吗
编辑:我需要窗体上的控件可见,所以我认为将不透明度设置为0不起作用
编辑:我尝试了透明键解决方案,但不起作用。我有一个黑色背景的圆形图像。OnPaint我将img像素的透明度关键点设置为0,0,这就留下了圆形图像(我想要的),它隐藏了黑色背景,但我仍然保留了表单的默认灰色矩形
以下是我的密码-
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
请注意:
对透明窗体使用透明键 例如 现在运行表单,您将发现按钮1上有一个孔 因此,使用这种方法,您可以在绘画中创建一个遮罩图像,该部分必须是透明的,并将该图像应用于形状,瞧,形状现在是透明的 编辑: 对不起,迟了答复 以下是为满足您的需求而修改的代码
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
您可以使用以下几种方法
- 使用表单透明键
- 覆盖绘画背景(WM_擦除BKGND)
- 覆盖WndProc并处理绘制消息(WM_NCPAINT、WM_paint等)
我建议重写窗口过程以获得最佳结果。将窗体的TransparencyKey颜色属性设置为与窗体的背景色属性相同。我添加了下面的代码以获得您想要的确切结果。如果需要任何额外的内容,请发表评论,我会更新表单。这也会隐藏表单上的所有控件元素。虽然这可能是一篇旧文章,但在谷歌搜索结果中排名靠前。。一个可能适用于某些人的简短示例就是
TransparencyKey=Me.BackColor
。。但请确保您使用的图像或表单上的任何其他控件中没有表单背景色。。仍然将窗体背景色设置为具有unqiue颜色
TransparencyKey = Color.Red
Button1.BackColor = Color.Red
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