miniblink是一个开源的、单文件、且目前已知的最小的基于chromium的,浏览器控件。通过其导出的纯C接口,几行代码即可创建一个浏览器控件。

我扫了下下最新的mb132版本,粗略地看了下mb的头文件,看样子是stdcall调用约定,偷闲翻译了一个简单的小例子。测试了下在64位的excel上跑起来了。我这只是开个头,加载网页以及事件回调。给有这方面有需求的人一个小小的参考。下面的代码放在模块里即可,miniblink 132的dll文件和excel文件放一起即可。

Option Explicit

' prepared by fxl447098457
'
#If Win64 Then
Private Declare PtrSafe Function mbInit Lib "mb132_x64.dll" (ByVal settings As LongPtr) As Long
Private Declare PtrSafe Function mbCreateWebWindow Lib "mb132_x64.dll" (ByVal windowType As Long, ByVal parent As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal width As Long, ByVal height As Long) As LongPtr
Private Declare PtrSafe Function mbShowWindow Lib "mb132_x64.dll" (ByVal view As LongPtr, ByVal show As Long) As Long
Private Declare PtrSafe Function mbMoveToCenter Lib "mb132_x64.dll" (ByVal view As LongPtr) As Long
Private Declare PtrSafe Function mbLoadURL Lib "mb132_x64.dll" (ByVal view As LongPtr, ByVal url As String) As Long
'Private Declare PtrSafe Sub mbRunMessageLoop Lib "mb132_x64.dll" ()  ' Might block VBA
Private Declare PtrSafe Sub mbUninit Lib "mb132_x64.dll" ()
Private Declare PtrSafe Function mbOnDocumentReady Lib "mb132_x64.dll" (ByVal webView As LongPtr, ByVal callback As LongPtr, ByVal param As LongPtr) As Long
Private Declare PtrSafe Function mbIsMainFrame Lib "mb132_x64.dll" (ByVal webView As LongPtr, ByVal frameId As LongPtr) As Long
Private Declare PtrSafe Function mbRunJs Lib "mb132_x64.dll" (ByVal webView As LongPtr, ByVal frameId As LongPtr, ByVal script As String, ByVal isEval As Long, _
                                                          ByVal callback As LongPtr, ByVal param As LongPtr, ByVal flags As Long) As Long
' Optional: For better control, declare mbDestroyWebWindow if needed
Private Declare PtrSafe Function mbDestroyWebWindow Lib "mb132_x64.dll" (ByVal view As LongPtr) As Long
#Else
 #If VBA7 = 0 Then
    enum Longptr
        [_]
    End Enum
 #End If
Private Declare Function mbInit Lib "mb132_x32.dll" (ByVal settings As LongPtr) As Long
Private Declare Function mbCreateWebWindow Lib "mb132_x32.dll" (ByVal windowType As Long, ByVal parent As LongPtr, ByVal x As Long, ByVal y As Long, ByVal width As Long, ByVal height As Long) As LongPtr
Private Declare Function mbShowWindow Lib "mb132_x32.dll" (ByVal view As LongPtr, ByVal show As Long) As Long
Private Declare Function mbMoveToCenter Lib "mb132_x32.dll" (ByVal view As LongPtr) As Long
Private Declare Function mbLoadURL Lib "mb132_x32.dll" (ByVal view As LongPtr, ByVal url As String) As Long
'Private Declare sub mbRunMessageLoop Lib "mb132_x32.dll" ()  ' Might block VBA
Private Declare Sub mbUninit Lib "mb132_x32.dll" ()
Private Declare Function mbOnDocumentReady Lib "mb132_x32.dll" (ByVal webView As LongPtr, ByVal callback As LongPtr, ByVal param As LongPtr) As Long
Private Declare Function mbIsMainFrame Lib "mb132_x32.dll" (ByVal webView As LongPtr, ByVal frameId As LongPtr) As Long
Private Declare Function mbRunJs Lib "mb132_x32.dll" (ByVal webView As LongPtr, ByVal frameId As LongPtr, ByVal script As String, ByVal isEval As Long, _
                                                          ByVal callback As LongPtr, ByVal param As LongPtr, ByVal flags As Long) As Long
