Rubrik: System/Windows · Desktop/Bildschirm/Display | VB-Versionen: VB4, VB5, VB6 | 22.03.05 |
Grafiken auf den Desktop projezieren Mit diesem Tipp ist es möglich, direkt auf dem Desktop zu "zeichnen" | ||
Autor: Martin Walcher | Bewertung: | Views: 15.946 |
www.martinwalcher.de | System: Win9x, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Unser heutiger beschreibt, wie man eine Grafik auf den Windows-Desktop projeziert und den Desktop danach wieder "cleanen" kann. Auf diese Weise kann man zur Weihnachtszeit bspw. Schneeflocken über den Desktop schneien lassen
Zuständig für das Projezieren der Grafik ist unsere Funktion DrawItem. Die Funktion selbst erwartet als Parameter eine PictureBox (AutoRedraw = True), deren Inhalt dann auf den Desktop zu zeichnen ist, sowie optional die Zielposition an der das Bild erscheinen soll. Dabei werden die Positionen 0 bis 1 automatisch an die aktuelle Bildschirmgröße angepasst. Der vierte Parameter legt die Farbe fest, die beim zeichnen transparent erscheinen soll. Standardmäßig wird Magenta als transparente Farbe verwendet. Über die Funktion ClearScreen werden alle auf den Bildschirm gezeichneten Symbole wieder gelöscht.
Erstellen Sie ein neues Projekt und platzieren auf die Form eine PictureBox, sowie ein Timer-Control. Laden Sie in die PictureBox ein Bildsymbol ihrer Wahl.
Im Allgemein-Teil der Form müssen folgende Deklarationen eingefügt werden:
Option Explicit ' Benötigte API-Deklarationen Private Declare Function GetDesktopWindow Lib "user32.dll" () As Long Private Declare Function GetDC Lib "user32.dll" ( _ ByVal hwnd As Long) As Long Private Declare Function GetWindowRect Lib "user32.dll" ( _ ByVal hwnd As Long, _ ByRef lpRect As RECT) As Long Private Declare Function TransparentBlt Lib "msimg32.dll" ( _ ByVal hdc As Long, _ ByVal x As Long, _ ByVal y As Long, _ ByVal nWidth As Long, _ ByVal nHeight As Long, _ ByVal hSrcDC As Long, _ ByVal xSrc As Long, _ ByVal ySrc As Long, _ ByVal nSrcWidth As Long, _ ByVal nSrcHeight As Long, _ ByVal crTransparent As Long) As Long Private Declare Function InvalidateRect Lib "user32.dll" ( _ ByVal hwnd As Long, _ lpRect As Any, _ ByVal bErase As Long) As Long Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
Wie eingangs erwähnt ist unsere Funktion DrawItem für das Projezieren des Bildes zuständig:
Private Function DrawItem(varItem As PictureBox, _ Optional ByVal x As Single = -1, _ Optional ByVal y As Single = -1, _ Optional ByVal lngTransparency As ColorConstants = vbMagenta) Dim hwnd As Long Dim dc As Long Dim varScreen As RECT Dim varPicture As RECT ' Handle des Windows-Desktop ermitteln hwnd = GetDesktopWindow() dc = GetDC(0) ' Größe des Desktopbereichs ermitteln GetWindowRect hwnd, varScreen ' Größe der PictureBox ermitteln GetWindowRect varItem.hwnd, varPicture With varPicture .Right = .Right - .Left .Bottom = .Bottom - .Top varScreen.Right = varScreen.Right - .Right + 1 varScreen.Bottom = varScreen.Bottom - .Bottom + 1 End With ' Falls x und y nicht angegeben wurden, Position zufällig ermitteln If x = -1 Then x = Rnd If y = -1 Then y = Rnd With varPicture DrawItem = (TransparentBlt(dc, varScreen.Right * x, varScreen.Bottom * y, _ .Right, .Bottom, varItem.hdc, 0, 0, .Right, .Bottom, lngTransparency) = 1) End With End Function
Das Wiederherstellen des Desktops erledigt unsere Funktion ClearScreen:
Private Function ClearScreen() ClearScreen = (InvalidateRect(0, 0&, 0) = 1) End Function
Mit Hilfe des Timers wird unser Bild nun an beliebigen Positionen des Desktops gezeichnet:
Private Sub Form_Load() ' Timer-Intervall festlegen Timer1.Interval = 150 Timer1.Enabled = True End Sub
Private Sub Timer1_Timer() DrawItem Picture1 End Sub
Beim Beenden der Anwendung soll der Desktop wieder gesäubert werden:
Private Sub Form_Unload(Cancel As Integer) ClearScreen End Sub
So... und nun viel Spaß damit