devflow.kr@:~#

Alpha-blend transparency form (PNG 파일 alpha로 form투명화)

VB

아래는 class입니다.

Imports System

Imports System.Drawing

Imports System.Drawing.Imaging

Imports System.Windows.Forms

Imports System.Runtime.InteropServices

'Translation from C# by Dj Den4ik

Public Class PerPixelAlphaForm

    'Implements Windows.Forms.IWin32Window


    Public Structure ARGB

        Public Blue As Byte

        Public Green As Byte

        Public Red As Byte

        Public Alpha As Byte

    End Structure


    Public Structure BLENDFUNCTION

        Public BlendOp As Byte

        Public BlendFlags As Byte

        Public SourceConstantAlpha As Byte

        Public AlphaFormat As Byte

    End Structure


    Public Const ULW_COLORKEY As Int32 = &H1

    Public Const ULW_ALPHA As Int32 = &H2

    Public Const ULW_OPAQUE As Int32 = &H4


    Public Const AC_SRC_OVER As Byte = &H0

    Public Const AC_SRC_ALPHA As Byte = &H1


    Public Declare Function UpdateLayeredWindow Lib "user32" Alias "UpdateLayeredWindow" (ByVal hwnd As IntPtr, ByVal hdcDst As IntPtr, ByRef pptDst As Point, ByRef psize As Size, ByVal hdcSrc As IntPtr, ByRef pprSrc As Point, ByVal crKey As Int32, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As Int32) As Boolean

    Public Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hWnd As IntPtr) As IntPtr

    Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer

    Public Declare Function CreateCompatibleDC Lib "gdi32.dll" Alias "CreateCompatibleDC" (ByVal hDC As IntPtr) As IntPtr

    Public Declare Function DeleteDC Lib "gdi32.dll" Alias "DeleteDC" (ByVal hDC As IntPtr) As Boolean

    Public Declare Function SelectObject Lib "gdi32.dll" Alias "SelectObject" (ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr

    Public Declare Function DeleteObject Lib "gdi32.dll" Alias "DeleteObject" (ByVal hObject As IntPtr) As Boolean


    Public Sub SetBitmap(ByVal bitmap As Bitmap, ByVal opacity As Byte, ByVal frm As Form)

        If bitmap.PixelFormat <> PixelFormat.Format32bppArgb Then Throw New ApplicationException("The bitmap must be 32ppp with alpha-channel.")


        ' The ideia of this is very simple,

        ' 1. Create a compatible DC with screen;

        ' 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;

        ' 3. Call the UpdateLayeredWindow.


        Dim screenDc As IntPtr = GetDC(IntPtr.Zero)

        Dim memDc As IntPtr = 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 = SelectObject(memDc, hBitmap)

            Dim size As New Size(bitmap.Width, bitmap.Height)

            Dim pointSource As New Point(0, 0)

            Dim topPos As New Point(frm.Left, frm.Top)

            Dim blend As New BLENDFUNCTION

            blend.BlendOp = AC_SRC_OVER

            blend.BlendFlags = 0

            blend.SourceConstantAlpha = opacity

            blend.AlphaFormat = AC_SRC_ALPHA

            UpdateLayeredWindow(frm.Handle, screenDc, topPos, size, memDc, pointSource, 0, blend, ULW_ALPHA)

        Finally

            ReleaseDC(IntPtr.Zero, screenDc)

            If hBitmap <> IntPtr.Zero Then

                SelectObject(memDc, oldBitmap)

                DeleteObject(hBitmap)

            End If

            DeleteDC(memDc)


        End Try

    End Sub

End Class


아래는 폼입니다.

Dim ppaf As New PerPixelAlphaForm

    Dim MainBack As Bitmap = My.Resources.mainback

    Dim bmp As New Bitmap(MainBack)

Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams

        Get

            Dim SecPerm As New Security.Permissions.SecurityPermission(Security.Permissions.PermissionState.Unrestricted)

            SecPerm.Demand()

 

            ' Extend the CreateParams property of the Button class.

            Dim cp As System.Windows.Forms.CreateParams = MyBase.CreateParams

            ' Update the button Style.

            cp.ExStyle = &H80000

 

            Return cp

        End Get

    End Property

    Public Sub redraw()

        Me.BackgroundImage = MainBack

        bmp.Dispose()

        bmp = New Bitmap(MainBack)

        For Each ctrl As Control In Me.Controls

            Application.DoEvents()

            ctrl.DrawToBitmap(bmp, ctrl.Bounds)

        Next

        Me.Region = New Region()

        ppaf.SetBitmap(bmp, 255, Me)

    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        Const WM_MOUSEMOVE As Int32 = &H200

        Const WM_NCLBUTTONDOWN As Int32 = &HA1

        Const HTCAPTION As Int32 = 2

 

        If m.Msg = WM_MOUSEMOVE Then

            MyBase.Capture = False

            Dim message As New Message

            With message

                .HWnd = Me.Handle

                .Msg = WM_NCLBUTTONDOWN

                .WParam = HTCAPTION

                .LParam = 0&

            End With

            MyBase.WndProc(message)

        End If

        MyBase.WndProc(m)

    End Sub


PNG-24를 지원합니다.

Alpha-blending으로 form을 투명화합니다.



출저 : http://pastesite.com/22086 & MORE

티스토리 툴바