c# 使用ActiveDesktop实现壁纸切换,适合大部分图片格式

我的广告单元,有空点一下哦,谢谢!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
操作步骤:
 
首先添加类:
 
using System;
using System.Runtime.InteropServices;
 
namespace WorkHelper
{
    enum WPSTYLE
    {
        CENTER = 0,
        TILE = 1,
        STRETCH = 2,
        MAX = 3
    }
 
    struct WALLPAPEROPT
    {
        public int dwSize;
        public WPSTYLE dwStyle;
    }
 
    struct COMPONENTSOPT
    {
        public int dwSize;
        [MarshalAs(UnmanagedType.Bool)]
        public bool fEnableComponents;
        [MarshalAs(UnmanagedType.Bool)]
        public bool fActiveDesktop;
    }
 
    struct COMPPOS
    {
        public const int COMPONENT_TOP = 0x3FFFFFFF;
        public const int COMPONENT_DEFAULT_LEFT = 0xFFFF;
        public const int COMPONENT_DEFAULT_TOP = 0xFFFF;
 
        public int dwSize;
        public int iLeft;
        public int iTop;
        public int dwWidth;
        public int dwHeight;
        public int izIndex;
        [MarshalAs(UnmanagedType.Bool)]
        public bool fCanResize;
        [MarshalAs(UnmanagedType.Bool)]
        public bool fCanResizeX;
        [MarshalAs(UnmanagedType.Bool)]
        public bool fCanResizeY;
        public int iPreferredLeftPercent;
        public int iPreferredTopPercent;
    }
 
    [Flags]
    enum ITEMSTATE
    {
        NORMAL = 0x00000001,
        FULLSCREEN = 00000002,
        SPLIT = 0x00000004,
        VALIDSIZESTATEBITS = NORMAL | SPLIT | FULLSCREEN,
        VALIDSTATEBITS = NORMAL | SPLIT | FULLSCREEN | unchecked((int)0x80000000) | 0x40000000
    }
 
    struct COMPSTATEINFO
    {
        public int dwSize;
        public int iLeft;
        public int iTop;
        public int dwWidth;
        public int dwHeight;
        public int dwItemState;
    }
 
    enum COMP_TYPE
    {
        HTMLDOC = 0,
        PICTURE = 1,
        WEBSITE = 2,
        CONTROL = 3,
        CFHTML = 4,
        MAX = 4
    }
 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct COMPONENT
    {
        private const int INTERNET_MAX_URL_LENGTH = 2084;   //   =  
        // INTERNET_MAX_SCHEME_LENGTH   (32)   +   "://\0".Length   +  
        // INTERNET_MAX_PATH_LENGTH   (2048) 
 
        public int dwSize;
        public int dwID;
        public COMP_TYPE iComponentType;
        [MarshalAs(UnmanagedType.Bool)]
        public bool fChecked;
        [MarshalAs(UnmanagedType.Bool)]
        public bool fDirty;
        [MarshalAs(UnmanagedType.Bool)]
        public bool fNoScroll;
        public COMPPOS cpPos;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string wszFriendlyName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = INTERNET_MAX_URL_LENGTH)]
        public string wszSource;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = INTERNET_MAX_URL_LENGTH)]
        public string wszSubscribedURL;
 
#if   AD_IE5  
      public   int   dwCurItemState;  
      public   COMPSTATEINFO   csiOriginal;  
      public   COMPSTATEINFO   csiRestored;  
