12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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
- {
- 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+=sss;
- }
- private void sss(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;
- }
- </style>
- </head>
- <body>
- <div>
- <img src='https://teammodeltest.blob.core.chinacloudapi.cn/0-public/007.png' alt='Irregular PNG Image'>
- </div>
-
- </body>
- </html>";
- //webView.CoreWebView2.Navigate("C:/Users/CrazyIter/Downloads/clock.html");
- webView.CoreWebView2.NavigateToString(htmlContent);
- }
- }
- }
|