' Optional: For better control, declare mbDestroyWebWindow if needed
Private Declare Function mbDestroyWebWindow Lib "mb132_x32.dll" (ByVal view As LongPtr) As Long
#End If

' --- Define Constants ---
Private Const MB_WINDOW_TYPE_POPUP As Long = 1 ' Adjust if the actual value differs
Private Const MB_WINDOW_TYPE_CONTROL As Long = 3
' --- Main Subroutine ---

Sub CreateMiniblinkWindow()
    Dim mbView As LongPtr
    Dim result As Long
    Dim url As String
    ChDrive ThisWorkbook.Path
    ChDir ThisWorkbook.Path
    ' --- 1. Initialize Miniblink (with NULL settings for defaults) ---
    ' Pass 0 (NULL pointer) for settings. If you need specific settings,
    ' passing a complex struct from VBA is significantly more complex.
    result = mbInit(0&)
    ' --- 2. Create the Web Window ---
    ' Parameters: Type, Parent (0 = no parent), X, Y, Width, Height
    mbView = mbCreateWebWindow(MB_WINDOW_TYPE_CONTROL, 0, 0, 0, 800, 600)
    If mbView = 0 Then
        MsgBox "Failed to create Miniblink window."
        mbUninit ' Clean up
        Exit Sub
    End If
    
    ' --- 3. Show and Position the Window ---
    result = mbShowWindow(mbView, 1) ' 1 = TRUE
    If result <> 0 Then
        MsgBox "Failed to show Miniblink window. Error code: " & result
        ' Consider calling mbDestroyWebWindow(mbView) and mbUninit here too
    End If

    result = mbMoveToCenter(mbView)

    ' --- 4. Load the URL ---
    url = "https://miniblink.net/views/doc/index.html"
    ' Note: mbLoadURL likely expects a null-terminated string.
    ' VBA strings are usually compatible, but passing ByRef (default for strings)
    ' or ByVal StrPtr(url) might be needed depending on the DLL's expectation.
    ' Passing ByVal url directly is common.
    result = mbLoadURL(mbView, url)
   If result <> 0 Then
         ' 设置 Document Ready 回调
        mbOnDocumentReady mbView, AddressOf OnDocumentReadyCallback, 0
    End If
    
    ' --- 5. Message Loop ---
    ' WARNING: mbRunMessageLoop is typically a blocking call that waits
    ' for window messages. This will freeze the VBA editor/UI until
    ' the miniblink window is closed externally or the loop exits somehow.
    ' This might not be desirable in an interactive VBA environment.
    ' Consider alternatives if responsiveness is needed.
    'mbRunMessageLoop

    ' --- Alternative to mbRunMessageLoop (Basic) ---
    ' VBA itself runs a message loop. Sometimes, just creating and showing
    ' the window is enough for it to be interactive, depending on how
    ' miniblink integrates. However, this is less reliable than a proper loop.
    ' Leaving the Sub ends VBA's execution path, but the window might persist
    ' if the DLL manages its own thread/window lifecycle.
    ' A callback mechanism (like mbOnDestroy) is usually better for cleanup.

    ' --- 6. Uninitialize (Ideally called when done) ---
    ' Because mbRunMessageLoop blocks, this line usually won't execute
    ' until the loop exits. If using an alternative approach,
    ' you need a way to trigger this (e.g., via a callback or another macro).
   mbUninit
End Sub

'evant callback

'void MB_CALL_TYPE onDocumentReady(mbWebView webView, void* param, mbWebFrameHandle frameId)
'{
'    if (mbIsMainFrame(webView, frameId)) {
'        mbRunJs(webView, frameId, "alert(1);", true, nullptr, nullptr, nullptr);
'    }
'}

'callback  mbOnDocumentReady
 Sub OnDocumentReadyCallback(ByVal webView As LongPtr, ByVal param As LongPtr, ByVal frameId As LongPtr)
    Dim isMain As Long
    Dim result As Long

    ' check is mainframe
    isMain = mbIsMainFrame(webView, frameId)

    If isMain <> 0 Then ' mbIsMainFrame
        ' ready to run JavaScript
        ' param: webView, frameId, script, isEval (True), callback (null), callbackParam (null), flags (0)
        mbRunJs webView, frameId, "alert(1);", 1, 0, 0, 0
    End If

End Sub

 运行结果:
QQ20250920-214612