MainWindow.xaml.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Microsoft.Web.WebView2.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. namespace HTEXTeachLib
  16. {
  17. /// <summary>
  18. /// MainWindow.xaml 的交互逻辑
  19. /// </summary>
  20. ///WPF初探——利用Winform库中的NotifyIcon实现托盘小程序
  21. //https://blog.csdn.net/yahahi/article/details/112636760 倒计时器 (WPF)
  22. ///https://www.cnblogs.com/royenhome/archive/2010/02/02/1662243.html
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. var userDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/webview2_temp");
  29. var environment = CoreWebView2Environment.CreateAsync(null, userDataFolder).Result;
  30. // webView.EnsureCoreWebView2Async(environment);
  31. webView.Loaded += WebView_CoreWebView2InitializationCompleted;
  32. webView.PreviewDragOver += WebView2_PreviewDragOver;
  33. webView.Drop += WebView2_Drop;
  34. webView.KeyDown+=sss;
  35. }
  36. private void sss(object sender, KeyEventArgs e)
  37. {
  38. DragMove();
  39. }
  40. private void WebView2_PreviewDragOver(object sender, DragEventArgs e)
  41. {
  42. // 检查拖拽的数据类型,如果是合适的类型,设置拖拽效果为 Copy 或 Move
  43. //if (e.Data.GetDataPresent(DataFormats.Text))
  44. // e.Effects = DragDropEffects.Copy;
  45. //else
  46. // e.Effects = DragDropEffects.None;
  47. e.Effects = DragDropEffects.All;
  48. e.Handled = true;
  49. }
  50. private void WebView2_Drop(object sender, DragEventArgs e)
  51. {
  52. // 获取拖拽的数据
  53. string draggedText = e.Data.GetData(DataFormats.Text) as string;
  54. // 在 WebView2 中执行相应的操作,例如插入文本
  55. webView.ExecuteScriptAsync($"insertText('{draggedText}')");
  56. e.Handled = true;
  57. }
  58. private async void WebView_CoreWebView2InitializationCompleted(object sender, RoutedEventArgs e)
  59. {
  60. var userDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/webview2_temp");
  61. var environment = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
  62. await webView.EnsureCoreWebView2Async(environment);
  63. string htmlContent = @"
  64. <!DOCTYPE html>
  65. <html>
  66. <head>
  67. <style>
  68. body {
  69. background-color: transparent;
  70. margin: 0;
  71. padding: 0;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <div>
  77. <img src='https://teammodeltest.blob.core.chinacloudapi.cn/0-public/007.png' alt='Irregular PNG Image'>
  78. </div>
  79. </body>
  80. </html>";
  81. //webView.CoreWebView2.Navigate("C:/Users/CrazyIter/Downloads/clock.html");
  82. webView.CoreWebView2.NavigateToString(htmlContent);
  83. }
  84. }
  85. }