以下提供C++代码:通过IActiveDesktop接口来实现
比如设置壁纸,就可以这样 IActiveDesktop *a;
CoInitialize(NULL);
if(SUCCEEDED(CoCreateInstance(Shlobj::CLSID_ActiveDesktop,NULL,CLSCTX_INPROC_SERVER,IID_IActiveDesktop,(void **)&a)))
{
WideString c = "C:\My Documents\yw2.jpg";
OleCheck(a->SetWallpaper(c.c_bstr(),0));
OleCheck(a->ApplyChanges(AD_APPLY_ALL));
a->Release();
}
CoUninitialize(); 注意在cpp的第一行加入#define NO_WIN32_LEAN_AND_MEAN
并要#include <shlobj.h>
请问要实现上面的功能在C#里该怎么写?
比如设置壁纸,就可以这样 IActiveDesktop *a;
CoInitialize(NULL);
if(SUCCEEDED(CoCreateInstance(Shlobj::CLSID_ActiveDesktop,NULL,CLSCTX_INPROC_SERVER,IID_IActiveDesktop,(void **)&a)))
{
WideString c = "C:\My Documents\yw2.jpg";
OleCheck(a->SetWallpaper(c.c_bstr(),0));
OleCheck(a->ApplyChanges(AD_APPLY_ALL));
a->Release();
}
CoUninitialize(); 注意在cpp的第一行加入#define NO_WIN32_LEAN_AND_MEAN
并要#include <shlobj.h>
请问要实现上面的功能在C#里该怎么写?
解决方案 »
- treeview里怎样改变选中节点的字体颜色
- 有关属性的问题
- 对数据库的操作不能保存
- 关于DataView.RowFilter
- dataSet+DatagridView
- 试图运行项目时出错:无法在Web服务器上启动调试。未能启动ASP。NET或 ATL Server调试。验证服务器上是否正确安装了ASP。NET或ATL Server
- 郁闷呀,发了几个贴子,没人回,白白浪费了几百分。呜呜呜
- 请教关于C#中舱特定情况下类库文件解耦的方法?
- 如何打印窗体的内容?
- C#如何读取16位的bitmap文件
- 如何创建单一的COM+对象供系统任何进程访问?
- foxmail 中优先级高低(红色标识和绿色标识)在Datagrid中能否实现,是插入图片吗?求救!
How to create COM wrapper for IActiveDesktop
http://www.dotnet247.com/247reference/msgs/41/205722.aspx
using VBActiveDesktop;
using System.Runtime.InteropServices;
using System.Text;namespace egxsun
{
/// <summary>
/// ActiveDesktop 的摘要说明。
/// </summary>
public class ActiveDesktop:IActiveDesktop
{
public ActiveDesktop()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public void SetActiveDesktopWallpaper(string sFileName ,int lStyle,bool fForce )
{
VBActiveDesktop.ActiveDesktop ad =new VBActiveDesktop.ActiveDesktopClass();
COMPONENTSOPT co;
// COMPONENTSOPT opts = new COMPONENTSOPT(); WALLPAPEROPT wpo;
// ad = new ActiveDesktop();
co.dwSize = 12;//, Marshal.SizeOf(co);
ad.GetDesktopItemOptions(out co, 0);
if((co.fActiveDesktop == 0) && fForce)
{
co.fActiveDesktop = 1;
ad.SetDesktopItemOptions(ref co,0);
}
wpo.dwSize = 8;//Marshal.SizeOf(wpo); wpo.dwStyle = (WPSTYLE)lStyle;
ad.SetWallpaperOptions(ref wpo, 0);
ad.SetWallpaper(sFileName,0);
ad.ApplyChanges(AD_APPLY.AD_APPLY_ALL);
ad =null;
}
// static readonly Guid CLSID_ActiveDesktop = new Guid("{75048700-EF1F-11D0-9888-006097DEACF9}");
//
// public static IActiveDesktop GetActiveDesktop()
// {
// Type typeActiveDesktop = Type.GetTypeFromCLSID(CLSID_ActiveDesktop);
// return (IActiveDesktop) Activator.CreateInstance(typeActiveDesktop);
// } public string GetActiveDesktopWallpaper()
{
VBActiveDesktop.ActiveDesktop ad =new VBActiveDesktop.ActiveDesktopClass();
// char[] c=new char[260];
string sCurrentFile =" ";//new string(c);
// string sCurrentFile = new string('0',0,260);
// StringBuilder sCurrentFile = new StringBuilder(0,260);
// IActiveDesktop ad = GetActiveDesktop();
// ad.GetDesktopItemOptions( ref opts, 0 );
for(int i=0;i<=260;i++)
{
sCurrentFile =sCurrentFile+" ";
}
WALLPAPEROPT wpo;
wpo.dwSize = 8;// Len(wpo)
//sCurrentFile = String$(260, 0);
//Set ad = New ActiveDesktop
ad.GetWallpaper(sCurrentFile,260, 0);
ad.GetWallpaperOptions(out wpo, 0);
ad =null;
sCurrentFile = sCurrentFile.ToString().Trim();
if(sCurrentFile.EndsWith("\0"))
{
sCurrentFile = sCurrentFile.Substring(0, sCurrentFile.Length - 1);
}
// sCurrentFile.Substring(0,sCurrentFile.Length-sCurrentFile.LastIndexOf('\\'));
return sCurrentFile;
} #region IActiveDesktop 成员 public void ModifyDesktopItem(System.IntPtr pcomp, VBActiveDesktop.COMP_ELEM dwFlags)
{
// TODO: 添加 ActiveDesktop.ModifyDesktopItem 实现
} public void ADDURL(int hwnd, string pszSource, System.IntPtr pcomp, VBActiveDesktop.ADDURL dwFlags)
{
// TODO: 添加 ActiveDesktop.ADDURL 实现
} public void AddDesktopItemWithUI(int hwnd, System.IntPtr pcomp, int dwReserved)
{
// TODO: 添加 ActiveDesktop.AddDesktopItemWithUI 实现
} public void SetPattern(string pwszPattern, int dwReserved)
{
// TODO: 添加 ActiveDesktop.SetPattern 实现
} public void GetWallpaper(string pwszWallpaper, int cchWallpaper, int dwReserved)
{
// TODO: 添加 ActiveDesktop.GetWallpaper 实现
} public void GetDesktopItemBySource(string pwszSource, System.IntPtr pcomp, int dwReserved)
{
// TODO: 添加 ActiveDesktop.GetDesktopItemBySource 实现
} public void AddDesktopItem(System.IntPtr pcomp, int dwReserved)
{
// TODO: 添加 ActiveDesktop.AddDesktopItem 实现
} public void GetPattern(string pwszPattern, int cchPattern, int dwReserved)
{
// TODO: 添加 ActiveDesktop.GetPattern 实现
} public void GetDesktopItemByID(int dwID, System.IntPtr pcomp, int dwReserved)
{
// TODO: 添加 ActiveDesktop.GetDesktopItemByID 实现
} public void SetWallpaperOptions(ref WALLPAPEROPT pwpo, int dwReserved)
{
// TODO: 添加 ActiveDesktop.SetWallpaperOptions 实现
} public void GetDesktopItemCount(out int lpiCount, int dwReserved)
{
// TODO: 添加 ActiveDesktop.GetDesktopItemCount 实现
lpiCount = 0;
} public void ApplyChanges(VBActiveDesktop.AD_APPLY dwFlags)
{
// TODO: 添加 ActiveDesktop.ApplyChanges 实现
} public void RemoveDesktopItem(System.IntPtr pcomp, int dwReserved)
{
// TODO: 添加 ActiveDesktop.RemoveDesktopItem 实现
} public void GetDesktopItem(int nComponent, System.IntPtr pcomp, int dwReserved)
{
// TODO: 添加 ActiveDesktop.GetDesktopItem 实现
} public void GetWallpaperOptions(out WALLPAPEROPT pwpo, int dwReserved)
{
// TODO: 添加 ActiveDesktop.GetWallpaperOptions 实现
pwpo = new WALLPAPEROPT ();
} public void SetWallpaper(string pwszWallpaper, int dwReserved)
{
// TODO: 添加 ActiveDesktop.SetWallpaper 实现
} public void SetDesktopItemOptions(ref COMPONENTSOPT pco, int dwReserved)
{
// TODO: 添加 ActiveDesktop.SetDesktopItemOptions 实现
} public void GetDesktopItemOptions(out COMPONENTSOPT pco, int dwReserved)
{
// TODO: 添加 ActiveDesktop.GetDesktopItemOptions 实现
pco = new COMPONENTSOPT ();
} public void GenerateDesktopItemHtml(string pwszFileName, System.IntPtr pcomp, int dwReserved)
{
// TODO: 添加 ActiveDesktop.GenerateDesktopItemHtml 实现
} #endregion
}
}
结构也需要重新定义,我给你的那个链接里面就是对使用到的接口以及接口进行的
定义在你的工程新建一个文件,然后把这些定义拷贝到这个文件中,然后你就可以用
ActiveDesktop ad = new ActiveDesktop();
IActiveDesktop iad = ad as IActiveDesktop ;
if (iad != null)
{
iad.SetWallpaper("d:\\temp\\1.bmp",0);
iad.ApplyChanges(AD_APPLY.ALL );
Marshal.ReleaseComObject(ad);
ad = null; }
using System.Runtime.InteropServices ;namespace WindowsApplication3
{
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 */ {}
}
我按你说的做了,但运行后桌面只是刷新了下,HTML文档墙纸并没变,为什么?
iad.ApplyChanges(AD_APPLY.ALL );