MainWindow.xaml.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. private bool isDragging = false;
  26. private Point startPosition;
  27. private bool isWebView2Ready = false;
  28. public MainWindow()
  29. {
  30. InitializeComponent();
  31. var userDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/webview2_temp");
  32. var environment = CoreWebView2Environment.CreateAsync(null, userDataFolder).Result;
  33. // webView.EnsureCoreWebView2Async(environment);
  34. webView.Loaded += WebView_CoreWebView2InitializationCompleted;
  35. webView.PreviewDragOver += WebView2_PreviewDragOver;
  36. webView.Drop += WebView2_Drop;
  37. webView.KeyDown+=KeyDown;
  38. webView.MouseDown += WebView_MouseDown;
  39. webView.MouseMove += WebView_MouseMove;
  40. webView.MouseUp += WebView_MouseUp;
  41. webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted1;
  42. }
  43. private void WebView_CoreWebView2InitializationCompleted1(object sender, CoreWebView2InitializationCompletedEventArgs e)
  44. {
  45. if (e.IsSuccess)
  46. {
  47. webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("document.addEventListener('mousedown', function(e) { window.external.notify('mousedown'); });");
  48. webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("document.addEventListener('mouseup', function(e) { window.external.notify('mouseup'); });");
  49. webView.CoreWebView2.AddHostObjectToScript("external", new ExternalObject(this));
  50. webView.CoreWebView2.NewWindowRequested += (s, args) => args.Handled = true;
  51. }
  52. else
  53. {
  54. // Handle initialization failure
  55. }
  56. }
  57. private void WebView_MouseDown(object sender, MouseButtonEventArgs e)
  58. {
  59. if (e.ChangedButton == MouseButton.Left)
  60. {
  61. isDragging = true;
  62. startPosition = e.GetPosition(this);
  63. }
  64. }
  65. private void WebView_MouseMove(object sender, MouseEventArgs e)
  66. {
  67. if (isDragging)
  68. {
  69. Point currentPosition = e.GetPosition(this);
  70. double deltaX = currentPosition.X - startPosition.X;
  71. double deltaY = currentPosition.Y - startPosition.Y;
  72. // 移动WebView2控件
  73. Canvas.SetLeft(webView, Canvas.GetLeft(webView) + deltaX);
  74. Canvas.SetTop(webView, Canvas.GetTop(webView) + deltaY);
  75. startPosition = currentPosition;
  76. }
  77. }
  78. private void WebView_MouseUp(object sender, MouseButtonEventArgs e)
  79. {
  80. if (e.ChangedButton == MouseButton.Left)
  81. {
  82. isDragging = false;
  83. }
  84. }
  85. private void KeyDown(object sender, KeyEventArgs e)
  86. {
  87. DragMove();
  88. }
  89. private void WebView2_PreviewDragOver(object sender, DragEventArgs e)
  90. {
  91. // 检查拖拽的数据类型,如果是合适的类型,设置拖拽效果为 Copy 或 Move
  92. //if (e.Data.GetDataPresent(DataFormats.Text))
  93. // e.Effects = DragDropEffects.Copy;
  94. //else
  95. // e.Effects = DragDropEffects.None;
  96. e.Effects = DragDropEffects.All;
  97. e.Handled = true;
  98. }
  99. private void WebView2_Drop(object sender, DragEventArgs e)
  100. {
  101. // 获取拖拽的数据
  102. string draggedText = e.Data.GetData(DataFormats.Text) as string;
  103. // 在 WebView2 中执行相应的操作,例如插入文本
  104. webView.ExecuteScriptAsync($"insertText('{draggedText}')");
  105. e.Handled = true;
  106. }
  107. private async void WebView_CoreWebView2InitializationCompleted(object sender, RoutedEventArgs e)
  108. {
  109. var userDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/webview2_temp");
  110. var environment = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
  111. await webView.EnsureCoreWebView2Async(environment);
  112. string htmlContent = @"
  113. <!DOCTYPE html>
  114. <html>
  115. <head>
  116. <style>
  117. body {
  118. background-color: transparent;
  119. margin: 0;
  120. padding: 0;
  121. }
  122. .centered-content {
  123. display: flex; /* 创建 Flexbox 布局 */
  124. justify-content: center; /* 水平居中对齐 */
  125. align-items: center; /* 垂直居中对齐 */
  126. }
  127. </style>
  128. </head>
  129. <body>
  130. <div class='centered-content'>
  131. <img src='https://teammodeltest.blob.core.chinacloudapi.cn/0-public/008.svg' width='450px' alt='Irregular PNG Image'>
  132. </div>
  133. </body>
  134. </html>";
  135. //webView.CoreWebView2.Navigate("C:/Users/CrazyIter/Downloads/clock.html");
  136. webView.CoreWebView2.NavigateToString(htmlContent);
  137. await webView.CoreWebView2.ExecuteScriptAsync("window.addEventListener('contextmenu', window => {window.preventDefault();});");
  138. }
  139. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  140. {
  141. DragMove();
  142. }
  143. }
  144. public class ExternalObject
  145. {
  146. private MainWindow mainWindow;
  147. public ExternalObject(MainWindow window)
  148. {
  149. mainWindow = window;
  150. }
  151. public void Notify(string message)
  152. {
  153. if (message == "mousedown")
  154. {
  155. // Handle mouse down event from JavaScript
  156. mainWindow.Dispatcher.Invoke(() =>
  157. {
  158. if (Mouse.LeftButton == MouseButtonState.Pressed)
  159. mainWindow.DragMove();
  160. });
  161. }
  162. else if (message == "mouseup")
  163. {
  164. // Handle mouse up event from JavaScript
  165. }
  166. }
  167. }
  168. public class DragDropHelper
  169. {
  170. private MainWindow mainWindow;
  171. private bool isDragging = false;
  172. private double offsetX, offsetY;
  173. public DragDropHelper(MainWindow window)
  174. {
  175. mainWindow = window;
  176. }
  177. public void onMouseDown(double clientX, double clientY)
  178. {
  179. isDragging = true;
  180. offsetX = clientX - mainWindow.Left;
  181. offsetY = clientY - mainWindow.Top;
  182. }
  183. public void onMouseMove(double clientX, double clientY)
  184. {
  185. if (isDragging)
  186. {
  187. mainWindow.Left = clientX - offsetX;
  188. mainWindow.Top = clientY - offsetY;
  189. }
  190. }
  191. public void onMouseUp()
  192. {
  193. isDragging = false;
  194. }
  195. }
  196. }