Skip to content

Commit 9043f7f

Browse files
committed
Add WinMD sample for WinForms
1 parent c4ef523 commit 9043f7f

File tree

15 files changed

+672
-2
lines changed

15 files changed

+672
-2
lines changed

Directory.Build.props

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
<ItemGroup>
2222
<PackageReference Include="StyleCop.Analyzers" Version="1.*" PrivateAssets="all" />
2323
<PackageReference Include="Nerdbank.GitVersioning" Version=" 2.*" PrivateAssets="all" />
24-
<PackageReference Include="Microsoft.Toolkit.Forms.UI.Controls.WebView" Version="5.0.0-preview.gb86cb1c4cb" Condition="'$(IsFormsProject)' == 'true'" />
25-
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.Controls.WebView" Version="5.0.0-preview.gb86cb1c4cb" Condition="'$(IsWPFProject)' == 'true'" />
2624
</ItemGroup>
2725
<ItemGroup>
2826
<AdditionalFiles Include="$(MSBuildThisFileDirectory)\stylecop.json">
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project>
2+
<Import Project="../../Directory.Build.props" />
3+
<ItemGroup>
4+
<PackageReference Include="Microsoft.Toolkit.Forms.UI.Controls.WebView" Version="5.0.0-preview.gb86cb1c4cb" Condition="'$(IsFormsProject)' == 'true'" />
5+
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.Controls.WebView" Version="5.0.0-preview.gb86cb1c4cb" Condition="'$(IsWPFProject)' == 'true'" />
6+
</ItemGroup>
7+
</Project>

WebViewSamples.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Windows Forms", "Windows Fo
3535
EndProject
3636
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Windows Presentation Foundation", "Windows Presentation Foundation", "{000AB802-D6FB-4DB5-9880-7CCFE1BAB061}"
3737
EndProject
38+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "dotnet", "dotnet", "{CD110DB5-8689-429B-9000-7E1DD6EA96A9}"
39+
EndProject
40+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebViewSamples.Forms.WinMD", "WinMD\dotnet\WebViewSamples.Forms.WinMD\WebViewSamples.Forms.WinMD.csproj", "{37CDAC79-19D1-4188-B902-57D5FDDA8103}"
41+
EndProject
3842
Global
3943
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4044
Debug|Any CPU = Debug|Any CPU
@@ -197,6 +201,22 @@ Global
197201
{08EE70C2-676D-425B-9633-1C9B84605562}.Release|x64.Build.0 = Release|Any CPU
198202
{08EE70C2-676D-425B-9633-1C9B84605562}.Release|x86.ActiveCfg = Release|Any CPU
199203
{08EE70C2-676D-425B-9633-1C9B84605562}.Release|x86.Build.0 = Release|Any CPU
204+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
205+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Debug|Any CPU.Build.0 = Debug|Any CPU
206+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Debug|Win32.ActiveCfg = Debug|Any CPU
207+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Debug|Win32.Build.0 = Debug|Any CPU
208+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Debug|x64.ActiveCfg = Debug|Any CPU
209+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Debug|x64.Build.0 = Debug|Any CPU
210+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Debug|x86.ActiveCfg = Debug|Any CPU
211+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Debug|x86.Build.0 = Debug|Any CPU
212+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Release|Any CPU.ActiveCfg = Release|Any CPU
213+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Release|Any CPU.Build.0 = Release|Any CPU
214+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Release|Win32.ActiveCfg = Release|Any CPU
215+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Release|Win32.Build.0 = Release|Any CPU
216+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Release|x64.ActiveCfg = Release|Any CPU
217+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Release|x64.Build.0 = Release|Any CPU
218+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Release|x86.ActiveCfg = Release|Any CPU
219+
{37CDAC79-19D1-4188-B902-57D5FDDA8103}.Release|x86.Build.0 = Release|Any CPU
200220
EndGlobalSection
201221
GlobalSection(SolutionProperties) = preSolution
202222
HideSolutionNode = FALSE
@@ -216,6 +236,8 @@ Global
216236
{7698F53A-CB4E-4669-B7C7-6F694D26202D} = {C2B36E66-2BB3-4F55-9E43-F72299D9555B}
217237
{C722670B-ABAC-455D-A215-DDF73492AD2C} = {7698F53A-CB4E-4669-B7C7-6F694D26202D}
218238
{000AB802-D6FB-4DB5-9880-7CCFE1BAB061} = {7698F53A-CB4E-4669-B7C7-6F694D26202D}
239+
{CD110DB5-8689-429B-9000-7E1DD6EA96A9} = {0DBA828C-4767-4C69-B7BA-71028577CFDC}
240+
{37CDAC79-19D1-4188-B902-57D5FDDA8103} = {CD110DB5-8689-429B-9000-7E1DD6EA96A9}
219241
EndGlobalSection
220242
GlobalSection(ExtensibilityGlobals) = postSolution
221243
SolutionGuid = {21952B92-234C-448F-A809-F178F04019D1}

