mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-09 07:56:08 +02:00
Update webbrowser sample (#173)
* Update webbrowser sample As per suggestions https://github.com/dotnet/winforms/issues/4443#issuecomment-759333446 Resolves dotnet/winforms#4443 * fixup! Update webbrowser sample * fixup! Update webbrowser sample * Update dotnet-desktop-guide/framework/winforms/controls/implement-two-way-com-between-dhtml-and-client.md Co-authored-by: Andy De George <[email protected]> Co-authored-by: Andy De George <[email protected]>
This commit is contained in:
co-authored by
Andy De George
parent
232f1b91e2
commit
1fe35d6424
+2
-7
@@ -38,19 +38,14 @@ The <xref:System.Windows.Forms.WebBrowser> control lets you implement two-way co
|
|||||||
[!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#3)]
|
[!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#3)]
|
||||||
[!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#3)]
|
[!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#3)]
|
||||||
|
|
||||||
4. Set the <xref:System.Windows.Forms.WebBrowser.ObjectForScripting%2A> property in the form's constructor or a <xref:System.Windows.Forms.Form.Load> event handler.
|
4. Set the <xref:System.Windows.Forms.WebBrowser.ObjectForScripting%2A> property in the form's constructor or override the <xref:System.Windows.Forms.Form.OnLoad%2A> method.
|
||||||
|
|
||||||
The following code uses the form class itself for the scripting object.
|
The following code uses the form class itself for the scripting object.
|
||||||
|
|
||||||
> [!NOTE]
|
|
||||||
> Component Object Model (COM) must be able to access the scripting object. To make your form visible to COM, add the <xref:System.Runtime.InteropServices.ComVisibleAttribute> attribute to your form class.
|
|
||||||
|
|
||||||
[!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#4](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#4)]
|
[!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#4](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#4)]
|
||||||
[!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#4](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#4)]
|
[!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#4](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#4)]
|
||||||
|
|
||||||
5. Implement public properties or methods in your application code that your script code will use.
|
5. Implement your scripting object.
|
||||||
|
|
||||||
For example, if you use the form class for the scripting object, add the following code to your form class.
|
|
||||||
|
|
||||||
[!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#5](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#5)]
|
[!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#5](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#5)]
|
||||||
[!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#5](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#5)]
|
[!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#5](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#5)]
|
||||||
|
|||||||
+21
-13
@@ -1,10 +1,7 @@
|
|||||||
//<snippet0>
|
//<snippet0>
|
||||||
using System;
|
using System;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using System.Security.Permissions;
|
|
||||||
|
|
||||||
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
|
|
||||||
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
|
|
||||||
public class Form1 : Form
|
public class Form1 : Form
|
||||||
{
|
{
|
||||||
private WebBrowser webBrowser1 = new WebBrowser();
|
private WebBrowser webBrowser1 = new WebBrowser();
|
||||||
@@ -25,11 +22,12 @@ public class Form1 : Form
|
|||||||
webBrowser1.Dock = DockStyle.Fill;
|
webBrowser1.Dock = DockStyle.Fill;
|
||||||
Controls.Add(webBrowser1);
|
Controls.Add(webBrowser1);
|
||||||
Controls.Add(button1);
|
Controls.Add(button1);
|
||||||
Load += new EventHandler(Form1_Load);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Form1_Load(object sender, EventArgs e)
|
protected override void OnLoad(EventArgs e)
|
||||||
{
|
{
|
||||||
|
base.OnLoad(e);
|
||||||
|
|
||||||
//<snippet1>
|
//<snippet1>
|
||||||
webBrowser1.AllowWebBrowserDrop = false;
|
webBrowser1.AllowWebBrowserDrop = false;
|
||||||
//</snippet1>
|
//</snippet1>
|
||||||
@@ -40,7 +38,7 @@ public class Form1 : Form
|
|||||||
webBrowser1.WebBrowserShortcutsEnabled = false;
|
webBrowser1.WebBrowserShortcutsEnabled = false;
|
||||||
//</snippet3>
|
//</snippet3>
|
||||||
//<snippet4>
|
//<snippet4>
|
||||||
webBrowser1.ObjectForScripting = this;
|
webBrowser1.ObjectForScripting = new MyScriptObject(this);
|
||||||
//</snippet4>
|
//</snippet4>
|
||||||
//<snippet9>
|
//<snippet9>
|
||||||
// Uncomment the following line when you are finished debugging.
|
// Uncomment the following line when you are finished debugging.
|
||||||
@@ -56,13 +54,6 @@ public class Form1 : Form
|
|||||||
"</body></html>";
|
"</body></html>";
|
||||||
}
|
}
|
||||||
|
|
||||||
//<snippet5>
|
|
||||||
public void Test(String message)
|
|
||||||
{
|
|
||||||
MessageBox.Show(message, "client code");
|
|
||||||
}
|
|
||||||
//</snippet5>
|
|
||||||
|
|
||||||
private void button1_Click(object sender, EventArgs e)
|
private void button1_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
//<snippet8>
|
//<snippet8>
|
||||||
@@ -71,4 +62,21 @@ public class Form1 : Form
|
|||||||
//</snippet8>
|
//</snippet8>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//<snippet5>
|
||||||
|
public class MyScriptObject
|
||||||
|
{
|
||||||
|
private Form1 _form;
|
||||||
|
|
||||||
|
public MyScriptObject(Form1 form)
|
||||||
|
{
|
||||||
|
_form = form;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Test(string message)
|
||||||
|
{
|
||||||
|
MessageBox.Show(message, "client code");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//</snippet5>
|
||||||
//</snippet0>
|
//</snippet0>
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>WinExe</OutputType>
|
||||||
|
<TargetFrameworks>net472;netcoreapp3.1;net5.0-windows</TargetFrameworks>
|
||||||
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
+18
-12
@@ -1,9 +1,6 @@
|
|||||||
'<snippet0>
|
'<snippet0>
|
||||||
Imports System.Windows.Forms
|
Imports System.Windows.Forms
|
||||||
Imports System.Security.Permissions
|
|
||||||
|
|
||||||
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
|
|
||||||
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
|
|
||||||
Public Class Form1
|
Public Class Form1
|
||||||
Inherits Form
|
Inherits Form
|
||||||
|
|
||||||
@@ -24,8 +21,8 @@ Public Class Form1
|
|||||||
Controls.Add(button1)
|
Controls.Add(button1)
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
|
Protected Overrides Sub OnLoad(e As EventArgs)
|
||||||
Handles Me.Load
|
MyBase.OnLoad(e)
|
||||||
|
|
||||||
'<snippet1>
|
'<snippet1>
|
||||||
webBrowser1.AllowWebBrowserDrop = False
|
webBrowser1.AllowWebBrowserDrop = False
|
||||||
@@ -37,7 +34,7 @@ Public Class Form1
|
|||||||
webBrowser1.WebBrowserShortcutsEnabled = False
|
webBrowser1.WebBrowserShortcutsEnabled = False
|
||||||
'</snippet3>
|
'</snippet3>
|
||||||
'<snippet4>
|
'<snippet4>
|
||||||
webBrowser1.ObjectForScripting = Me
|
webBrowser1.ObjectForScripting = New MyScriptObject(Me)
|
||||||
'</snippet4>
|
'</snippet4>
|
||||||
'<snippet9>
|
'<snippet9>
|
||||||
' Uncomment the following line when you are finished debugging.
|
' Uncomment the following line when you are finished debugging.
|
||||||
@@ -53,12 +50,6 @@ Public Class Form1
|
|||||||
"</body></html>"
|
"</body></html>"
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
'<snippet5>
|
|
||||||
Public Sub Test(ByVal message As String)
|
|
||||||
MessageBox.Show(message, "client code")
|
|
||||||
End Sub
|
|
||||||
'</snippet5>
|
|
||||||
|
|
||||||
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
|
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
|
||||||
Handles button1.Click
|
Handles button1.Click
|
||||||
|
|
||||||
@@ -70,4 +61,19 @@ Public Class Form1
|
|||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
End Class
|
End Class
|
||||||
|
|
||||||
|
'<snippet5>
|
||||||
|
Public Class MyScriptObject
|
||||||
|
Private _form As Form1
|
||||||
|
|
||||||
|
Public Sub New(ByVal form As Form1)
|
||||||
|
_form = form
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Public Sub Test(ByVal message As String)
|
||||||
|
MessageBox.Show(message, "client code")
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
End Class
|
||||||
|
'</snippet5>
|
||||||
'</snippet0>
|
'</snippet0>
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>WinExe</OutputType>
|
||||||
|
<TargetFrameworks>net472;net5.0-windows</TargetFrameworks>
|
||||||
|
<RootNamespace>sample</RootNamespace>
|
||||||
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
|
<MyType>WindowsForms</MyType>
|
||||||
|
<StartupObject>sample.Form1</StartupObject>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user