Form1.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Microsoft.Office.Interop.Word;
  2. using Microsoft.Web.WebView2.WinForms;
  3. using Microsoft.Web.WebView2.Wpf;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace HTEXWordTest
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. this.Load+=Form1_Load;
  21. }
  22. private async void Form1_Load(object sender, EventArgs e)
  23. {
  24. //在加载窗体时初始化 WebView2
  25. await webView21.EnsureCoreWebView2Async();
  26. // 注册自定义对象以供 JavaScript 使用
  27. // webView21.CoreWebView2.NavigateToString("<html><head><meta charset=\"utf-8\"><title>菜鸟教程(runoob.com)</title></head><body> <h1>我的第一个标题</h1> <p>我的第一个段落。</p></body></html>");
  28. // 加载网页
  29. // webView2.Source = new Uri("https://www.baidu.com");
  30. webView21.CoreWebView2.Navigate("https://cn.vuejs.org/");
  31. }
  32. private void webView21_Click(object sender, EventArgs e)
  33. {
  34. }
  35. private void button1_Click(object sender, EventArgs e)
  36. {
  37. this.Invoke(new Action(() =>
  38. {
  39. this.webView21.CoreWebView2.ExecuteScriptAsync("document.execCommand('selectAll', false, null);");
  40. }));
  41. }
  42. private async void button2_Click(object sender, EventArgs e)
  43. {
  44. //var sting = Clipboard.GetText();
  45. //string htmlContent = await webView21.ExecuteScriptAsync("document.documentElement.outerHTML");
  46. //string html = @"<html><head><meta charset=""utf-8""><title>菜鸟教程(runoob.com)</title></head><body> <h1>我的第一个标题</h1> <p>我的第一个段落。</p></body></html>";
  47. ////var html = Rtf.ToHtml(htmlContent);
  48. //// 将 HTML 内容放入剪贴板
  49. ////Clipboard.SetText(sting, TextDataFormat.Rtf);
  50. //Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.Paste();
  51. // 获取剪贴板中的数据
  52. IDataObject dataObject = Clipboard.GetDataObject();
  53. // 检查是否有图像数据
  54. if (dataObject != null && dataObject.GetDataPresent(DataFormats.Bitmap))
  55. {
  56. // 如果有图像数据,粘贴图像
  57. Bitmap bitmap = (Bitmap)dataObject.GetData(DataFormats.Bitmap);
  58. Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.InlineShapes.AddPicture("", false, true).Range.PasteSpecial(DataType: WdPasteDataType.wdPasteBitmap);
  59. }
  60. else
  61. {
  62. // 如果没有图像数据,可以尝试其他格式的粘贴
  63. Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.Paste();
  64. }
  65. // Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.Range.PasteSpecial(DataType: WdPasteDataType.wdPasteRTF);
  66. }
  67. private async void button3_Click(object sender, EventArgs e)
  68. {
  69. // 使用 JavaScript 选择整个网页内容
  70. await webView21.ExecuteScriptAsync("document.execCommand('selectall')");
  71. // 使用 JavaScript 复制选定的内容到剪贴板
  72. await webView21.ExecuteScriptAsync("document.execCommand('copy')");
  73. IDataObject dataObject = Clipboard.GetDataObject();
  74. // 检查是否有图像数据
  75. if (dataObject != null && dataObject.GetDataPresent(DataFormats.Bitmap))
  76. {
  77. // 如果有图像数据,粘贴图像
  78. Bitmap bitmap = (Bitmap)dataObject.GetData(DataFormats.Bitmap);
  79. Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.InlineShapes.AddPicture("", false, true).Range.PasteSpecial(DataType: WdPasteDataType.wdPasteBitmap);
  80. }
  81. else
  82. {
  83. // 如果没有图像数据,可以尝试其他格式的粘贴
  84. Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.Paste();
  85. }
  86. }
  87. private void button4_Click(object sender, EventArgs e)
  88. {
  89. }
  90. }
  91. }