HERE YOU GO...
Imports System.Threading
Imports Xilium.CefGlue
Imports Xilium.CefGlue.WindowsForms
Imports Xilium.CefGlue.CefSettings
Imports System.IO
Public Class Form1
Public Shared browserCtl As CefWebBrowser = Nothing
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
createBrowser()
loadBrowser()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
If Environment.OSVersion.Version.Major < 6 Then
Process.GetCurrentProcess().Kill()
End If
Catch ex As Exception
End Try
End Sub
Private Sub createBrowser()
Xilium.CefGlue.CefRuntime.Load()
Dim mainArgs = New Xilium.CefGlue.CefMainArgs(New String() {})
Dim cefSettings = New Xilium.CefGlue.CefSettings
With cefSettings
.SingleProcess = True
.MultiThreadedMessageLoop = True
.LogSeverity = Xilium.CefGlue.CefLogSeverity.Verbose
.LogFile = "C:\CefGlue.log"
End With
Xilium.CefGlue.CefRuntime.Initialize(mainArgs, cefSettings, Nothing)
CefRuntime.RegisterSchemeHandlerFactory("http", "", New schemeFactory())
End Sub
Private Sub loadBrowser()
If Debugger.IsAttached Then
If IO.Directory.Exists("C:\Documents and Settings\u60lev00\My Documents\Visual Studio 2010\Projects\TRAIN Safe\TRAIN Safe\src") Then
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory("C:\Documents and Settings\u60lev00\My Documents\Visual Studio 2010\Projects\TRAIN Safe\TRAIN Safe\src", Application.StartupPath & "\src", True)
Application.DoEvents()
End If
End If
browserCtl = New CefWebBrowser
browserCtl.Dock = DockStyle.Fill
browserCtl.BringToFront()
browserCtl.StartUrl = Application.StartupPath & "\src\handler.htm"
Me.Controls.Add(browserCtl)
browserCtl.CreateControl()
End Sub
Private Sub btnJS_Click(sender As System.Object, e As System.EventArgs) Handles btnJS.Click
browserCtl.Browser.GetMainFrame.ExecuteJavaScript("callFromWinForm('Im in.')", Nothing, 0)
End Sub
End Class
Public Class schemeHandler
Inherits CefResourceHandler
Protected Overrides Function ProcessRequest(request As CefRequest, callback As CefCallback) As Boolean
Dim inCom As String() = request.Url.ToString.Split("~")
Dim xClass = inCom(1)
If xClass = "DOMComplete" Then
JSComm.OnLoadEnd()
ElseIf xClass = "btnJS" Then
JSComm.OnLoadEnd()
ElseIf xClass = "EXITAPP" Then
JSComm.EXITCommand()
ElseIf xClass = "INTRODONE" Then
JSComm.checkforNewCourseFiles()
End If
callback.Continue()
Return (True)
End Function
Protected Overrides Sub GetResponseHeaders(response As CefResponse, ByRef responseLength As Long, ByRef redirectUrl As String)
End Sub
Protected Overrides Function ReadResponse(response As System.IO.Stream, bytesToRead As Integer, ByRef bytesRead As Integer, callback As CefCallback) As Boolean
Return (False)
End Function
Protected Overrides Function CanGetCookie(cookie As CefCookie) As Boolean
Return False
End Function
Protected Overrides Function CanSetCookie(cookie As CefCookie) As Boolean
Return False
End Function
Protected Overrides Sub Cancel()
End Sub
End Class
Public Class schemeFactory
Inherits CefSchemeHandlerFactory
Protected Overrides Function Create(browser As CefBrowser, frame As CefFrame, schemeName As String, request As CefRequest) As CefResourceHandler
Return New schemeHandler()
End Function
End Class
'''My html file is setup like this:
var xmlhttp;
function ajaxFunction() {
xmlhttp = new XMLHttpRequest();
}
function sendMessageToWindows(whatM) {
whatM = whatM.replace(/&/g, '--NN--');
//alert(whatM);
xmlhttp.open("GET", "
http://fromJS/~" + whatM, true);
xmlhttp.send(null);
}
function callFromWinForm(winMsg) {
var zp = winMsg.split('~');
//alert(winMsg);
if (zp[0] == 'DNLDNEW') {
showProgNewCourse(zp[1]);
} else if (zp[0] == 'HIDEDLD') {
hideProgCourse();
}
//if (winMsg.match(',')) {
//var sp = winMsg.split(',');
//}
//alert(winMsg);
}
'''Then I have a VB Class file called JSComm with the following:
Imports System.IO
Public Class JSComm
Public Shared Sub answerTest()
MsgBox("It works")
End Sub
Public Shared Sub OnLoadEnd()
TRAIN_Safe.Form1.browserCtl.Browser.GetMainFrame.ExecuteJavaScript("callFromWinForm('welcome to TRAIN Safe')", Nothing, 0)
End Sub
Public Shared Sub EXITCommand()
Application.Exit()
End Sub
Public Shared Sub checkforNewCourseFiles()
'TRAIN_Safe.Form1.browserCtl.Browser.GetMainFrame.ExecuteJavaScript("callFromWinForm('Other Stuff')", Nothing, 0)
End Sub
End Class
Look at my javascript and then look at the ProcessRequest Function to get a sense of how I am doing this...