<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
<Runtime.InteropServices.DllImport("gdi32.dll", ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function SelectObject(ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function UpdateLayeredWindow( _
ByVal hwnd As IntPtr, _
ByVal hdcDst As IntPtr, _
<System.Runtime.InteropServices.In()> ByRef pptDst As System.Drawing.Point, _
<System.Runtime.InteropServices.In()> ByRef psize As System.Drawing.Size, _
ByVal hdcSrc As IntPtr, _
<System.Runtime.InteropServices.In()> ByRef pptSrc As System.Drawing.Point, _
ByRef crKey As Int32, _
<System.Runtime.InteropServices.In()> ByRef pblend As BLENDFUNCTION, _
ByVal dwFlags As Int32 _
) As Boolean
End Function
<System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential, Pack:=1)> _
Public 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_COLORKEY As Int32 = 1
Private Const ULW_ALPHA As Int32 = 2
Private Const ULW_OPAQUE As Int32 = 4
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
'Set WS_EX_LAYERED
Dim cp As System.Windows.Forms.CreateParams
cp = 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
Public Sub SetBitmap(ByVal SrcBmp As System.Drawing.Bitmap)
Dim g_sc As System.Drawing.Graphics
Dim g_mem As System.Drawing.Graphics
Dim hdc_sc As IntPtr
Dim hdc_mem As IntPtr
Dim hbmp As IntPtr
Dim hbmp_old As IntPtr
Dim blf As New BLENDFUNCTION
g_sc = System.Drawing.Graphics.FromHwnd(IntPtr.Zero)
g_mem = System.Drawing.Graphics.FromImage(SrcBmp)
hdc_sc = g_sc.GetHdc()
hdc_mem = g_mem.GetHdc()
hbmp = SrcBmp.GetHbitmap(Drawing.Color.Black)
hbmp_old = SelectObject(hdc_mem, hbmp)
blf.BlendOp = AC_SRC_OVER
blf.BlendFlags = 0
blf.SourceConstantAlpha = 255
blf.AlphaFormat = AC_SRC_ALPHA
Dim r As Boolean
r = UpdateLayeredWindow( _
Me.Handle, _
hdc_sc, Me.Location, New System.Drawing.Size(SrcBmp.Width, SrcBmp.Height), _
hdc_mem, New System.Drawing.Point(0, 0), _
0, blf, ULW_ALPHA)
SelectObject(hdc_mem, hbmp_old)
DeleteObject(hbmp)
g_mem.ReleaseHdc(hdc_mem)
g_sc.ReleaseHdc(hdc_sc)
g_mem.Dispose()
g_sc.Dispose()
If Not r Then Throw New ApplicationException(System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString)
End Sub
|