CrazyIter_Bin vor 1 Jahr
Ursprung
Commit
dadebde540

+ 15 - 14
HTEXScreen/Controllers/IndexController.cs

@@ -44,22 +44,23 @@ namespace HTEXScreen.Controllers
         [RequestSizeLimit(102_400_000_00)] //最大10000m左右
         public   async Task<IActionResult> HttpLog(JsonElement json)
         {
-            string data = json.ToJsonString();
-            var gmt8Time = DateTimeOffset.Now.GetGMTTime(8);
-            var appendBlob = _azureStorage.GetBlobContainerClient("0-service-log").GetAppendBlobClient($"http-log/{gmt8Time:yyyy}/{gmt8Time:MM}/{gmt8Time:dd}/{gmt8Time:HH}.log");
-            if (!appendBlob.Exists())
-            {
-               await appendBlob.CreateAsync();
-                using var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{data},\n"));
-               _=   appendBlob.AppendBlockAsync(stream);
+            try {
+                string data = json.ToJsonString();
+                var gmt8Time = DateTimeOffset.Now.GetGMTTime(8);
+                var appendBlob = _azureStorage.GetBlobContainerClient("0-service-log").GetAppendBlobClient($"http-log/{gmt8Time:yyyy}/{gmt8Time:MM}/{gmt8Time:dd}/{gmt8Time:HH}.log");
+                if (!await appendBlob.ExistsAsync())
+                {
+                    await appendBlob.CreateAsync();
+                }
+                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{data},\n")))
+                {
+                    await appendBlob.AppendBlockAsync(stream);
+                }
+                return Ok(new { code = 1 });
             }
-            else
-            {
-                using var stream = new MemoryStream(Encoding.UTF8.GetBytes($"{data},\n"));
-                _= appendBlob.AppendBlockAsync(stream);
+            catch (Exception ex) {
+                return Ok(new { code = 1 });
             }
-            return Ok(new { code=1});
         }
-
     }
 }

+ 22 - 20
HTEXTeach/MainRibbon.cs

@@ -14,8 +14,8 @@ namespace HTEXTeach
     {
         private void MainRibbon_Load(object sender, RibbonUIEventArgs e)
         {
-           
-         
+
+
         }
         public bool IsEditingMode()
         {
@@ -70,34 +70,36 @@ namespace HTEXTeach
 
             Microsoft.Office.Interop.PowerPoint.Presentation presentation = Globals.ThisAddIn.Application.ActivePresentation;
             // 在演示文稿中添加新的幻灯片
-            Microsoft.Office.Interop.PowerPoint.Slide newSlide = presentation.Slides.Add(presentation.Slides.Count + 1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank);
+            // Microsoft.Office.Interop.PowerPoint.Slide newSlide = presentation.Slides.Add(presentation.Slides.Count + 1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank);
 
             //newSlide.Shapes.AddPicture2()
 
             // 可以在这里对新幻灯片进行进一步的设置
 
             // 最后,显示新幻灯片
-            newSlide.Select();
+            // newSlide.Select();
             Microsoft.Office.Interop.PowerPoint.Application pptApp = Globals.ThisAddIn.Application;
             Microsoft.Office.Interop.PowerPoint.Slide activeSlide = pptApp.ActiveWindow.View.Slide;
             MainWindow mainWindow = new MainWindow();
             mainWindow.Show();
-            Microsoft.Office.Interop.PowerPoint.Shape pictureShape = activeSlide.Shapes.AddPicture(
-                   FileName: "F:\\12.jpg",
-                   LinkToFile: MsoTriState.msoCTrue,
-                   SaveWithDocument: MsoTriState.msoCTrue,
-                   Left: 100,   // 设置图片左上角坐标
-                   Top: 100,    // 设置图片左上角坐标
-                   Width: 200,  // 设置图片宽度
-                   Height: 150  // 设置图片高度
-                   
-               );
-            pictureShape.Name = "12345678901234567890Name";
-            pictureShape.AlternativeText="12345678901234567890AlternativeText";
-            pictureShape.Title="12345678901234567890Title";
-            foreach (var s in activeSlide.Shapes) {
-                if (s is Microsoft.Office.Interop.PowerPoint.Shape) { 
-                
+            //Microsoft.Office.Interop.PowerPoint.Shape pictureShape = activeSlide.Shapes.AddPicture(
+            //       FileName: "F:\\12.jpg",
+            //       LinkToFile: MsoTriState.msoCTrue,
+            //       SaveWithDocument: MsoTriState.msoCTrue,
+            //       Left: 100,   // 设置图片左上角坐标
+            //       Top: 100,    // 设置图片左上角坐标
+            //       Width: 200,  // 设置图片宽度
+            //       Height: 150  // 设置图片高度
+
+            //   );
+            //pictureShape.Name = "12345678901234567890Name";
+            //pictureShape.AlternativeText="12345678901234567890AlternativeText";
+            //pictureShape.Title="12345678901234567890Title";
+            foreach (var s in activeSlide.Shapes)
+            {
+                if (s is Microsoft.Office.Interop.PowerPoint.Shape)
+                {
+
                 }
             }
         }

+ 4 - 7
HTEXTeachLib/MainWindow.xaml

@@ -7,15 +7,12 @@
         xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
         x:Class="HTEXTeachLib.MainWindow"
         mc:Ignorable="d"
-        Title="MainWindow" Height="450" Width="800" WindowStyle="None"  AllowsTransparency="True"  >
+        Title="MainWindow" Height="450" Width="450" WindowStyle="None"   AllowsTransparency="True"  >
     <Window.Background>
         <!-- 1% 透明度的白色 -->
         <SolidColorBrush Color="#01FFFFFF" />
     </Window.Background>
-    <Canvas x:Name="myCanvas"  Background="Transparent"  Height="450" Width="800">
-
-        <Border BorderBrush="Gray" BorderThickness="0.5" CornerRadius="5" IsHitTestVisible="False">
-            <wv2:WebView2 Name="webView" Height="400" Width="400"  />
-        </Border>
-    </Canvas>
+    <Grid x:Name="myCanvas"  Background="Transparent"  Height="450" Width="450">
+        <wv2:WebView2 Name="webView" Height="450" Width="450" DefaultBackgroundColor="Transparent"  />
+    </Grid>
 </Window>

+ 135 - 6
HTEXTeachLib/MainWindow.xaml.cs

@@ -23,19 +23,75 @@ namespace HTEXTeachLib
     ///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;
+            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;
+            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();
         }
@@ -60,7 +116,7 @@ namespace HTEXTeachLib
             webView.ExecuteScriptAsync($"insertText('{draggedText}')");
 
             e.Handled = true;
-           
+
         }
         private async void WebView_CoreWebView2InitializationCompleted(object sender, RoutedEventArgs e)
         {
@@ -78,17 +134,90 @@ namespace HTEXTeachLib
                             margin: 0;
                             padding: 0;
                         }
+                        .centered-content {
+                            display: flex; /* 创建 Flexbox 布局 */
+                            justify-content: center; /* 水平居中对齐 */
+                            align-items: center; /* 垂直居中对齐 */
+                        }
                     </style>
                 </head>
                 <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>
                     
                 </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;
         }
     }
 }