Date: 02-16-2022
Return to Index
created by gbSnippets
'The Windows SystemParameterInfo API can be used to set the desktop
'wallpaper from a BMP file. It's often useful for an application to use an
'image of the dialog for the wallpaper - to capture whatever the application
'is showing for the user to have onscreen (until the wallpaper is changed again).
'Primary Code:
'Capturing the current dialog uses the keybd_event API:
keybd_event(%VK_SnapShot, 1, 0, 0) 'place active dialog on clipboard
'Whereas setting the wallpaper is done with the SystemParaemtersInfo API,
'using an image from the specified file.
SystemParametersInfo(%SPI_SETDESKWALLPAPER, 0&, tempFile , 0&) 'center of screen, no stretch
'One additional note - the result of the keybd_event function does not become
'available until the Callback function is exited. This requires the use of a Thread
'to allow a single button to BOTH capture the dialog and use the resulting image to set
'the wallpaper.
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10 (primary code placed in Sub)
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord, hThread as DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Dialog Capture",300,300,250,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Capture Dialog", 20,10,100,20
Control Add Button, hDlg, 200,"Set Wallpaper", 20,40,100,20
Control Add Button, hDlg, 300,"Capture/Set Wallpaper", 20,70,140,20
Control Add Button, hDlg, 400,"Remove Wallpaper", 20,100,200,20
Control Add Button, hDlg, 500,"Capture/Set Wallpaper (doesn't work)", 20,130,200,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_Command
Select Case CB.Ctl
Case 100 : CaptureDialog
Case 200 : SetWallpaper
Case 300 : CaptureDialog
Thread Create MyThread(0) To hThread 'start a new thread, argument not used
Thread Close hThread To hThread 'suggested by PowerBASIC Inc. as good practice
Case 400 : Local temp$
SystemParametersInfo(%SPI_SETDESKWALLPAPER, 0&, temp$, %SPIF_SENDCHANGE)
Case 500 : CaptureDialog
SetWallpaper
End Select
End Select
End Function
Thread Function MyThread (ByVal x As Long) As Long
' Sleep 500 'not required - just for visual offset
SetWallpaper
End Function
Sub CaptureDialog
'create file using captured image of the dialog
Clipboard Reset
keybd_event(%VK_SnapShot, 1, 0, 0) 'place active dialog on clipboard
End Sub
Sub SetWallpaper
Dim hTemp as DWord, iReturn& 'create handle for new memory Bitmap
Clipboard Get Bitmap hTemp, iReturn& 'create new memory Bitmap and put clipboard image into the bitmap
If iReturn& = 0 Then MsgBox "Error getting image from clipboard!" : Exit Sub
Graphic Attach hTemp, 0 'select Bitmap for Graphic statements
Graphic Save Exe.path$ + "gbsnippets_temp.bmp" 'save bitmap to file
Graphic Bitmap End 'release the memory bitmap
'now use file to create wallpaper
Dim tempFile As Asciiz * %Max_Path
tempFile = Exe.path$ + "gbsnippets_temp.bmp"
SystemParametersInfo(%SPI_SETDESKWALLPAPER, 0&, tempFile , 0&) 'put image in center of screen, no stretch
End Sub
Sub CaptureSetWallpaper
'create file using captured image of the dialog
Clipboard Reset
keybd_event(%VK_SnapShot, 1, 0, 0) 'place active dialog on clipboard
Dim hTemp as DWord, iReturn& 'create handle for new memory Bitmap
Clipboard Get Bitmap hTemp, iReturn& 'create new memory Bitmap and put clipboard image into the bitmap
If iReturn& = 0 Then MsgBox "Error getting image from clipboard!" : Exit Sub
Graphic Attach hTemp, 0 'select Bitmap for Graphic statements
Graphic Save Exe.path$ + "gbsnippets_temp.bmp" 'save bitmap to file
Graphic Bitmap End 'release the memory bitmap
'now use file to create wallpaper
Dim tempFile As Asciiz * %Max_Path
tempFile = Exe.path$ + "gbsnippets_temp.bmp"
SystemParametersInfo(%SPI_SETDESKWALLPAPER, 0&, tempFile , 0&) 'put image in center of screen, no stretch
End Sub
'gbs_00181
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm