Weil maximal 5000 Zeichen zulässig sind, kommt jetzt der Rest:
Wenn man die Imports und die Win32-Klasse (als Public) in eine eigene Datei verfrachtet kann man auch einfach in den generierten Code des Form-Designers die Methoden und Eigenschaften einfügen und das Ganze nutzen.
Das würde dann, bei Form1, so aussehen:
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Das Formular überschreibt den Löschvorgang, um die Komponentenliste zu
' bereinigen.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
' Hier das Neue
' Dim CurrBitmap as Bitmap
Public Sub New()
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.AllowDrop = True
End Sub
Public Sub SetBitmap(ByVal bitmap As Bitmap)
SetBitmap(bitmap, 255)
End Sub
Public Sub SetBitmap(ByVal bitmap As Bitmap, ByVal opacity As Byte)
If Not (bitmap.PixelFormat = PixelFormat.Format32bppArgb) Then
Throw New ApplicationException("The bitmap must be 32ppp with" & _
"alpha-channel.")
End If
'CurrBitmap = bitmap
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))
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(Left, 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(Handle, screenDc, topPos, size, memDc, _
pointSource, 0, blend, Win32.ULW_ALPHA)
Finally
Win32.ReleaseDC(IntPtr.Zero, screenDc)
If Not (hBitmap.Equals(IntPtr.Zero)) Then
Win32.SelectObject(memDc, oldBitmap)
Win32.DeleteObject(hBitmap)
End If
Win32.DeleteDC(memDc)
End Try
End Sub
Protected Overloads Overrides ReadOnly Property CreateParams() As _
CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or (524288)
Return cp
End Get
End Property
'Wird vom Windows Form-Designer benötigt.
Private components As System.ComponentModel.IContainer
' Hier bitte beliebigen Code, der Controls, eingeben
End Class Schreibt ruhig mal, ob es bei euch geht und ob man den Code noch verbessern könnte.
Vielleicht kennt jemand eine noch bessere Variante oder verändert all das hier.
Alles ist erlaubt! ;) |