2009-02-05 74 views
6

我努力让我的形式有新我设置如何设置窗体有一个透明的背景

Me.SetStyle(ControlStyles.SupportsTransparentBackColor, true) 

但还是形式在vb.net透明背景

目前该表格显示为具有默认灰色背景

任何人都可以帮忙吗?

编辑:我需要的控件在窗体上是可见的,所以我不觉得不透明度设置为0,将工作

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

下面

是我的代码 -

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 
+0

我添加了下面的代码以获取您正在查找的确切内容。 如果需要额外的东西,请发表评论,我会更新相同的 – 2009-02-09 13:32:51

回答

11

使用TransparencyKey作为透明窗体。

例如。

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

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

因此,使用此方法,您可以在绘图中创建一个掩膜图像,其中的部分必须是透明的,并将该图像应用于窗体,并且窗体现在是透明的。

编辑: 对不起,延迟回复。

以下是你的代码修改,以满足您的需求

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 
+0

虽然这可能是一个旧的帖子,谷歌的最佳结果..一个可能适用于某些人的简短例子就是这个'TransparencyKey = Me.BackColor` ..但是请确保你的表单背景不在你使用的图像或任何其他控件的形式..仍然设置你的窗体backcolor有一个unqiue颜色 – Mayhem 2016-07-17 07:28:31

-2
Me.Opacity = 0 

被警告:

  1. 这是整个窗体,而不仅仅是背景。有一些解决方法可以使某些部分更加相似。
  2. 这只是假透明的,它需要快照它背后的内容。它足够聪明,可以知道何时移动窗体,但当您将其他透明对象移动到窗体顶部时不会。
+0

这也会隐藏窗体上的所有控件元素。 – Ahmad 2014-12-04 05:21:06

1

有可以使用的一些方法。

  • 使用窗体TransparencyKey
  • 覆盖OnPaintBackground(WM_ERASEBKGND)
  • 覆盖的WndProc和处理绘制消息(WM_NCPAINT,WM_PAINT等)

我建议重写窗口过程得到优化结果。

2

设置窗体的TransparencyKey颜色属性与窗体的背景颜色属性相同