2009-02-05 73 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顏色屬性與窗體的背景顏色屬性相同