You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Microsoft.InteropFormTools在VB6中调用WinForms RichTextBox

把WinForms RichTextBox包装成VB6能用的OCX解决方案

一、解决VS2019里InteropFormProxyGenerator模板找不到的问题

你编译完工具后,模板不会自动加载,得手动复制:

  • 找到编译输出里的Templates文件夹,一般在Microsoft.InteropFormTools.InteropFormProxyGenerator\bin\Release\Templates路径下,里面有个.zip格式的模板包
  • 把这个zip包复制到VS的用户模板目录:C:\Users\[你的用户名]\Documents\Visual Studio 2019\Templates\ProjectTemplates\Visual C#
  • 重启VS2019,新建项目时就能找到Interop Form相关模板了

二、用工具包装RichTextBox的具体步骤

  1. 通过修复后的模板新建Interop Form项目
  2. 给项目添加System.Windows.Forms引用,然后把RichTextBox控件拖到窗体上
  3. 把VB6需要调用的属性、方法暴露出来:
    比如暴露RTF文本属性和追加文本方法,在窗体类里加代码:
    public string RichTextContent
    {
        get { return richTextBox1.Rtf; }
        set { richTextBox1.Rtf = value; }
    }
    
    public void AddText(string text)
    {
        richTextBox1.AppendText(text);
    }
    
  4. 右键项目,选Interop Form -> Generate Proxy,工具会自动生成COM代理类和注册脚本
  5. 以管理员身份运行生成的注册脚本,完成COM组件注册
  6. 打开VB6,通过工程->部件找到你生成的组件,添加后就能像普通OCX一样用了

三、工具失效时的手动包装方案

如果这个二次开发工具因为停更出问题,你可以手动写COM可见的.NET类来包装:

  1. 新建Class Library项目,在项目属性的程序集信息里勾选使程序集COM可见
  2. 添加System.Windows.Forms引用,创建继承自AxHost的包装类:
    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    [ComVisible(true)]
    [ProgId("MyRichTextBoxOCX.Wrapper")]
    [ClassInterface(ClassInterfaceType.None)]
    public class RichTextBoxWrapper : AxHost
    {
        private AxRichTextBox _innerRichTextBox;
    
        public RichTextBoxWrapper() : base("63a69f44-7b7d-11d1-a7f3-0000f8757e2a")
        {
        }
    
        protected override void CreateSink()
        {
            base.CreateSink();
            _innerRichTextBox = (AxRichTextBox)GetOcx();
        }
    
        public string RtfText
        {
            get => _innerRichTextBox.Rtf;
            set => _innerRichTextBox.Rtf = value;
        }
    
        public void AppendText(string text)
        {
            _innerRichTextBox.AppendText(text);
        }
    }
    
  3. 在项目属性里勾选注册为COM互操作,编译后系统会自动注册这个组件
  4. 之后在VB6里添加部件就能使用了

内容的提问来源于stack exchange,提问作者tmighty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 01:33:24