我创建了一个带有 ActiveX 界面的 .NET 用户控件。它运作良好。
现在,我希望能够从 ActiveX 接口的属性包中读取和写入。
我该怎么做?
我创建了一个带有 ActiveX 界面的 .NET 用户控件。它运作良好。
现在,我希望能够从 ActiveX 接口的属性包中读取和写入。
我该怎么做?
最简单的方法是使用客户端脚本将参数值传递给 ActiveX
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script language="javascript">
function Rundata(file)
{
var winCtrl = document.getElementById("YourActiveX");
winCtrl.Option1 = file;
winCtrl.WriteToFile();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<object id="YourActiveX" classid="clsid:6b1bdf22-1c1d-774e-cd9d-1d1aaf7fd88f"
width="300px" height="200px">
<param name="Option1" value="valuetoRetrieve1" />
</object>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button runat="server" ID="Button1" OnClientClick="javascript:Rundata('valuetoRetrieve2');" />
</div>
</form>
</body>
</html>
如果您不能使用客户端脚本,您可以尝试这种方式:
假设您要读取一个参数,例如:
<object id="YourActiveX" classid="clsid:6b1bdf22-1c1d-774e-cd9d-1d1aaf7fd88f"
width="300px" height="200px">
<param name="option1" value="valuetoRetrieve" />
</object>
您需要在项目中公开以下 COM 接口:
[ComImport]
[Guid("55272A00-42CB-11CE-8135-00AA004BB851")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPropertyBag
{
void Write([InAttribute] string propName, [InAttribute] ref Object ptrVar);
void Read([InAttribute] string propName, out Object ptrVar, int errorLog);
}
[ComImport]
[Guid("37D84F60-42CB-11CE-8135-00AA004BB851")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPersistPropertyBag
{
[PreserveSig]
void InitNew();
[PreserveSig]
void Load(IPropertyBag propertyBag, int errorLog);
[PreserveSig]
void Save(IPropertyBag propertyBag, [InAttribute] bool clearDirty, [InAttribute] bool saveAllProperties);
[PreserveSig]
void GetClassID(out Guid classID);
}
您的 activeX 控件应该实现这些接口。您需要实现一种方法:
void IPersistPropertyBag.Load(IPropertyBag propertyBag, int errorLog)
{
object value;
propertyBag.Read("option1", out value, errorLog);
string parameter = (string)value;
}
瞧!参数应等于“valuetoRetrieve”
我试图让我的 C# ActiveX 在 OBJECT 标记中接收 PARAM 属性。
我尝试使用此处提出的解决方案,但在加载对象时发现 IE 崩溃...
最后,我可以使用不同的 IPropertyBag 和 IPersistPropertyBag 接口来正确处理它:
[ComVisible(true), ComImport,
Guid("0000010C-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPersist
{
[PreserveSig]
int GetClassID([Out] out Guid pClassID);
}
[ComVisible(true), ComImport,
Guid("37D84F60-42CB-11CE-8135-00AA004BB851"),//Guid("5738E040-B67F-11d0-BD4D-00A0C911CE86"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPersistPropertyBag : IPersist
{
#region IPersist
[PreserveSig]
new int GetClassID([Out] out Guid pClassID);
#endregion
[PreserveSig]
int InitNew();
[PreserveSig]
int Load(
[In] IPropertyBag pPropBag,
[In, MarshalAs(UnmanagedType.Interface)] object pErrorLog
);
[PreserveSig]
int Save(
IPropertyBag pPropBag,
[In, MarshalAs(UnmanagedType.Bool)] bool fClearDirty,
[In, MarshalAs(UnmanagedType.Bool)] bool fSaveAllProperties
);
}
[ComVisible(true), ComImport,
Guid("55272A00-42CB-11CE-8135-00AA004BB851"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPropertyBag
{
[PreserveSig]
int Read(
[In, MarshalAs(UnmanagedType.LPWStr)] string pszPropName,
[In, Out, MarshalAs(UnmanagedType.Struct)] ref object pVar,
[In] IntPtr pErrorLog);
[PreserveSig]
int Write(
[In, MarshalAs(UnmanagedType.LPWStr)] string pszPropName,
[In, MarshalAs(UnmanagedType.Struct)] ref object pVar);
}
然后我实现了这样的加载方法:
#region IPropertyBag Members
public int Read(string pszPropName, ref object pVar, IntPtr pErrorLog)
{
pVar = null;
switch (pszPropName)
{
case "FileType": pVar = _fileType; break;
case "WebServiceUrl": pVar = _webServiceUrl; break;
case "Language": pVar = _language; break;
}
return 0;
}
public int Write(string pszPropName, ref object pVar)
{
switch (pszPropName)
{
case "FileType": _fileType = (string)pVar; break;
case "WebServiceUrl": _webServiceUrl = (string)pVar; break;
case "Language": _language = (string)pVar; break;
}
return 0;
}
#endregion
#region IPersistPropertyBag Members
public int GetClassID(out Guid pClassID)
{
throw new NotImplementedException();
}
public int InitNew()
{
return 0;
}
public int Load(IPropertyBag pPropBag, object pErrorLog)
{
object val = null;
pPropBag.Read("FileType", ref val, IntPtr.Zero);
Write("FileType", ref val);
pPropBag.Read("WebServiceUrl", ref val, IntPtr.Zero);
Write("WebServiceUrl", ref val);
pPropBag.Read("Language", ref val, IntPtr.Zero);
Write("Language", ref val);
return 0;
}
public int Save(IPropertyBag pPropBag, bool fClearDirty, bool fSaveAllProperties)
{
return 0;
}
#endregion
它奏效了。
希望这可以帮助处于相同情况的人。