使用C#中的ActiveX PropertyBags [英] Using ActiveX PropertyBags from C#

查看:139
本文介绍了使用C#中的ActiveX PropertyBags的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有ActiveX界面的.NET用户控件。

I have created a .NET user control with an ActiveX interface. It works well.

现在,我希望能够从属性包中读取和写入ActiveX界面。

Now, I want to be able to read and write from the property bag for the ActiveX interface.

我该怎么做?

推荐答案

最简单的方法是使用客户端脚本将参数值传递给ActiveX。

The easiest is to use client script to pass the parameters values to the 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>

如果您不能使用客户端脚本,可以尝试以下方式:

假设您要读取一个参数,例如:

Let's say you want to read a parameter such as:

<object id="YourActiveX" classid="clsid:6b1bdf22-1c1d-774e-cd9d-1d1aaf7fd88f" 
    width="300px" height="200px">
    <param name="option1" value="valuetoRetrieve" />
    </object>

您需要在项目中公开以下COM接口:

You need to expose the following COM interfaces in your project:

[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控件应实现这些接口。有一种方法需要实现:

Your activeX control should implement these interfaces. There's one method you need to implement :

void IPersistPropertyBag.Load(IPropertyBag propertyBag, int errorLog) 
    {
        object value; 
        propertyBag.Read("option1", out value, errorLog);  
        string parameter = (string)value;
    }

Voilà!参数应等于 valuetoRetrieve

Voilà! parameter should be equal to "valuetoRetrieve"

这篇关于使用C#中的ActiveX PropertyBags的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