Alpha透明窗口形式

时间:2012-05-22 09:18:07

标签: vb.net winforms vb.net-2010 windows-applications

我得到以下代码,将带有Alpha通道的PNG加载到表单上,并重新整形表单以匹配Alpha通道。

Public Function applyAlphaForm(ByVal f As Form, ByVal bitmap As Bitmap, Optional ByVal opacity As Byte = 255) As Boolean
    f.FormBorderStyle = FormBorderStyle.None
    Dim style As Long
    style = Win32.GetWindowLong(f.Handle, Win32.GWL_EXSTYLE)
    If Not (style And Win32.WS_EX_LAYERED = Win32.WS_EX_LAYERED) Then
        style = style Or Win32.WS_EX_LAYERED
        Win32.SetWindowLong(f.Handle, Win32.GWL_EXSTYLE, style)
    End If
    Return SetBitmap(f, bitmap, opacity)
End Function

Public Function SetBitmap(ByVal f As Form, ByVal bitmap As Bitmap, ByVal opacity As Byte) As Boolean
    f.Height = bitmap.Height
    f.Width = bitmap.Width
    If bitmap.PixelFormat <> PixelFormat.Format32bppArgb Then
        f.BackgroundImage = bitmap
        f.TransparencyKey = bitmap.GetPixel(0, 0)
        Return True
    End If

    Dim screenDC As IntPtr = Win32.GetDC(IntPtr.Zero)
    Dim memDC As IntPtr = Win32.CreateCompatibleDC(screenDC)
    Dim hBitmap As IntPtr = IntPtr.Zero
    Dim oldBitmap As IntPtr = IntPtr.Zero

    Try
        hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)) 'grab a GDI handle from this GDI+ bitmap
        oldBitmap = Win32.SelectObject(memDC, hBitmap)

        Dim size As Win32.Size = New Win32.Size(bitmap.Width, bitmap.Height)
        Dim pointSource As Win32.Point = New Win32.Point(0, 0)
        Dim topPos As Win32.Point = New Win32.Point(f.Left, f.Top)
        Dim blend As Win32.BLENDFUNCTION = New Win32.BLENDFUNCTION()
        blend.BlendOp = Win32.AC_SRC_OVER
        blend.BlendFlags = 0
        blend.SourceConstantAlpha = opacity
        blend.AlphaFormat = Win32.AC_SRC_ALPHA

        Win32.UpdateLayeredWindow(f.Handle, screenDC, topPos, size, memDC, pointSource, 0, blend, Win32.ULW_ALPHA)

    Catch ex As Exception
    Finally
        Win32.ReleaseDC(IntPtr.Zero, screenDC)
        If hBitmap <> IntPtr.Zero Then
            Win32.SelectObject(memDC, oldBitmap)
            Win32.DeleteObject(hBitmap)
        End If
        Win32.DeleteDC(memDC)
    End Try
    Return True
End Function

很好,很容易,但如果我在表格上放一些控件(按钮,文本框......),它们就会消失。我的客人 UpdateLayeredWindow 将在hDC表格上绘制,因此我们看不到它背后的任何内容。那么如何在表单上绘制一些表单控件呢?我尝试循环遍历所有控件并在调用api之前渲染到png位图,但这将是静态图像。

1 个答案:

答案 0 :(得分:0)

所以我来客我必须创建两个,一个背景形式alpha透明和一个带有控制的前景形式。控制表格仅显示控制区域。