在.NET和COM之间编组复杂的数据类型

时间:2021-10-29 23:25:11

I have spent the last day or so writing a simple control library in VB.NET to be used in an existing vb6 project. I have most things working; my controls show up in the vb6 toolbox, events work, etc. My current problem is trying to deal with passing complex objects between vb6 and .NET.

我花了大约一天左右在VB.NET中编写一个简单的控件库,用于现有的vb6项目。我有大部分工作;我的控件出现在vb6工具箱中,事件工作等等。我当前的问题是试图处理在vb6和.NET之间传递复杂对象。

For example, the Font property of a .NET TextBox. Now, vb6 uses stdFont objects and .NET (obviously) uses the System.Drawing.Font type. My goal with this project is to make the change transparent from the vb6 side. I would like to completely emulate the vb6 interface for each control to minimize the code that needs to change when we add the new controls in, but I am stuck here. Here is an example (.NET):

例如,.NET TextBox的Font属性。现在,vb6使用stdFont对象,.NET(显然)使用System.Drawing.Font类型。我在这个项目中的目标是让vb6方面的变更透明化。我想完全模拟每个控件的vb6接口,以最小化我们添加新控件时需要更改的代码,但我被困在这里。这是一个例子(.NET):

Public Overloads Property Font() As stdole.StdFont
    Get                        
        Return ???
    End Get
    Set(ByVal value As stdole.StdFont)
        Dim style As FontStyle = GetFontStyle(value)
        TextBox1.Font = New Font(value.Name, value.Size, style, GraphicsUnit.Pixel, value.Charset)
    End Set
End Property

As you can see, the 'getter' is the problem. How can I mimic the vb6 interface here if I cannot return the textbox's Font property? I don't want to return a stdFont copy of the System.Drawing.Font for obvious reasons.

如你所见,'getter'就是问题所在。如果我不能返回文本框的Font属性,我怎么能模仿这里的vb6接口?出于显而易见的原因,我不想返回System.Drawing.Font的stdFont副本。

Now, I could just add methods like SetBold(), SetItalic(), and SetFont() (or boolean properties like "FontIsBold"), but this is exactly what I am trying to avoid. My interop experience is fairly minimal, so any help here would be appreciated.

现在,我可以添加像SetBold(),SetItalic()和SetFont()这样的方法(或像“FontIsBold”这样的布尔属性),但这正是我想要避免的。我的互操作经验相当小,所以任何帮助都会受到赞赏。

1 个解决方案

#1


I was happy to see your answer... but I think I'm missing something. I followed the instructions in the link to msdn that you provided, and on the VB6 side, I have access to the font properties on the UserControl, but when I try to (say) set the font size, it doesn't affect the way the text is drawn. And when I try to force a change to font size on the embedded TextBox on the VB.Net side (in the Set Text property), I get an error saying that font.size is ReadOnly...

我很高兴看到你的回答......但我想我错过了什么。我按照你提供的msdn链接中的说明进行操作,在VB6端,我可以访问UserControl上的字体属性,但是当我尝试(比如说)设置字体大小时,它不会影响方式文字是绘制的。当我尝试在VB.Net端的嵌入式TextBox上强制更改字体大小时(在Set Text属性中),我收到一个错误,说font.size是ReadOnly ...

Is a working example available?

是一个有效的例子吗?

#1


I was happy to see your answer... but I think I'm missing something. I followed the instructions in the link to msdn that you provided, and on the VB6 side, I have access to the font properties on the UserControl, but when I try to (say) set the font size, it doesn't affect the way the text is drawn. And when I try to force a change to font size on the embedded TextBox on the VB.Net side (in the Set Text property), I get an error saying that font.size is ReadOnly...

我很高兴看到你的回答......但我想我错过了什么。我按照你提供的msdn链接中的说明进行操作,在VB6端,我可以访问UserControl上的字体属性,但是当我尝试(比如说)设置字体大小时,它不会影响方式文字是绘制的。当我尝试在VB.Net端的嵌入式TextBox上强制更改字体大小时(在Set Text属性中),我收到一个错误,说font.size是ReadOnly ...

Is a working example available?

是一个有效的例子吗?