Back

private ActiveDesktop dummy;
private IActiveDesktop IActive;
private void button1_Click(object sender, System.EventArgs e)
{
dummy = new ActiveDesktop();
IActive = (IActiveDesktop)dummy; // cast an instance of the IActiveDesktop from the ActiveDesktop Class.

System.Text.StringBuilder sb=new System.Text.StringBuilder(256);
IActive.GetWallpaper(sb , sb.Capacity,0);
MessageBox.Show(sb.ToString());
}

private void button2_Click(object sender, System.EventArgs e)
{
IActive.SetWallpaper(textBox2.Text,0);
IActive.ApplyChanges(_win32.AD_APPLY_FORCE | _win32.AD_APPLY_SAVE | _win32.AD_APPLY_REFRESH);
}

#region ActiveDesktop class
 
[ComImport(),Guid("75048700-EF1F-11D0-9888-006097DEACF9")]
public class ActiveDesktop
{
// dummy to enable access to the IActiveDesktop
}
#endregion
 #region IActiveDesktop interface
 
[ComImport(),Guid("F490EB00-1240-11D1-9888-006097DEACF9"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IActiveDesktop
{
IntPtr ApplyChanges( int dwFlags
); //HRESULT ApplyChanges(DWORD dwFlags);

IntPtr GetWallpaper([MarshalAs(UnmanagedType.LPWStr)]System.Text.StringBuilder pwszWallpaper,
int cchWallpaper,
int dwReserved
); // HRESULT GetWallpaper(LPWSTR pwszWallpaper,UINT cchWallpaper,DWORD dwReserved);

IntPtr SetWallpaper([MarshalAs(UnmanagedType.LPWStr)]string pwszWallpaper,
int dwReserved
); // HRESULT SetWallpaper(LPCWSTR pwszWallpaper,DWORD dwReserved);

IntPtr GetWallpaperOptions(ref _win32._tagWALLPAPEROPT pwpo,
int dwReserved
); // HRESULT GetWallpaperOptions(LPWALLPAPEROPT pwpo,DWORD dwReserved);

IntPtr SetWallpaperOptions([In] ref _win32._tagWALLPAPEROPT pwpo,
int dwReserved
); // HRESULT SetWallpaperOptions(LPCWALLPAPEROPT pwpo,DWORD dwReserved);

IntPtr GetPattern([MarshalAs(UnmanagedType.LPWStr)]System.Text.StringBuilder pwszPattern,
int cchPattern,
int dwReserved
); //HRESULT GetPattern(LPWSTR pwszPattern,UINT cchPattern,DWORD dwReserved);

IntPtr SetPattern([MarshalAs(UnmanagedType.LPWStr)]string pwszPattern,
int dwReserved
); // HRESULT SetPattern(LPCWSTR pwszPattern,DWORD dwReserved);

IntPtr GetDesktopItemOptions(ref _win32._tagCOMPONENTSOPT pco,
int dwReserved
); // HRESULT GetDesktopItemOptions(LPCOMPONENTSOPT pco,DWORD dwReserved);

IntPtr SetDesktopItemOptions([In]ref _win32._tagCOMPONENTSOPT pcomp,
int dwReserved
); // HRESULT SetDesktopItemOptions(LPCCOMPONENTSOPT pcomp,DWORD dwReserved);

IntPtr AddDesktopItem([In]ref _win32._tagCOMPONENT pcomp,
int dwReserved
); // HRESULT AddDesktopItem(LPCOMPONENT pcomp,DWORD dwReserved);

IntPtr AddDesktopItemWithUI( IntPtr hwnd,
[In]ref _win32._tagCOMPONENT pcomp,
int dwFlags
); // HRESULT AddDesktopItemWithUI(HWND hwnd,LPCOMPONENT pcomp,DWORD dwFlags);

IntPtr ModifyDesktopItem([In]ref _win32._tagCOMPONENT pcomp,
int dwFlags
); // HRESULT ModifyDesktopItem(LPCCOMPONENT pcomp,DWORD dwFlags);

IntPtr RemoveDesktopItem([In]ref _win32._tagCOMPONENT pcomp,
int dwReserved
); // HRESULT RemoveDesktopItem(LPCCOMPONENT pcomp,DWORD dwReserved);

IntPtr GetDesktopItemCount(out int lpiCount,
int dwReserved
); // HRESULT GetDesktopItemCount(LPINT lpiCount,DWORD dwReserved);

IntPtr GetDesktopItem( int nComponent,
ref _win32._tagCOMPONENT pcomp,
int dwReserved
); // HRESULT GetDesktopItem(int nComponent,LPCOMPONENT pcomp,DWORD dwReserved);

IntPtr GetDesktopItemByID( IntPtr dwID,
ref _win32._tagCOMPONENT pcomp,
int dwReserved
); // HRESULT GetDesktopItemByID(DWORD dwID,LPCOMPONENT pcomp,DWORD dwReserved);

IntPtr GenerateDesktopItemHtml([MarshalAs(UnmanagedType.LPWStr)]string pwszFileName,
[In] ref _win32._tagCOMPPOS pcomp,
int dwReserved
); // HRESULT GenerateDesktopItemHtml(LPCWSTR pwszFileName,LPCOMPONENT pcomp,DWORD dwReserved);

IntPtr AddUrl( IntPtr hwnd,
[MarshalAs(UnmanagedType.LPWStr)]
string pszSource,
[In] ref _win32._tagCOMPONENT pcomp,
int dwFlags
); // HRESULT AddUrl(HWND hwnd,LPCWSTR pszSource,LPCOMPONENT pcomp,DWORD dwFlags);

IntPtr GetDesktopItemBySource([MarshalAs(UnmanagedType.LPWStr)]string pszSource,
ref _win32._tagCOMPONENT pcomp,
int dwReserved
); // HRESULT GetDesktopItemBySource(LPCWSTR pszSource,LPCOMPONENT pcomp,DWORD dwReserved);

}
#endregion
 #region win32 consts & structs
 
public class _win32
{
public const int AD_APPLY_ALL = AD_APPLY_SAVE | AD_APPLY_HTMLGEN | AD_APPLY_REFRESH;
public const int AD_APPLY_BUFFERED_REFRESH = 0x10;
public const int AD_APPLY_DYNAMICREFRESH = 0x20;
public const int AD_APPLY_FORCE = 0x8;
public const int AD_APPLY_HTMLGEN = 0x2;
public const int AD_APPLY_REFRESH = 0x4;
public const int AD_APPLY_SAVE = 0x1;
public const int COMP_ELEM_ALL = COMP_ELEM_TYPE | COMP_ELEM_CHECKED | COMP_ELEM_DIRTY | COMP_ELEM_NOSCROLL | COMP_ELEM_POS_LEFT | COMP_ELEM_SIZE_WIDTH | COMP_ELEM_SIZE_HEIGHT | COMP_ELEM_POS_ZINDEX | COMP_ELEM_SOURCE | COMP_ELEM_FRIENDLYNAME | COMP_ELEM_POS_TOP | COMP_ELEM_SUBSCRIBEDURL | COMP_ELEM_ORIGINAL_CSI | COMP_ELEM_RESTORED_CSI | COMP_ELEM_CURITEMSTATE;
public const int COMP_ELEM_CHECKED = 0x2;
public const int COMP_ELEM_CURITEMSTATE = 0x4000;
public const int COMP_ELEM_DIRTY = 0x4;
public const int COMP_ELEM_FRIENDLYNAME = 0x400;
public const int COMP_ELEM_NOSCROLL = 0x8;
public const int COMP_ELEM_ORIGINAL_CSI = 0x1000;
public const int COMP_ELEM_POS_LEFT = 0x10;
public const int COMP_ELEM_POS_TOP = 0x20;
public const int COMP_ELEM_POS_ZINDEX = 0x100;
public const int COMP_ELEM_RESTORED_CSI = 0x2000;
public const int COMP_ELEM_SIZE_HEIGHT = 0x80;
public const int COMP_ELEM_SIZE_WIDTH = 0x40;
public const int COMP_ELEM_SOURCE = 0x200;
public const int COMP_ELEM_TYPE = 0x1;
public const int COMP_ELEM_SUBSCRIBEDURL = 0x800;
public const int COMP_TYPE_CONTROL = 3;
public const int COMP_TYPE_HTMLDOC = 0;
public const int COMP_TYPE_PICTURE = 1;
public const int COMP_TYPE_WEBSITE = 2;
public const int COMPONENT_DEFAULT_LEFT = (0xFFFF);
public const int COMPONENT_DEFAULT_TOP = (0xFFFF);
public const int IS_FULLSCREEN = 0x2;
public const int IS_NORMAL = 0x1;
public const int IS_SPLIT = 0x4;
public const int MAX_PATH = 260;
public const int WPSTYLE_CENTER = 0;
public const int WPSTYLE_MAX = 3;
public const int WPSTYLE_STRETCH = 2;
public const int WPSTYLE_TILE = 1;

[StructLayout(LayoutKind.Sequential)]
public struct _tagCOMPSTATEINFO
{
int dwSize;
int iLeft;
int iTop;
int dwWidth;
int dwHeight;
int dwItemState;
}

[StructLayout(LayoutKind.Sequential)]
public struct _tagCOMPPOS
{
int dwSize;
int iLeft;
int iTop;
int dwWidth;
int dwHeight;
int izIndex;
[MarshalAs(UnmanagedType.Bool)]
bool fCanResize;
[MarshalAs(UnmanagedType.Bool)]
bool fCanResizeX;
[MarshalAs(UnmanagedType.Bool)]
bool fCanResizeY;
int iPreferredLeftPercent;
int iPreferredTopPercent;
}

[StructLayout(LayoutKind.Sequential)]
public struct _tagCOMPONENT
{
public int dwSize;
public int dwID;
public int iComponentType;
[MarshalAs(UnmanagedType.Bool)]
public bool fChecked;
[MarshalAs(UnmanagedType.Bool)]
public bool fDirty;
[MarshalAs(UnmanagedType.Bool)]
public bool fNoScroll;
public _tagCOMPPOS cpPos;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
public string wszFriendlyName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=2084)]
public string wszSource;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=2084)]
public string wszSubscribedURL;
public int dwCurItemState;
public _tagCOMPSTATEINFO csiOriginal;
public _tagCOMPSTATEINFO csiRestored;
}

[StructLayout(LayoutKind.Sequential)]
public struct _tagCOMPONENTSOPT
{
public int dwSize;
[MarshalAs(UnmanagedType.Bool)]
public bool fEnableComponents;
[MarshalAs(UnmanagedType.Bool)]
public bool fActiveDesktop;
}

[StructLayout(LayoutKind.Sequential)]
public struct _tagWALLPAPEROPT
{
public int dwSize;
public int dwStyle;
}
}
#endregion