#endif
    }
 
    enum DTI_ADTIWUI
    {
        DTI_ADDUI_DEFAULT = 0x00000000,
        DTI_ADDUI_DISPSUBWIZARD = 0x00000001,
        DTI_ADDUI_POSITIONITEM = 0x00000002,
    }
 
    [Flags]
    enum AD_APPLY
    {
        SAVE = 0x00000001,
        HTMLGEN = 0x00000002,
        REFRESH = 0x00000004,
        ALL = SAVE | HTMLGEN | REFRESH,
        FORCE = 0x00000008,
        BUFFERED_REFRESH = 0x00000010,
        DYNAMICREFRESH = 0x00000020
    }
 
    [Flags]
    enum COMP_ELEM
    {
        TYPE = 0x00000001,
        CHECKED = 0x00000002,
        DIRTY = 0x00000004,
        NOSCROLL = 0x00000008,
        POS_LEFT = 0x00000010,
        POS_TOP = 0x00000020,
        SIZE_WIDTH = 0x00000040,
        SIZE_HEIGHT = 0x00000080,
        POS_ZINDEX = 0x00000100,
        SOURCE = 0x00000200,
        FRIENDLYNAME = 0x00000400,
        SUBSCRIBEDURL = 0x00000800,
        ORIGINAL_CSI = 0x00001000,
        RESTORED_CSI = 0x00002000,
        CURITEMSTATE = 0x00004000,
        ALL = TYPE | CHECKED | DIRTY | NOSCROLL | POS_LEFT | SIZE_WIDTH |
            SIZE_HEIGHT | POS_ZINDEX | SOURCE |
            FRIENDLYNAME | POS_TOP | SUBSCRIBEDURL | ORIGINAL_CSI |
            RESTORED_CSI | CURITEMSTATE
    }
 
    [Flags]
    enum ADDURL
    {
        SILENT = 0x0001
    }
 
    [
        ComImport(),
        Guid("F490EB00-1240-11D1-9888-006097DEACF9"),
        InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
    ]
    interface IActiveDesktop
    {
        void ApplyChanges(AD_APPLY dwFlags);
        void GetWallpaper([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pwszWallpaper, int cchWallpaper, int dwReserved);
        void SetWallpaper([MarshalAs(UnmanagedType.LPWStr)]   string pwszWallpaper, int dwReserved);
        void GetWallpaperOptions(ref   WALLPAPEROPT pwpo, int dwReserved);
        void SetWallpaperOptions([In]   ref   WALLPAPEROPT pwpo, int dwReserved);
        void GetPattern([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pwszPattern, int cchPattern, int dwReserved);
        void SetPattern([MarshalAs(UnmanagedType.LPWStr)]   string pwszPattern, int dwReserved);
        void GetDesktopItemOptions(ref   COMPONENTSOPT pco, int dwReserved);
        void SetDesktopItemOptions([In]   ref   COMPONENTSOPT pco, int dwReserved);
        void AddDesktopItem([In]   ref   COMPONENT pcomp, int dwReserved);
        void AddDesktopItemWithUI(IntPtr hwnd, [In]   ref   COMPONENT pcomp, DTI_ADTIWUI dwFlags);
        void ModifyDesktopItem([In]   ref   COMPONENT pcomp, COMP_ELEM dwFlags);
        void RemoveDesktopItem([In]   ref   COMPONENT pcomp, int dwReserved);
        void GetDesktopItemCount(out   int lpiCount, int dwReserved);
        void GetDesktopItem(int nComponent, ref   COMPONENT pcomp, int dwReserved);
        void GetDesktopItemByID(IntPtr dwID, ref   COMPONENT pcomp, int dwReserved);
        void GenerateDesktopItemHtml([MarshalAs(UnmanagedType.LPWStr)] string pwszFileName, [In]   ref   COMPONENT pcomp, int dwReserved);
        void AddUrl(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]   string pszSource, [In]   ref   COMPONENT pcomp, ADDURL dwFlags);
        void GetDesktopItemBySource([MarshalAs(UnmanagedType.LPWStr)] string pwszSource, ref   COMPONENT pcomp, int dwReserved);
    }
 
    [
        ComImport(),
        Guid("75048700-EF1F-11D0-9888-006097DEACF9")
    ]
    class ActiveDesktop   /*   :   IActiveDesktop   */   { }
}
 
 
调用:(终于搞定背景图片样式了)
 
public bool SetDeskBg(String filename)
        {
            try
            {
                ActiveDesktop ad = new ActiveDesktop();
                IActiveDesktop iad = ad as IActiveDesktop;
                if (iad != null)
                {
                    //WPSTYLE_CENTER 居中 0
                    //WPSTYLE_TILE 平铺 1
                    //WPSTYLE_STRETCH 拉伸 2
                    WPSTYLE dwstyle = WPSTYLE.STRETCH;//这里调整显示背景图片样式
                    WALLPAPEROPT wp = new WALLPAPEROPT();
                    wp.dwSize = System.Runtime.InteropServices.Marshal.SizeOf(wp);
                    wp.dwStyle = dwstyle;
                    iad.SetWallpaperOptions(ref wp, 0);
 
                    iad.SetWallpaper(filename, 0);
                    iad.ApplyChanges(AD_APPLY.ALL);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(ad);
                    ad = null;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
posted @   jackchain  阅读(755)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示