|
-
Oct 30th, 2017, 08:49 AM
#8
Re: I hire best vb6 programmer here, buget $500-$1000
 Originally Posted by wqweto
Doable but this is going to be tricky and hard to debug implementation requiring a custom .tlb, a bit of multi-threading and probably interface hooking on the VB6 container the std web browser control is sited on.
An alternative (and much easier) approach would be, to add your own JS-Functions and -Properties/Vars into the WebBrowser-Controls DOM -
and then execute these JS-snippets over the WB.Document.Script-Object from the VB-side...
What many are not aware of is, that you can e.g. define a js-Variable with public scope - and then set it from outside the WB-Control to a VB-Object.
This js-Variable will then behave entirely normal inside JS - and allows easy to undertake Property- and Method-interactions with e.g. your VB-Form
(without resorting to Event-Bindings, which are also possible of course).
Here is an example (needs a Form with a Command1 and a WebBrowser1):
Code:
Option Explicit
Private script As Object
Private Sub Form_Load()
WebBrowser1.Navigate2 "about:blank"
Do Until WebBrowser1.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set script = WebBrowser1.Document.script
JsAdd "var oHost;"
Set JsProp("oHost") = Me
JsAdd "function reflect(p){return p}" 'just a plain JS-function, which returns the passed Arg as a result
JsAdd "function hostSetCaption(p){oHost.Caption = p}" 'Property-Access of the HostObj from inside JS
JsAdd "function hostCallMethod(p){oHost.DoSomeThingWith(p)}" 'a MethodCall on the HostObj from inside JS
End Sub
Private Sub Command1_Click()
Debug.Print "JsRun -> reflect: ", JsRun("reflect", "Hello")
Debug.Print "JsProp -> oHost.Caption: ", JsProp("oHost").Caption
JsRun "hostSetCaption", "Hello" 'should result in a change of our Forms Caption
JsRun "hostCallMethod", "Param, passed to the host-defined function from inside JS"
End Sub
Public Sub DoSomeThingWith(p) 'this public Method is accessible from inside JS as well (not only the default Form-Methods/Props)
MsgBox p
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set JsProp("oHost") = Nothing 'just to make sure, we cleanup a potential circle-ref
End Sub
'a small set of simple Helper-Routines for JS-Interaction
Public Sub JsAdd(code As String)
CallByName JsProp("window"), "execScript", VbMethod, code
End Sub
Public Property Get JsProp(propName As String)
VarCopy JsProp, CallByName(script, propName, VbGet)
End Property
Public Property Let JsProp(propName As String, RHS)
CallByName script, propName, VbLet, RHS
End Property
Public Property Set JsProp(propName As String, RHS)
CallByName script, propName, VbSet, RHS
End Property
Public Function JsRun(funcName As String, ParamArray p())
Select Case UBound(p) + 1
Case 0: VarCopy JsRun, CallByName(script, funcName, VbMethod)
Case 1: VarCopy JsRun, CallByName(script, funcName, VbMethod, p(0))
Case 2: VarCopy JsRun, CallByName(script, funcName, VbMethod, p(0), p(1))
Case 3: VarCopy JsRun, CallByName(script, funcName, VbMethod, p(0), p(1), p(2))
Case 4: VarCopy JsRun, CallByName(script, funcName, VbMethod, p(0), p(1), p(2), p(3))
End Select
End Function
Private Sub VarCopy(Dst, Src)
If IsObject(Src) Then Set Dst = Src Else Dst = Src
End Sub
Olaf
Last edited by Schmidt; Oct 30th, 2017 at 09:04 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|