首页 > 解决方案 > 在 VB6 中从 Form1 调用 Form2 使用实例问题

问题描述

实际上,我想在我的 VB6 项目中使用 cefsharp,但我遇到了以下两个问题:-

  1. 假设我的项目运行form1,我需要form2使用 C# 类库调用我使用 cefsharp 初始化 chromium 浏览器的位置。

现在,当我从 form1 调用 form2 时 form2.showme (这里showme只是一个自定义函数来设置大小等),铬浏览器能够毫无问题地进行初始化,但是当我调用 form2 时

Public ofrm2 AS form2

Public Sub function()
Set ofrm2=new form2
call ofrm2.showme
End Sub

那么即使没有给出错误日志或任何类型的错误并且form2成功显示,但这次没有cefsharp chromium浏览器对象。

我也在使用SetParentSetWindowPos在 form2 中设置我的铬浏览器的长度和宽度。我真的找不到问题,请提出一些解决方案。

  1. 第二个问题是,一旦我的代码初始化 chromium,VB6 编辑器就不允许调试。为我测试应用程序的唯一方法是制作一个exe,然后运行应用程序并尝试使用日志进行调试。

编辑:- *********************************************** *****************

我的 C# dll 包含 cefsharp 包

[PermissionSet(SecurityAction.Demand, Name ="FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)] 
    [ClassInterface(ClassInterfaceType.None)]
    public partial class Form1 : Form, IForm1
    {
        private ChromiumWebBrowser _browser;
        public Form1()
        {
            InitializeComponent();
            //initializeChromium();
        }

        public int initializeChromium()
        {
            CefSettings settings = new CefSettings();

            if (!Cef.IsInitialized)
            {
                CefSharp.Cef.Initialize(settings);
            }
            _browser = new ChromiumWebBrowser("http://127.0.0.1:24125/ABC/DEF.html");
            _browser.RegisterJsObject("External", this);

            this.Controls.Add(_browser);

            _browser.BringToFront();
            _browser.Dock = DockStyle.Fill;

            this.Show();
            return 0;

        }
    }

我的表格1:-

Private Sub Form_Load()
Call launch ------ This one does not work. definition is given in my module below
'frmHTML.Show -----This one works

End Sub

Private Sub Form_UnLoad(cancel As Integer)
Set ofrmHtml = Nothing
End Sub

ModDec.bas:-

Option Explicit

Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long


Public ofrmHtml As frmHTML

Public Sub launch()
Set ofrmHtml = New frmHTML
Call ofrmHtml.InitChromium1


ofrmHtml.ShowMe
'call ofrmHtml.ShowMe
End Sub

我的 form2(frmHTML):-

Option Explicit

Public cSharpObj As New cSharp.Form1

Private Sub Form_Load()  
   'Call InitChromium1
    Exit Sub
End Sub

Public Sub InitChromium1()
'   On Error GoTo Err_Hndlr   

   SetParent cSharpObj.getHandle, frmHTML.hWnd
   SetWindowPos cSharpObj.getHandle, 0, 0, 0, 512, 480, 0



   Dim i As Integer
   i = -1
   i = cSharpObj.initializeChromium()

    Exit Sub
End Sub
Public Sub ShowMe()
    Call Me.Show
End Sub

标签: c#vb6cefsharp

解决方案


推荐阅读