先声明,看不懂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
1<structlayout(layoutkind.sequential)> _
2 Public Structure _tagCOMPSTATEINFO
3 Private dwSize As Integer
4 Private iLeft As Integer
5 Private iTop As Integer
6 Private dwWidth As Integer
7 Private dwHeight As Integer
8 Private dwItemState As Integer
9 End Structure
10 <structlayout(layoutkind.sequential)> _
11 Public Structure _tagCOMPPOS
12 Private dwSize As Integer
13 Private iLeft As Integer
14 Private iTop As Integer
15 Private dwWidth As Integer
16 Private dwHeight As Integer
17 Private izIndex As Integer
18 <marshalas(unmanagedtype.bool)> _
19 Private fCanResize As Boolean
20 <marshalas(unmanagedtype.bool)> _
21 Private fCanResizeX As Boolean
22 <marshalas(unmanagedtype.bool)> _
23 Private fCanResizeY As Boolean
24 Private iPreferredLeftPercent As Integer
25 Private iPreferredTopPercent As Integer
26 End Structure
27 <structlayout(layoutkind.sequential)> _
28 Public Structure _tagCOMPONENT
29 Public dwSize As Integer
30 Public dwID As Integer
31 Public iComponentType As Integer
32 <marshalas(unmanagedtype.bool)> _
33 Public fChecked As Boolean
34 <marshalas(unmanagedtype.bool)> _
35 Public fDirty As Boolean
36 <marshalas(unmanagedtype.bool)> _
37 Public fNoScroll As Boolean
38 Public cpPos As _tagCOMPPOS
39 <marshalas(unmanagedtype.byvaltstr, sizeconst:="MAX_PATH)"> _
40 Public wszFriendlyName As String
41 <marshalas(unmanagedtype.byvaltstr, sizeconst:="2084)"> _
42 Public wszSource As String
43 <marshalas(unmanagedtype.byvaltstr, sizeconst:="2084)"> _
44 Public wszSubscribedURL As String
45 Public dwCurItemState As Integer
46 Public csiOriginal As _tagCOMPSTATEINFO
47 Public csiRestored As _tagCOMPSTATEINFO
48 End Structure
49 <structlayout(layoutkind.sequential)> _
50 Public Structure _tagCOMPONENTSOPT
51 Public dwSize As Integer
52 <marshalas(unmanagedtype.bool)> _
53 Public fEnableComponents As Boolean
54 <marshalas(unmanagedtype.bool)> _
55 Public fActiveDesktop As Boolean
56 End Structure
57 <structlayout(layoutkind.sequential)> _
58 Public Structure _tagWALLPAPEROPT
59 Public dwSize As Integer
60 Public dwStyle As Integer
61 End Structure
62 End Class
63
64 申明IActiveDesktop对象
65
66
67
68 <comimport(), guid("f490eb00-1240-11d1-9888-006097deacf9"),="" interfacetype(cominterfacetype.interfaceisiunknown)=""> _
69 Public Interface IActiveDesktop
70 Function ApplyChanges(ByVal dwFlags As Integer) As IntPtr
71 Function GetWallpaper(<marshalas(unmanagedtype.lpwstr)> ByVal pwszWallpaper As System.Text.StringBuilder, ByVal cchWallpaper As Integer, ByVal dwReserved As Integer) As IntPtr
72 Function SetWallpaper(<marshalas(unmanagedtype.lpwstr)> ByVal pwszWallpaper As String, ByVal dwReserved As Integer) As IntPtr
73 Function GetWallpaperOptions(ByRef pwpo As _win32._tagWALLPAPEROPT, ByVal dwReserved As Integer) As IntPtr
74 Function SetWallpaperOptions(<[In]()> ByRef pwpo As _win32._tagWALLPAPEROPT, ByVal dwReserved As Integer) As IntPtr
75 Function GetPattern(<marshalas(unmanagedtype.lpwstr)> ByVal pwszPattern As System.Text.StringBuilder, ByVal cchPattern As Integer, ByVal dwReserved As Integer) As IntPtr
76 Function SetPattern(<marshalas(unmanagedtype.lpwstr)> ByVal pwszPattern As String, ByVal dwReserved As Integer) As IntPtr
77 Function GetDesktopItemOptions(ByRef pco As _win32._tagCOMPONENTSOPT, ByVal dwReserved As Integer) As IntPtr
78 Function SetDesktopItemOptions(<[In]()> ByRef pcomp As _win32._tagCOMPONENTSOPT, ByVal dwReserved As Integer) As IntPtr
79 Function AddDesktopItem(<[In]()> ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
80 Function AddDesktopItemWithUI(ByVal hwnd As IntPtr, <[In]()> ByRef pcomp As _win32._tagCOMPONENT, ByVal dwFlags As Integer) As IntPtr
81 Function ModifyDesktopItem(<[In]()> ByRef pcomp As _win32._tagCOMPONENT, ByVal dwFlags As Integer) As IntPtr
82 Function RemoveDesktopItem(<[In]()> ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
83 Function GetDesktopItemCount(ByRef lpiCount As Integer, ByVal dwReserved As Integer) As IntPtr
84 Function GetDesktopItem(ByVal nComponent As Integer, ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
85 Function GetDesktopItemByID(ByVal dwID As IntPtr, ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
86 Function GenerateDesktopItemHtml(<marshalas(unmanagedtype.lpwstr)> ByVal pwszFileName As String, <[In]()> ByRef pcomp As _win32._tagCOMPPOS, ByVal dwReserved As Integer) As IntPtr
87 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
88 Function GetDesktopItemBySource(<marshalas(unmanagedtype.lpwstr)> ByVal pszSource As String, ByRef pcomp As _win32._tagCOMPONENT, ByVal dwReserved As Integer) As IntPtr
89 End Interface
90
91
92
93
94
95现在开始使用这个对象。在使用之前要用 CoCreateObject 来进行创建。
96
97
98
99 <dllimport("ole32.dll")> _
100 Public Shared Function CoCreateInstance( _
101 ByRef ClassGuid As Guid, _
102 ByVal pUnkOuter As IntPtr, _
103 ByVal dwClsContext As Integer, _
104 ByRef InterfaceGuid As Guid, _
105 ByRef Result As IActiveDesktop) As IntPtr
106 End Function
107
108 Private CLSID_ActiveDesktop As New Guid("75048700-EF1F-11D0-9888-006097DEACF9")
109
110 Private IID_IActiveDesktop As New Guid("F490EB00-1240-11D1-9888-006097DEACF9")
111
112 Private Const CLSCTX_INPROC_SERVER As Integer = 1
113
114 Private ActiveDesktop As IActiveDesktop
115
116 下面的语句就是用来创建IActiveDesktop对象的。
117 CoCreateInstance(CLSID_ActiveDesktop,IntPtr.Zero, CLSCTX_INPROC_SERVER,IID_IActiveDesktop, ActiveDesktop)
118
119
120
121 释放该对象:
122 Marshal.ReleaseComObject(ActiveDesktop)
123
124 这里的俩条语句是用来在桌面上保存图片的。
125 ActiveDesktop.SetWallpaper(PicturePathName, 0)
126 ActiveDesktop.ApplyChanges(_win32.AD_APPLY_FORCE Or _win32.AD_APPLY_SAVE Or _win32.AD_APPLY_REFRESH)
127
128
129* * *</dllimport("ole32.dll")></marshalas(unmanagedtype.lpwstr)></marshalas(unmanagedtype.lpwstr)></marshalas(unmanagedtype.lpwstr)></marshalas(unmanagedtype.lpwstr)></marshalas(unmanagedtype.lpwstr)></marshalas(unmanagedtype.lpwstr)></marshalas(unmanagedtype.lpwstr)></comimport(),></structlayout(layoutkind.sequential)></marshalas(unmanagedtype.bool)></marshalas(unmanagedtype.bool)></structlayout(layoutkind.sequential)></marshalas(unmanagedtype.byvaltstr,></marshalas(unmanagedtype.byvaltstr,></marshalas(unmanagedtype.byvaltstr,></marshalas(unmanagedtype.bool)></marshalas(unmanagedtype.bool)></marshalas(unmanagedtype.bool)></structlayout(layoutkind.sequential)></marshalas(unmanagedtype.bool)></marshalas(unmanagedtype.bool)></marshalas(unmanagedtype.bool)></structlayout(layoutkind.sequential)></structlayout(layoutkind.sequential)>