回答編集履歴
1
追記
answer
CHANGED
@@ -1,2 +1,84 @@
|
|
1
1
|
その前に両者のコードが同じものじゃないんですが。
|
2
|
-
window あたりが怪しくありませんか?
|
2
|
+
window あたりが怪しくありませんか?
|
3
|
+
|
4
|
+
# 追記
|
5
|
+
|
6
|
+
結論から言えば、メッセージループが必要だったので、コンソールアプリでも Form を作る必要がありました。
|
7
|
+
以下がコンソールアプリでのサンプルコードです。
|
8
|
+
[WebBrowser.ObjectForScripting Property](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.webbrowser.objectforscripting?view=netframework-4.7.2#System_Windows_Forms_WebBrowser_ObjectForScripting) を参考にしました。
|
9
|
+
|
10
|
+
```C#
|
11
|
+
using System;
|
12
|
+
using System.Drawing;
|
13
|
+
using System.Drawing.Drawing2D;
|
14
|
+
using System.Runtime.InteropServices;
|
15
|
+
using System.Security.Permissions;
|
16
|
+
using System.Windows.Forms;
|
17
|
+
|
18
|
+
namespace ConsoleApp1
|
19
|
+
{
|
20
|
+
class Program
|
21
|
+
{
|
22
|
+
[STAThread]
|
23
|
+
static void Main(string[] args)
|
24
|
+
{
|
25
|
+
var result = ParserForm.Parse("Hello World");
|
26
|
+
Console.WriteLine(result ?? "(null)");
|
27
|
+
Console.ReadKey();
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
|
32
|
+
[ComVisibleAttribute(true)]
|
33
|
+
public class ParserForm : Form
|
34
|
+
{
|
35
|
+
public static string Parse(string message)
|
36
|
+
{
|
37
|
+
var region = new Region(new GraphicsPath());
|
38
|
+
var form = new ParserForm
|
39
|
+
{
|
40
|
+
Visible = false,
|
41
|
+
message = message,
|
42
|
+
FormBorderStyle = FormBorderStyle.None,
|
43
|
+
Region = region
|
44
|
+
};
|
45
|
+
form.ShowDialog();
|
46
|
+
return form.Result?.ToString();
|
47
|
+
}
|
48
|
+
|
49
|
+
private WebBrowser webBrowser1 = new WebBrowser();
|
50
|
+
private string message;
|
51
|
+
public object Result { get; set; }
|
52
|
+
|
53
|
+
public ParserForm()
|
54
|
+
{
|
55
|
+
webBrowser1.Dock = DockStyle.Fill;
|
56
|
+
Controls.Add(webBrowser1);
|
57
|
+
Load += (s, e) =>
|
58
|
+
{
|
59
|
+
webBrowser1.ObjectForScripting = this;
|
60
|
+
webBrowser1.DocumentText = @"
|
61
|
+
<html>
|
62
|
+
<head>
|
63
|
+
<meta charset=""utf-8"" />
|
64
|
+
<meta http-equiv=""X-UA-Compatible"" content=""IE=11"" />
|
65
|
+
<title>title</title>
|
66
|
+
</head>
|
67
|
+
<body>
|
68
|
+
<script>
|
69
|
+
function cs_func(text) {
|
70
|
+
window.external.Result = text;
|
71
|
+
}
|
72
|
+
</script>
|
73
|
+
<h1>Hello!</h1>
|
74
|
+
</body>
|
75
|
+
</html>
|
76
|
+
";
|
77
|
+
Application.DoEvents();
|
78
|
+
webBrowser1.Document.InvokeScript("cs_func", new[] { message });
|
79
|
+
DialogResult = DialogResult.OK;
|
80
|
+
};
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
```
|