先声明,看不懂VB的别在这里乱叫,烦。这里是给大家例子看的。不是帮你写代码让你COPY了就能用的。
Windows32使用的一些常量定义:
Public Class _win32
Public Const AD_APPLY_ALL As Integer = AD_APPLY_SAVE Or AD_APPLY_HTMLGEN Or AD_APPLY_REFRESH
Public Const AD_APPLY_BUFFERED_REFRESH As Integer = &H10
Public Const AD_APPLY_DYNAMICREFRESH As Integer = &H20
Public Const AD_APPLY_FORCE As Integer = &H8
Public Const AD_APPLY_HTMLGEN As Integer = &H2
Public Const AD_APPLY_REFRESH As Integer = &H4
Public Const AD_APPLY_SAVE As Integer = &H1
Public Const COMP_ELEM_ALL As Integer = COMP_ELEM_TYPE Or COMP_ELEM_CHECKED Or COMP_ELEM_DIRTY Or COMP_ELEM_NOSCROLL Or COMP_ELEM_POS_LEFT Or COMP_ELEM_SIZE_WIDTH Or COMP_ELEM_SIZE_HEIGHT Or COMP_ELEM_POS_ZINDEX Or COMP_ELEM_SOURCE Or COMP_ELEM_FRIENDLYNAME Or COMP_ELEM_POS_TOP Or COMP_ELEM_SUBSCRIBEDURL Or COMP_ELEM_ORIGINAL_CSI Or COMP_ELEM_RESTORED_CSI Or COMP_ELEM_CURITEMSTATE
Public Const COMP_ELEM_CHECKED As Integer = &H2
Public Const COMP_ELEM_CURITEMSTATE As Integer = &H4000
Public Const COMP_ELEM_DIRTY As Integer = &H4
Public Const COMP_ELEM_FRIENDLYNAME As Integer = &H400
Public Const COMP_ELEM_NOSCROLL As Integer = &H8
Public Const COMP_ELEM_ORIGINAL_CSI As Integer = &H1000
Public Const COMP_ELEM_POS_LEFT As Integer = &H10
Public Const COMP_ELEM_POS_TOP As Integer = &H20
Public Const COMP_ELEM_POS_ZINDEX As Integer = &H100
Public Const COMP_ELEM_RESTORED_CSI As Integer = &H2000
Public Const COMP_ELEM_SIZE_HEIGHT As Integer = &H80
Public Const COMP_ELEM_SIZE_WIDTH As Integer = &H40
Public Const COMP_ELEM_SOURCE As Integer = &H200
Public Const COMP_ELEM_TYPE As Integer = &H1
Public Const COMP_ELEM_SUBSCRIBEDURL As Integer = &H800
Public Const COMP_TYPE_CONTROL As Integer = 3
Public Const COMP_TYPE_HTMLDOC As Integer = 0
Public Const COMP_TYPE_PICTURE As Integer = 1
Public Const COMP_TYPE_WEBSITE As Integer = 2
Public Const COMPONENT_DEFAULT_LEFT As Integer = &HFFFF
Public Const COMPONENT_DEFAULT_TOP As Integer = &HFFFF
Public Const IS_FULLSCREEN As Integer = &H2
Public Const IS_NORMAL As Integer = &H1
Public Const IS_SPLIT As Integer = &H4
Public Const MAX_PATH As Integer = 260
Public Const WPSTYLE_CENTER As Integer = 0
Public Const WPSTYLE_MAX As Integer = 3
Public Const WPSTYLE_STRETCH As Integer = 2
Public Const WPSTYLE_TILE As Integer = 1
<StructLayout(LayoutKind.Sequential)> _
Public Structure _tagCOMPSTATEINFO
Private dwSize As Integer
Private iLeft As Integer
Private iTop As Integer
Private dwWidth As Integer
Private dwHeight As Integer
Private dwItemState As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure _tagCOMPPOS
Private dwSize As Integer
Private iLeft As Integer
Private iTop As Integer
Private dwWidth As Integer
Private dwHeight As Integer
Private izIndex As Integer
<MarshalAs(UnmanagedType.Bool)> _
Private fCanResize As Boolean
<MarshalAs(UnmanagedType.Bool)> _
Private fCanResizeX As Boolean
<MarshalAs(UnmanagedType.Bool)> _
Private fCanResizeY As Boolean
Private iPreferredLeftPercent As Integer
Private iPreferredTopPercent As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure _tagCOMPONENT
Public dwSize As Integer
Public dwID As Integer
Public iComponentType As Integer
<MarshalAs(UnmanagedType.Bool)> _
Public fChecked As Boolean
<MarshalAs(UnmanagedType.Bool)> _
Public fDirty As Boolean
<MarshalAs(UnmanagedType.Bool)> _
Public fNoScroll As Boolean
Public cpPos As _tagCOMPPOS
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> _
Public wszFriendlyName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2084)> _
Public wszSource As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2084)> _
Public wszSubscribedURL As String
Public dwCurItemState As Integer
Public csiOriginal As _tagCOMPSTATEINFO
Public csiRestored As _tagCOMPSTATEINFO
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure _tagCOMPONENTSOPT
Public dwSize As Integer
<MarshalAs(UnmanagedType.Bool)> _
Public fEnableComponents As Boolean
<MarshalAs(UnmanagedType.Bool)> _
Public fActiveDesktop As Boolean
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure _tagWALLPAPEROPT
Public dwSize As Integer
Public dwStyle As Integer
End Structure
End Class申明IActiveDesktop对象
<ComImport(), Guid("F490EB00-1240-11D1-9888-006097DEACF9"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IActiveDesktop
Function ApplyChanges(ByVal dwFlags As Integer) As IntPtr
Function GetWallpaper(<MarshalAs(UnmanagedType.LPWStr)> ByVal pwszWallpaper As System.Text.StringBuilder, ByVal cchWallpaper As Integer, ByVal dwReserved As Integer) As IntPtr
Function SetWallpaper(<MarshalAs(UnmanagedType.LPWStr)> ByVal pwszWallpaper As String, ByVal dwReserved As Integer) As IntPtr
Function GetWallpaperOptions(ByRef pwpo As _win32._tagWALLPAPEROPT, ByVal dwReserved As Integer) As IntPtr
Function SetWallpaperOptions(<[In]()> ByRef pwpo As _win32._tagWALLPAPEROPT, ByVal dwReserved As Integer) As IntPtr
Function GetPattern(<MarshalAs(UnmanagedType.LPWStr)> ByVal pwszPattern As System.Text.StringBuilder, ByVal cchPattern As Integer, ByVal dwReserved As Integer) As IntPtr
Function SetPattern(<MarshalAs(UnmanagedType.LPWStr)> ByVal pwszPattern As String, ByVal dwReserved As Integer) As IntPtr
Function GetDesktopItemOptions(ByRef pco As _win32._tagCOMPONENTSOPT, ByVal dwReserved As Integer) As IntPtr
Function SetDesktopItemOptions(<[In]()> ByRef pcomp As _win32._tagCOMPONENTSOPT, ByVal dwReserved As Integer) As IntPtr
Function AddDesktopItem(<[In]()> ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
Function AddDesktopItemWithUI(ByVal hwnd As IntPtr, <[In]()> ByRef pcomp As _win32._tagCOMPONENT, ByVal dwFlags As Integer) As IntPtr
Function ModifyDesktopItem(<[In]()> ByRef pcomp As _win32._tagCOMPONENT, ByVal dwFlags As Integer) As IntPtr
Function RemoveDesktopItem(<[In]()> ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
Function GetDesktopItemCount(ByRef lpiCount As Integer, ByVal dwReserved As Integer) As IntPtr
Function GetDesktopItem(ByVal nComponent As Integer, ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
Function GetDesktopItemByID(ByVal dwID As IntPtr, ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
Function GenerateDesktopItemHtml(<MarshalAs(UnmanagedType.LPWStr)> ByVal pwszFileName As String, <[In]()> ByRef pcomp As _win32._tagCOMPPOS, ByVal dwReserved As Integer) As IntPtr
Function AddUrl(ByVal hwnd As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal pszSource As String, <[In]()> ByRef pcomp As _win32._tagCOMPONENT, ByVal dwFlags As Integer) As IntPtr
Function GetDesktopItemBySource(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszSource As String, ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
End Interface
现在开始使用这个对象。在使用之前要用CoCreateObject来进行创建。
<DllImport("OLE32.DLL")> _
Public Shared Function CoCreateInstance( _
ByRef ClassGuid As Guid, _
ByVal pUnkOuter As IntPtr, _
ByVal dwClsContext As Integer, _
ByRef InterfaceGuid As Guid, _
ByRef Result As IActiveDesktop) As IntPtr
End Function
Private CLSID_ActiveDesktop As New Guid("75048700-EF1F-11D0-9888-006097DEACF9")
Private IID_IActiveDesktop As New Guid("F490EB00-1240-11D1-9888-006097DEACF9")
Private Const CLSCTX_INPROC_SERVER As Integer = 1
Private ActiveDesktop As IActiveDesktop
下面的语句就是用来创建IActiveDesktop对象的。
CoCreateInstance(CLSID_ActiveDesktop,IntPtr.Zero, CLSCTX_INPROC_SERVER,IID_IActiveDesktop, ActiveDesktop)
释放该对象:
Marshal.ReleaseComObject(ActiveDesktop)
这里的俩条语句是用来在桌面上保存图片的。
ActiveDesktop.SetWallpaper(PicturePathName, 0)
ActiveDesktop.ApplyChanges(_win32.AD_APPLY_FORCE Or _win32.AD_APPLY_SAVE Or _win32.AD_APPLY_REFRESH)