2012-05-22 4 views
0

알파 채널이있는 PNG를 폼에로드하고 알파 채널과 일치하도록 폼을 변형하려면 다음 코드가 있습니다.알파 투명 윈도우 폼

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 비트 맵에 렌더링하려고 시도했지만 정적 이미지가됩니다.

+0

비트 맵을 32bpp로 변환하고 해당 코드를 제거하지 않는 이유는 무엇입니까? –

+0

죄송합니다. 솔루션을 찾았습니다. http://stackoverflow.com/questions/2979125/how-to-make-the-form-into-fully-transparent-32bit-alpha :( –

+0

@ 한스 패스언트 : PNG (일부 불투명 지역)의 반투명 데이터 ... –

답변

0

그래서 나는 투명한 백그라운드 폼과 컨트롤을 가진 전경 폼을 만들어야 만한다. 제어 양식에는 제어 영역 만 표시됩니다.

관련 문제