Imports
System.Security
Imports System.Runtime.InteropServices
< SuppressUnmanagedCodeSecurity() > _
Friend NotInheritable Class UnsafeNativeMethods
< DllImport( " user32.dll " , CharSet: = CharSet.Auto) > _
Public Shared Function SystemParametersInfo( _
ByVal nAction As Integer , _
ByVal nParam As Integer , _
ByVal lpvParam As String , _
ByVal nUpdate As Integer _
) As Boolean
End Function
Public Const SPI_SETDESKWALLPAPER As Integer = & H14
Public Const SPIF_UPDATEINIFILE As Integer = & H1
Public Const SPIF_SENDWININICHANGE As Integer = & H2
End Class
Public Class DesktopWallpaper
Private Sub New ()
End Sub
Public Shared Function Clear( ByVal update As UpdateType) As Boolean
Return InternalChange( "" , update)
End Function
Public Shared Function Change( ByVal bmpFile As String , ByVal update As UpdateType) As Boolean
Return InternalChange(bmpFile, update)
End Function
Public Shared Function Change( ByVal path As String , ByVal bmpFilename As String , ByVal update As UpdateType) As Boolean
Dim bmpFile As String = Microsoft.VisualBasic.FileIO.FileSystem.CombinePath(path, bmpFilename)
Return InternalChange(bmpFile, update)
End Function
Private Shared Function InternalChange( ByVal bmpFile As String , ByVal update As UpdateType) As Boolean
Return UnsafeNativeMethods.SystemParametersInfo( _
UnsafeNativeMethods.SPI_SETDESKWALLPAPER, _
0 , _
bmpFile, _
GetUpdateValue(update))
End Function
Private Shared Function GetUpdateValue( ByVal update As UpdateType) As Integer
Select Case update
Case UpdateType.Save
Return UnsafeNativeMethods.SPIF_UPDATEINIFILE
Case UpdateType.Reflesh
Return UnsafeNativeMethods.SPIF_SENDWININICHANGE
Case UpdateType.SaveAndReflesh
Return UnsafeNativeMethods.SPIF_UPDATEINIFILE Or UnsafeNativeMethods.SPIF_SENDWININICHANGE
End Select
End Function
Public Enum UpdateType
' '' <summary>
' '' 保存设置,重启系统后有效
' '' </summary>
Save = 1
' '' <summary>
' '' 更新设置,本次有效,重启系统后无效
' '' </summary>
Reflesh = 2
' '' <summary>
' '' 保存设置并生效
' '' </summary>
SaveAndReflesh = 3
End Enum
End Class
' 没全部测试。在我的简单测试中,SaveOnly都会Reflesh了。
Public Class Demo
Public Shared Sub RefleshOnly()
DesktopWallpaper.Change( " c: /test.bmp " , DesktopWallpaper.UpdateType.Reflesh)
End Sub
Public Shared Sub SaveOnly()
DesktopWallpaper.Change( " c:/ test.bmp " , DesktopWallpaper.UpdateType.Save)
End Sub
Public Shared Sub Update()
DesktopWallpaper.Change( " c:/ test.bmp " , DesktopWallpaper.UpdateType.SaveAndReflesh)
End Sub
Public Shared Sub Clear()
DesktopWallpaper.Clear(DesktopWallpaper.UpdateType.SaveAndReflesh)
End Sub
End Class
Imports System.Runtime.InteropServices
< SuppressUnmanagedCodeSecurity() > _
Friend NotInheritable Class UnsafeNativeMethods
< DllImport( " user32.dll " , CharSet: = CharSet.Auto) > _
Public Shared Function SystemParametersInfo( _
ByVal nAction As Integer , _
ByVal nParam As Integer , _
ByVal lpvParam As String , _
ByVal nUpdate As Integer _
) As Boolean
End Function
Public Const SPI_SETDESKWALLPAPER As Integer = & H14
Public Const SPIF_UPDATEINIFILE As Integer = & H1
Public Const SPIF_SENDWININICHANGE As Integer = & H2
End Class
Public Class DesktopWallpaper
Private Sub New ()
End Sub
Public Shared Function Clear( ByVal update As UpdateType) As Boolean
Return InternalChange( "" , update)
End Function
Public Shared Function Change( ByVal bmpFile As String , ByVal update As UpdateType) As Boolean
Return InternalChange(bmpFile, update)
End Function
Public Shared Function Change( ByVal path As String , ByVal bmpFilename As String , ByVal update As UpdateType) As Boolean
Dim bmpFile As String = Microsoft.VisualBasic.FileIO.FileSystem.CombinePath(path, bmpFilename)
Return InternalChange(bmpFile, update)
End Function
Private Shared Function InternalChange( ByVal bmpFile As String , ByVal update As UpdateType) As Boolean
Return UnsafeNativeMethods.SystemParametersInfo( _
UnsafeNativeMethods.SPI_SETDESKWALLPAPER, _
0 , _
bmpFile, _
GetUpdateValue(update))
End Function
Private Shared Function GetUpdateValue( ByVal update As UpdateType) As Integer
Select Case update
Case UpdateType.Save
Return UnsafeNativeMethods.SPIF_UPDATEINIFILE
Case UpdateType.Reflesh
Return UnsafeNativeMethods.SPIF_SENDWININICHANGE
Case UpdateType.SaveAndReflesh
Return UnsafeNativeMethods.SPIF_UPDATEINIFILE Or UnsafeNativeMethods.SPIF_SENDWININICHANGE
End Select
End Function
Public Enum UpdateType
' '' <summary>
' '' 保存设置,重启系统后有效
' '' </summary>
Save = 1
' '' <summary>
' '' 更新设置,本次有效,重启系统后无效
' '' </summary>
Reflesh = 2
' '' <summary>
' '' 保存设置并生效
' '' </summary>
SaveAndReflesh = 3
End Enum
End Class
' 没全部测试。在我的简单测试中,SaveOnly都会Reflesh了。
Public Class Demo
Public Shared Sub RefleshOnly()
DesktopWallpaper.Change( " c: /test.bmp " , DesktopWallpaper.UpdateType.Reflesh)
End Sub
Public Shared Sub SaveOnly()
DesktopWallpaper.Change( " c:/ test.bmp " , DesktopWallpaper.UpdateType.Save)
End Sub
Public Shared Sub Update()
DesktopWallpaper.Change( " c:/ test.bmp " , DesktopWallpaper.UpdateType.SaveAndReflesh)
End Sub
Public Shared Sub Clear()
DesktopWallpaper.Clear(DesktopWallpaper.UpdateType.SaveAndReflesh)
End Sub
End Class