WinForms_GettingStarted

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;

namespace WinForms_GettingStarted
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Resize += new System.EventHandler(this.Form_Resize);
            webView.NavigationStarting += EnsureHttps;
            InitializeAsync();
        }

        private void Form_Resize(object sender, EventArgs e)
        {
            webView.Size = this.ClientSize - new System.Drawing.Size(webView.Location);
            //goButton.Left = this.ClientSize.Width - goButton.Width;
            //addressBar.Width = goButton.Left - addressBar.Left;
        }

        void EnsureHttps(object sender, CoreWebView2NavigationStartingEventArgs args)
        {
            //String uri = args.Uri;
            //if (!uri.StartsWith("https://"))
            //{
            //    webView.CoreWebView2.ExecuteScriptAsync($"alert('{uri} is not safe, try an https link')");
            //    args.Cancel = true;
            //}
        }

        async void InitializeAsync()
        {
            await webView.EnsureCoreWebView2Async(null);
            webView.CoreWebView2.WebMessageReceived += UpdateAddressBar;

            webView.CoreWebView2.AddHostObjectToScript("OS", new WebView2Script(this));
            webView.CoreWebView2.Navigate("file:///" + AppDomain.CurrentDomain.BaseDirectory + "home\\index.html");

            //webView.NavigateToString(File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "home\\index.html"));
            await webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.chrome.webview.postMessage(window.document.URL);");
            await webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.chrome.webview.addEventListener(\'message\', event => alert(event.data));");

        }

        void UpdateAddressBar(object sender, CoreWebView2WebMessageReceivedEventArgs args)
        {


            String uri = args.TryGetWebMessageAsString();
            //addressBar.Text = uri;
            webView.CoreWebView2.PostWebMessageAsString(uri);
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            //this.webView.Source = new Uri("file:///" + AppDomain.CurrentDomain.BaseDirectory + "home\\index.html");
            //webView.CoreWebView2.Navigate("file:///" + AppDomain.CurrentDomain.BaseDirectory + "home\\index.html");

    
            //webView.CoreWebView2.AddHostObjectToScript("OS", new WebView2Script(this));
        }

        private void webView21_Click(object sender, EventArgs e)
        {

        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForms_GettingStarted
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class WebView2Script
    {
        Form form;
        public WebView2Script(Form formmain) {
            this.form = formmain;
        }
        public void CloseFrom() {
            form.Close();
        }
        public void HidenFrom()
        {
            form.Hide();
        }
        public void MinFrom()
        {
            form.WindowState= FormWindowState.Minimized;
        }
        public void MaxFrom()
        {
            form.WindowState = FormWindowState.Maximized;
        }
    }
}
 

你可能感兴趣的:(c#)