Imports System.Drawing
Imports System.Runtime.InteropServices
Public Class LayeredForm
#Region " UpdateLayerdWindow 関連 API "
<DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function CreateCompatibleDC(ByVal hDC As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
End Function
<DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=True)> _
Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
<DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=True)> _
Private Shared Function SelectObject(ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
Private Shared Function GetDC(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
Private Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
End Function
<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
Private Shared Function UpdateLayeredWindow( _
ByVal hwnd As IntPtr, _
ByVal hdcDst As IntPtr, _
<System.Runtime.InteropServices.In()> _
ByRef pptDst As Point, _
<System.Runtime.InteropServices.In()> _
ByRef psize As Size, _
ByVal hdcSrc As IntPtr, _
<System.Runtime.InteropServices.In()> _
ByRef pptSrc As Point, _
ByRef crKey As Integer, _
<System.Runtime.InteropServices.In()> _
ByRef pblend As BLENDFUNCTION, _
ByVal dwFlags As Integer _
) As Boolean
End Function
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Private Structure BLENDFUNCTION
Public BlendOp As Byte
Public BlendFlags As Byte
Public SourceConstantAlpha As Byte
Public AlphaFormat As Byte
End Structure
Private Const WS_EX_LAYERED As Integer = &H80000
Private Const WS_BORDER As Integer = &H800000
Private Const WS_THICKFRAME As Integer = &H40000
Private Const AC_SRC_OVER As Byte = 0
Private Const AC_SRC_ALPHA As Byte = 1
Private Const ULW_ALPHA As Integer = 2
#End Region
#Region " コンストラクタ "
Public Sub New(ByVal bmp As Bitmap)
InitializeComponent()
Me.SetBackground(bmp)
End Sub
#End Region
#Region " オーバーライド "
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_LAYERED
cp.Style = cp.Style And (Not WS_BORDER)
cp.Style = cp.Style And (Not WS_THICKFRAME)
Return cp
End Get
End Property
#End Region
#Region " Public メソッド "
Public Sub SetBackground(ByVal srcBitmap As Bitmap)
Dim screenDc As IntPtr = GetDC(IntPtr.Zero)
Dim memDc As IntPtr = CreateCompatibleDC(screenDc)
Dim hBitmap As IntPtr = IntPtr.Zero
Dim hOldBitmap As IntPtr = IntPtr.Zero
Try
hBitmap = srcBitmap.GetHbitmap(Color.FromArgb(0))
hOldBitmap = SelectObject(memDc, hBitmap)
Dim blend As New BLENDFUNCTION
blend.BlendOp = AC_SRC_OVER
blend.BlendFlags = 0
blend.SourceConstantAlpha = 255
blend.AlphaFormat = AC_SRC_ALPHA
Dim r As Boolean = UpdateLayeredWindow( _
Me.Handle, screenDc, Me.Location, New Size(srcBitmap.Width, srcBitmap.Height), _
memDc, New Point(0, 0), 0, blend, ULW_ALPHA _
)
Finally
ReleaseDC(IntPtr.Zero, screenDc)
If hBitmap <> IntPtr.Zero Then
SelectObject(memDc, hOldBitmap)
DeleteObject(hBitmap)
End If
DeleteDC(memDc)
End Try
End Sub
#End Region
End Class