123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- using Microsoft.Web.WebView2.Core;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace HTEXTeachLib
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- ///WPF初探——利用Winform库中的NotifyIcon实现托盘小程序
- //https://blog.csdn.net/yahahi/article/details/112636760 倒计时器 (WPF)
- ///https://www.cnblogs.com/royenhome/archive/2010/02/02/1662243.html
- public partial class MainWindow : Window
- {
- private bool isDragging = false;
- private Point startPosition;
- private bool isWebView2Ready = false;
- public MainWindow()
- {
- InitializeComponent();
- var userDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/webview2_temp");
- var environment = CoreWebView2Environment.CreateAsync(null, userDataFolder).Result;
- // webView.EnsureCoreWebView2Async(environment);
- webView.Loaded += WebView_CoreWebView2InitializationCompleted;
- webView.PreviewDragOver += WebView2_PreviewDragOver;
- webView.Drop += WebView2_Drop;
- webView.KeyDown+=KeyDown;
- webView.MouseDown += WebView_MouseDown;
- webView.MouseMove += WebView_MouseMove;
- webView.MouseUp += WebView_MouseUp;
- webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted1;
- }
- private void WebView_CoreWebView2InitializationCompleted1(object sender, CoreWebView2InitializationCompletedEventArgs e)
- {
- if (e.IsSuccess)
- {
- webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("document.addEventListener('mousedown', function(e) { window.external.notify('mousedown'); });");
- webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("document.addEventListener('mouseup', function(e) { window.external.notify('mouseup'); });");
- webView.CoreWebView2.AddHostObjectToScript("external", new ExternalObject(this));
- webView.CoreWebView2.NewWindowRequested += (s, args) => args.Handled = true;
- }
- else
- {
- // Handle initialization failure
- }
- }
- private void WebView_MouseDown(object sender, MouseButtonEventArgs e)
- {
- if (e.ChangedButton == MouseButton.Left)
- {
- isDragging = true;
- startPosition = e.GetPosition(this);
- }
- }
- private void WebView_MouseMove(object sender, MouseEventArgs e)
- {
- if (isDragging)
- {
- Point currentPosition = e.GetPosition(this);
- double deltaX = currentPosition.X - startPosition.X;
- double deltaY = currentPosition.Y - startPosition.Y;
- // 移动WebView2控件
- Canvas.SetLeft(webView, Canvas.GetLeft(webView) + deltaX);
- Canvas.SetTop(webView, Canvas.GetTop(webView) + deltaY);
- startPosition = currentPosition;
- }
- }
- private void WebView_MouseUp(object sender, MouseButtonEventArgs e)
- {
- if (e.ChangedButton == MouseButton.Left)
- {
- isDragging = false;
- }
- }
- private void KeyDown(object sender, KeyEventArgs e)
- {
- DragMove();
- }
- private void WebView2_PreviewDragOver(object sender, DragEventArgs e)
- {
- // 检查拖拽的数据类型,如果是合适的类型,设置拖拽效果为 Copy 或 Move
- //if (e.Data.GetDataPresent(DataFormats.Text))
- // e.Effects = DragDropEffects.Copy;
- //else
- // e.Effects = DragDropEffects.None;
- e.Effects = DragDropEffects.All;
- e.Handled = true;
- }
- private void WebView2_Drop(object sender, DragEventArgs e)
- {
- // 获取拖拽的数据
- string draggedText = e.Data.GetData(DataFormats.Text) as string;
- // 在 WebView2 中执行相应的操作,例如插入文本
- webView.ExecuteScriptAsync($"insertText('{draggedText}')");
- e.Handled = true;
- }
- private async void WebView_CoreWebView2InitializationCompleted(object sender, RoutedEventArgs e)
- {
- var userDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/webview2_temp");
- var environment = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
- await webView.EnsureCoreWebView2Async(environment);
- string htmlContent = @"
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- body {
- background-color: transparent;
- margin: 0;
- padding: 0;
- }
- .centered-content {
- display: flex; /* 创建 Flexbox 布局 */
- justify-content: center; /* 水平居中对齐 */
- align-items: center; /* 垂直居中对齐 */
- }
- </style>
- </head>
- <body>
- <div class='centered-content'>
- <img src='https://teammodeltest.blob.core.chinacloudapi.cn/0-public/008.svg' width='450px' alt='Irregular PNG Image'>
- </div>
-
- </body>
- </html>";
- //webView.CoreWebView2.Navigate("C:/Users/CrazyIter/Downloads/clock.html");
- webView.CoreWebView2.NavigateToString(htmlContent);
- await webView.CoreWebView2.ExecuteScriptAsync("window.addEventListener('contextmenu', window => {window.preventDefault();});");
-
- }
- private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- DragMove();
- }
- }
- public class ExternalObject
- {
- private MainWindow mainWindow;
- public ExternalObject(MainWindow window)
- {
- mainWindow = window;
- }
- public void Notify(string message)
- {
- if (message == "mousedown")
- {
- // Handle mouse down event from JavaScript
- mainWindow.Dispatcher.Invoke(() =>
- {
- if (Mouse.LeftButton == MouseButtonState.Pressed)
- mainWindow.DragMove();
- });
- }
- else if (message == "mouseup")
- {
- // Handle mouse up event from JavaScript
- }
- }
- }
- public class DragDropHelper
- {
- private MainWindow mainWindow;
- private bool isDragging = false;
- private double offsetX, offsetY;
- public DragDropHelper(MainWindow window)
- {
- mainWindow = window;
- }
- public void onMouseDown(double clientX, double clientY)
- {
- isDragging = true;
- offsetX = clientX - mainWindow.Left;
- offsetY = clientY - mainWindow.Top;
- }
- public void onMouseMove(double clientX, double clientY)
- {
- if (isDragging)
- {
- mainWindow.Left = clientX - offsetX;
- mainWindow.Top = clientY - offsetY;
- }
- }
- public void onMouseUp()
- {
- isDragging = false;
- }
- }
- }
|