WinMD/dotnet/Directory.Build.props

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project>
2+
<Import Project="../../Directory.Build.props" />
3+
<PropertyGroup>
4+
<TargetPlatformVersion>10.0.17763.0</TargetPlatformVersion>
5+
<TargetPlatformMinVersion>10.0.17134.0</TargetPlatformMinVersion>
6+
</PropertyGroup>
7+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project>
2+
<Import Project="../../Directory.Build.targets" />
3+
<ItemGroup>
4+
<Reference Include="Windows">
5+
<HintPath Condition="Exists('$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\$(TargetPlatformMinVersion)\Windows.winmd')">$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\$(TargetPlatformMinVersion)\Windows.winmd</HintPath>
6+
<HintPath Condition="Exists('$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\$(TargetPlatformVersion)\Windows.winmd')">$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\$(TargetPlatformVersion)\Windows.winmd</HintPath>
7+
<Private>False</Private>
8+
</Reference>
9+
<Reference Include="System.Runtime.WindowsRuntime">
10+
<HintPath>$(WINDIR)\Microsoft.NET\Framework\v4.0.30319\System.Runtime.WindowsRuntime.dll</HintPath>
11+
</Reference>
12+
</ItemGroup>
13+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
5+
</startup>
6+
</configuration>

WinMD/dotnet/WebViewSamples.Forms.WinMD/Form1.Designer.cs

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Data;
5+
using System.Drawing;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using System.Windows.Forms;
10+
using Windows.Foundation;
11+
using Windows.Web.UI;
12+
using Windows.Web.UI.Interop;
13+
14+
namespace WebViewSamples.Forms.WinMD
15+
{
16+
public partial class Form1 : Form
17+
{
18+
private bool isFullScreen;
19+
20+
public Form1()
21+
{
22+
InitializeComponent();
23+
}
24+
25+
private async void Form1_Load(object sender, EventArgs e)
26+
{
27+
var options = new WebViewControlProcessOptions
28+
{
29+
// Enable private network access to enable Enterprise SSO
30+
PrivateNetworkClientServerCapability = WebViewControlProcessCapabilityState.Enabled
31+
};
32+
var process = new WebViewControlProcess(options);
33+
var control = await process.CreateWebViewControlAsync(
34+
(long)this.Handle,
35+
new Windows.Foundation.Rect(0.0f, 0.0f, Width, Height));
36+
37+
this.Layout += (o, a) =>
38+
{
39+
// This event is raised once at startup with the AffectedControl and AffectedProperty properties
40+
// on the LayoutEventArgs as null.
41+
if (a.AffectedControl != null && a.AffectedProperty != null)
42+
{
43+
// Ensure that the affected property is the Bounds property to the control
44+
if (a.AffectedProperty == nameof(this.Bounds))
45+
{
46+
// In a typical control the DisplayRectangle is the interior canvas of the control
47+
// and in a scrolling control the DisplayRectangle would be larger than the ClientRectangle.
48+
// However, that is abstracted from us in WebView so we need to synchronize the ClientRectangle
49+
// and permit WebView to handle scrolling based on the new viewport
50+
var rect = new Rect(
51+
this.ClientRectangle.X,
52+
this.ClientRectangle.Y,
53+
this.ClientRectangle.Width,
54+
this.ClientRectangle.Height);
55+
56+
control.Bounds = rect;
57+
}
58+
}
59+
};
60+
61+
control.ContainsFullScreenElementChanged += (o, a) =>
62+
{
63+
void EnterFullScreen()
64+
{
65+
this.WindowState = FormWindowState.Normal;
66+
this.FormBorderStyle = FormBorderStyle.None;
67+
this.WindowState = FormWindowState.Maximized;
68+
}
69+
70+
void LeaveFullScreen()
71+
{
72+
this.FormBorderStyle = FormBorderStyle.Sizable;
73+
this.WindowState = FormWindowState.Normal;
74+
}
75+
76+
// Toggle
77+
this.isFullScreen = !this.isFullScreen;
78+
79+
if (this.isFullScreen)
80+
{
81+
EnterFullScreen();
82+
}
83+
else
84+
{
85+
LeaveFullScreen();
86+
}
87+
};
88+
89+
control.ScriptNotify += (o, a) => { MessageBox.Show(a.Value, a.Uri?.ToString() ?? string.Empty); };
90+
91+
control.NavigationCompleted += (o, a) =>
92+
{
93+
this.Text = o.DocumentTitle;
94+
if (!a.IsSuccess)
95+
{
96+
MessageBox.Show(
97+
$"Could not navigate to {a.Uri}",
98+
$"Error: {a.WebErrorStatus}",
99+
MessageBoxButtons.OK,
100+
MessageBoxIcon.Error);
101+
}
102+
};
103+
104+
control.NavigationStarting += (o, a) =>
105+
{
106+
this.Text = "Navigating " + (a.Uri?.ToString() ?? string.Empty);
107+
};
108+
109+
control.PermissionRequested += (o, a) =>
110+
{
111+
if (a.PermissionRequest.State == WebViewControlPermissionState.Allow)
112+
{
113+
return;
114+
}
115+
116+
var msg = $"Allow {a.PermissionRequest.Uri.Host} to access {a.PermissionRequest.PermissionType}?";
117+
118+
var response = MessageBox.Show(msg, "Permission Request", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
119+
120+
if (response == DialogResult.Yes)
121+
{
122+
if (a.PermissionRequest.State == WebViewControlPermissionState.Defer)
123+
{
124+
o.GetDeferredPermissionRequestById(a.PermissionRequest.Id, out var permission);
125+
permission?.Allow();
126+
}
127+
else
128+
{
129+
a.PermissionRequest.Allow();
130+
}
131+
}
132+
else
133+
{
134+
if (a.PermissionRequest.State == WebViewControlPermissionState.Defer)
135+
{
136+
o.GetDeferredPermissionRequestById(a.PermissionRequest.Id, out var permission);
137+
permission?.Deny();
138+
}
139+
else
140+
{
141+
a.PermissionRequest.Deny();
142+
}
143+
}
144+
};
145+
146+
control.Navigate(new Uri("http://bing.com"));
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)