Form1.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Microsoft.Web.WebView2.Core;
  2. using Microsoft.Web.WebView2.WinForms;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Timers;
  13. using System.Windows.Forms;
  14. namespace PrintHtmlFrom
  15. {
  16. public partial class Form1 : Form
  17. {
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. InitBrowser();
  22. }
  23. private void InitBrowser()
  24. {
  25. AwaitCoreWebView2Async(webView21); //设置浏览器参数
  26. webView21.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = true;
  27. webView21.CoreWebView2.NavigateToString(@"D:\index.html");
  28. }
  29. private async void AwaitCoreWebView2Async(WebView2 WebView21)
  30. {
  31. string userDataFolder = null; //"你的缓存文件夹路径";
  32. CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions($"--lang=zh-CN");
  33. var environment = await CoreWebView2Environment.CreateAsync(null, userDataFolder, options);
  34. // Create a WebView with the specified environment.
  35. await WebView21.EnsureCoreWebView2Async(environment);
  36. }
  37. private void WebView2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
  38. {
  39. }
  40. private void WebView2_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  41. {
  42. }
  43. private void WebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
  44. {
  45. }
  46. private void WebView2_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
  47. {
  48. }
  49. private void Form1_Load(object sender, EventArgs e)
  50. {
  51. }
  52. }
  53. }