【发布时间】:2016-02-02 18:15:44
【问题描述】:
问题
尝试在 Visual Studio 设计器中为 ActiveX 控件添加变量时出现“无法访问 ActiveX 控件类型库”错误(通过 右键单击 ->“添加变量” 或通过 类向导 em>) 在基于 MFC 对话框的应用程序中。我需要它来处理这个 ActiveX 控件的方法和事件,它是从 windows 窗体控件创建并在系统中注册的 (Windows 7)。
我有什么
这是简化的 C# COM 实现(一种从 MFC 端调用的方法和一个用于通知 MFC 任何事情的事件):
[ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IWinFormControl
{
[DispId(1)]
string MethodTest(string s);
}
[ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IWinFormControlEvents
{
[DispId(2)]
void SimpleEvent(string s);
}
[ComVisible(true),
ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(IWinFormControl)),
ComSourceInterfaces(typeof(IWinFormControlEvents))]
public partial class WinFormControlImpl : UserControl, IWinFormControl
{
public delegate void SimpleEventType(string s);
public event SimpleEventType SimpleEvent;
public WinFormControlImpl()
{
this.InitializeComponent();
}
private void Button_Click(object sender, EventArgs e)
{
if (this.SimpleEvent != null)
{
this.SimpleEvent("TESTEVENT");
}
}
public string MethodTest(string s)
{
MessageBox.Show("TESTMETHOD " + s);
}
// ... ComRegisterFunction and ComUnregisterFunction
}
"Register for COM interop" 项目标志选中,构建组件成功添加到注册表后,我可以使用 TstCon32 实用程序对其进行测试并调用 MethodTest时间>那里。
在 MFC 项目中,我在设计视图中打开对话框面板,右键单击并选择“插入 ActiveX 控件”,然后成功找到我的 ActiveX 控件并将其插入到对话框面板中。应用程序运行,我看到所有内容的 ActiveX。但是我不能为这个控件创建变量。
我尝试了什么
研究给了我两个一般的解决方法。首先是忘记 COM,将 .NET 项目的引用添加到 MFC 项目中,并使用 CWinFormsControl 和 /CLR 为用户控制创建本机 C++ 包装器。这不适合我的任务。
另一种可能的解决方案是手动为 ActiveX 控件创建变量,但我找不到足够的示例来实现这一点。我发现的最接近的黑客是在question 中,其中在CWnd 变量内创建的ActiveX 对象和此COM 的功能通过接口访问,但我不知道如何放置此变量信息显示对话框面板的可视内容。
问题是:如何在基于MFC对话框的窗口中为ActiveX组件(用C#创建)创建变量?
【问题讨论】: