|
@@ -23,19 +23,75 @@ namespace HTEXTeachLib
|
|
///https://www.cnblogs.com/royenhome/archive/2010/02/02/1662243.html
|
|
///https://www.cnblogs.com/royenhome/archive/2010/02/02/1662243.html
|
|
public partial class MainWindow : Window
|
|
public partial class MainWindow : Window
|
|
{
|
|
{
|
|
|
|
+ private bool isDragging = false;
|
|
|
|
+ private Point startPosition;
|
|
|
|
+ private bool isWebView2Ready = false;
|
|
public MainWindow()
|
|
public MainWindow()
|
|
{
|
|
{
|
|
InitializeComponent();
|
|
InitializeComponent();
|
|
var userDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/webview2_temp");
|
|
var userDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/webview2_temp");
|
|
- var environment = CoreWebView2Environment.CreateAsync(null, userDataFolder).Result;
|
|
|
|
|
|
+ var environment = CoreWebView2Environment.CreateAsync(null, userDataFolder).Result;
|
|
// webView.EnsureCoreWebView2Async(environment);
|
|
// webView.EnsureCoreWebView2Async(environment);
|
|
webView.Loaded += WebView_CoreWebView2InitializationCompleted;
|
|
webView.Loaded += WebView_CoreWebView2InitializationCompleted;
|
|
webView.PreviewDragOver += WebView2_PreviewDragOver;
|
|
webView.PreviewDragOver += WebView2_PreviewDragOver;
|
|
webView.Drop += WebView2_Drop;
|
|
webView.Drop += WebView2_Drop;
|
|
- webView.KeyDown+=sss;
|
|
|
|
|
|
+ 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 sss(object sender, KeyEventArgs e)
|
|
|
|
|
|
+ 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();
|
|
DragMove();
|
|
}
|
|
}
|
|
@@ -60,7 +116,7 @@ namespace HTEXTeachLib
|
|
webView.ExecuteScriptAsync($"insertText('{draggedText}')");
|
|
webView.ExecuteScriptAsync($"insertText('{draggedText}')");
|
|
|
|
|
|
e.Handled = true;
|
|
e.Handled = true;
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
private async void WebView_CoreWebView2InitializationCompleted(object sender, RoutedEventArgs e)
|
|
private async void WebView_CoreWebView2InitializationCompleted(object sender, RoutedEventArgs e)
|
|
{
|
|
{
|
|
@@ -78,17 +134,90 @@ namespace HTEXTeachLib
|
|
margin: 0;
|
|
margin: 0;
|
|
padding: 0;
|
|
padding: 0;
|
|
}
|
|
}
|
|
|
|
+ .centered-content {
|
|
|
|
+ display: flex; /* 创建 Flexbox 布局 */
|
|
|
|
+ justify-content: center; /* 水平居中对齐 */
|
|
|
|
+ align-items: center; /* 垂直居中对齐 */
|
|
|
|
+ }
|
|
</style>
|
|
</style>
|
|
</head>
|
|
</head>
|
|
<body>
|
|
<body>
|
|
- <div>
|
|
|
|
- <img src='https://teammodeltest.blob.core.chinacloudapi.cn/0-public/007.png' alt='Irregular PNG Image'>
|
|
|
|
|
|
+ <div class='centered-content'>
|
|
|
|
+ <img src='https://teammodeltest.blob.core.chinacloudapi.cn/0-public/008.svg' width='450px' alt='Irregular PNG Image'>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</body>
|
|
</body>
|
|
</html>";
|
|
</html>";
|
|
//webView.CoreWebView2.Navigate("C:/Users/CrazyIter/Downloads/clock.html");
|
|
//webView.CoreWebView2.Navigate("C:/Users/CrazyIter/Downloads/clock.html");
|
|
webView.CoreWebView2.NavigateToString(htmlContent);
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|