0

我得到了以下代码,将带有 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 位图,但这将是静态图像。

4

1 回答 1

0

所以我客人我必须创建两个,一个是透明的背景表单,一个是带有控件的前景表单。控制窗体将只显示控制区域。

于 2012-08-01T15:28:17.933 回答