|
| 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