-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
75 lines (65 loc) · 2.29 KB
/
Copy pathForm1.cs
File metadata and controls
75 lines (65 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CefSharpCallFromJavaScript
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeChromium();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
private ChromiumWebBrowser _browser;
public void InitializeChromium()
{
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
CefSettings settings = new CefSettings();
// これを入れないと黒い余白が発生していまう。
Cef.EnableHighDPISupport();
Cef.Initialize(settings);
// こちらの接続先はWeb側でデバッグ実行した時に表示されるローカルの接続先をコピペしてください。
_browser = new ChromiumWebBrowser("https://localhost:44315/");
Controls.Add(_browser);
_browser.Dock = DockStyle.Fill;
var eventObject = new ScriptedMethodsBoundObject();
eventObject.EventArrived += OnJavascriptEventArrived;
_browser.RegisterJsObject("boundEvent", eventObject, options: BindingOptions.DefaultBinder);
}
public class ScriptedMethodsBoundObject
{
public event Action<string, object> EventArrived;
public void RaiseEvent(string eventName, object eventData = null)
{
if (EventArrived != null)
{
EventArrived(eventName, eventData);
}
}
}
public static void OnJavascriptEventArrived(string eventName, object eventData)
{
var jsonString = eventData.ToString();
string path = string.Empty;
if (jsonString.Contains("memo"))
{
path = @"notepad.exe";
Process.Start(path);
}
Console.WriteLine("Event arrived: {0}", eventName); // output 'click'
}
}
}