using Newtonsoft.Json.Linq; using ScreenGrab; using System.Drawing; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using Bitmap = System.Drawing.Bitmap; using MessageBox = System.Windows.MessageBox; using Point = System.Windows.Point; using CefSharp; using CefSharp.Wpf; namespace WpfEditor { public partial class TreeMenuPage : Window { private bool _isMenuVisible = false; private bool _forceClose = false; private NotifyIcon _notifyIcon; public TreeMenuPage() { InitializeComponent(); // 初始化时隐藏菜单 TreeMenuBorder.Visibility = Visibility.Collapsed; // 添加点击其他区域隐藏菜单的事件 this.MouseDown += TreeMenuPage_MouseDown; GlobalHotKey.RegisterHotKey("Alt + S", () => { if (ScreenGrabber.IsCapturing) return; // https://github.com/wuqinchao/ScreenCapturer // 替代品 ScreenGrabber.OnCaptured = async (bitmap, type) => await ScreenshotCallbackAsync(bitmap, type, null); ScreenGrabber.Capture(true); }); GlobalHotKey.RegisterHotKey("Alt + D", () => { //var win = Echodict.EchodictWindow.Instance; //if (win.WindowState == WindowState.Minimized) //{ // win.WindowState = win.LastWindowState; //} //win.WindowStartupLocation = WindowStartupLocation.CenterScreen; //win.Show(); //win.Activate(); //win.Topmost = true; showDict(); }); GlobalHotKey.RegisterHotKey("Alt + E", () => { Show(); WindowState = WindowState.Normal; Activate(); }); Loaded += TreeMenuPage_Loaded; Closing += TreeMenuPage_Closing; StateChanged += TreeMenuPage_StateChanged; } private void TreeMenuPage_StateChanged(object? sender, EventArgs e) { if (WindowState == WindowState.Minimized) { Hide(); } } private void showDict() { var win = Echodict.EchodictWindow.Instance; if (win.WindowState == WindowState.Minimized) { win.WindowState = win.LastWindowState; } win.WindowStartupLocation = WindowStartupLocation.CenterScreen; win.Show(); win.Activate(); win.Topmost = true; } private void TreeMenuPage_Loaded(object sender, RoutedEventArgs e) { WindowState = WindowState.Minimized; Hide(); _notifyIcon = new NotifyIcon(); _notifyIcon.Icon = new Icon("ico.ico"); _notifyIcon.Visible = true; _notifyIcon.Text = "EchoDict"; var contextMenu = new ContextMenuStrip(); var exitMenuItem = new ToolStripMenuItem("退出"); exitMenuItem.Click += (s, e) => { _forceClose = true; System.Windows.Application.Current.Shutdown(); }; var ocrMenuItem = new ToolStripMenuItem("Alt+S 截图OCR"); ocrMenuItem.Click += (s, e) => { if (ScreenGrabber.IsCapturing) return; ScreenGrabber.OnCaptured = async (bitmap, type) => await ScreenshotCallbackAsync(bitmap, type, null); ScreenGrabber.Capture(true); }; var EchoEditorMenuItem = new ToolStripMenuItem("Alt+E EchoEditor"); EchoEditorMenuItem.Click += (s, e) => { Show(); WindowState = WindowState.Normal; Activate(); }; var EchoDictMenuItem = new ToolStripMenuItem("Alt+D EchoDict"); EchoDictMenuItem.Click += (s, e) => { //var win = Echodict.EchodictWindow.Instance; //if (win.WindowState == WindowState.Minimized) //{ // win.WindowState = win.LastWindowState; //} //win.WindowStartupLocation = WindowStartupLocation.CenterScreen; //win.Show(); //win.Activate(); //win.Topmost = true; showDict(); }; contextMenu.Items.Add(ocrMenuItem); contextMenu.Items.Add(EchoDictMenuItem); contextMenu.Items.Add(EchoEditorMenuItem); contextMenu.Items.Add(exitMenuItem); _notifyIcon.ContextMenuStrip = contextMenu; _notifyIcon.DoubleClick += (s, args) => { //Show(); //WindowState = WindowState.Normal; //Activate(); showDict(); }; } private void TreeMenuPage_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (_forceClose) { if (_notifyIcon != null) { _notifyIcon.Visible = false; _notifyIcon.Dispose(); } } else { e.Cancel = true; WindowState = WindowState.Minimized; Hide(); } } private async Task ScreenshotCallbackAsync(Bitmap? bitmap, string type, CancellationToken? token = null) { if (bitmap == null) { //ShowBalloonTip("图像不存在"); return; } // 将 bitmap 转成 bytes byte[] bytes; using (var ms = new MemoryStream()) { bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); bytes = ms.ToArray(); } // run first -> huggingface/comicocr/main.py //string result = await ComicOcr.RecognizeImage("", bytes); // 获取临时文件夹路径 string tempDir = Path.GetTempPath(); // 生成唯一的临时文件名 string tempFile = Path.Combine(tempDir, Guid.NewGuid().ToString("N") + ".jpg"); File.WriteAllBytes(tempFile, bytes); //try //{ // // 将 bytes 写入临时文件 // File.WriteAllBytes(tempFile, bytes); //} //finally //{ // // 删除临时文件 // if (File.Exists(tempFile)) // { // File.Delete(tempFile); // } //} //var win = new OCRResultWindow("just a test."); //win.Owner = this; //win.WindowStartupLocation = WindowStartupLocation.CenterOwner; //win.Show(); var ReviseEditWindow = new MainWindow(EditorMode.ReviseEdit); if (type == "文字识别") { ReviseEditWindow._ocrMode = OcrMode.WeChatOcr; } else if (type == "漫画识别") { ReviseEditWindow._ocrMode = OcrMode.ComicOcr; } ReviseEditWindow.WindowState = WindowState.Maximized; ReviseEditWindow.Show(); ReviseEditWindow.handle_comicocr_open_img(tempFile); } private void MenuButton_Click(object sender, RoutedEventArgs e) { ToggleMenu(); } private void ToggleMenu() { if (_isMenuVisible) { HideMenu(); } else { ShowMenu(); } } private void ShowMenu() { TreeMenuBorder.Visibility = Visibility.Visible; _isMenuVisible = true; // 添加滑入动画效果 var slideAnimation = new DoubleAnimation { From = -250, To = 0, Duration = TimeSpan.FromMilliseconds(300), EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut } }; var transform = new TranslateTransform(); TreeMenuBorder.RenderTransform = transform; transform.BeginAnimation(TranslateTransform.XProperty, slideAnimation); } private void HideMenu() { var slideAnimation = new DoubleAnimation { From = 0, To = -250, Duration = TimeSpan.FromMilliseconds(200), EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseIn } }; slideAnimation.Completed += (s, e) => { TreeMenuBorder.Visibility = Visibility.Collapsed; _isMenuVisible = false; }; var transform = TreeMenuBorder.RenderTransform as TranslateTransform ?? new TranslateTransform(); TreeMenuBorder.RenderTransform = transform; transform.BeginAnimation(TranslateTransform.XProperty, slideAnimation); } private void TreeItem_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (sender is TreeViewItem item) { string action = item.Tag?.ToString() ?? ""; HandleMenuAction(action, item.Header?.ToString() ?? ""); } } private void HandleMenuAction(string action, string itemName) { // 更新内容显示 ContentText.Text = $"您点击了: {itemName}"; // 根据不同的操作执行相应的功能 switch (action) { case "ImageOCR": var ImageOCRWindow = new MainWindow(EditorMode.ImageOCR); ImageOCRWindow.WindowState = WindowState.Maximized; ImageOCRWindow.Show(); break; case "ReviseEdit": var ReviseEditWindow = new MainWindow(EditorMode.ReviseEdit); ReviseEditWindow.WindowState = WindowState.Maximized; ReviseEditWindow.Show(); break; case "MenuSplit": var MenuSplitWindow = new MainWindow(EditorMode.MenuSplit); MenuSplitWindow.WindowState = WindowState.Maximized; MenuSplitWindow.Show(); break; case "SaveFile": MessageBox.Show("执行保存文件操作", "操作提示", MessageBoxButton.OK, MessageBoxImage.Information); break; case "Copy": MessageBox.Show("执行复制操作", "操作提示", MessageBoxButton.OK, MessageBoxImage.Information); break; case "ExportPDFImage": var exportPdfImageWindow = new ExportPDFImage(); exportPdfImageWindow.Show(); break; case "Paste": MessageBox.Show("执行粘贴操作", "操作提示", MessageBoxButton.OK, MessageBoxImage.Information); break; case "About": MessageBox.Show("WPF编辑器 v1.0\n一个功能丰富的文本编辑器", "关于", MessageBoxButton.OK, MessageBoxImage.Information); break; default: // 对于其他操作,只显示提示信息 break; } // 点击后隐藏菜单 HideMenu(); } private void TreeMenuPage_MouseDown(object sender, MouseButtonEventArgs e) { // 如果点击的不是菜单区域,则隐藏菜单 if (_isMenuVisible && !IsMouseOverElement(TreeMenuBorder, e.GetPosition(this))) { HideMenu(); } } private bool IsMouseOverElement(FrameworkElement element, Point mousePosition) { var elementPosition = element.TransformToAncestor(this).Transform(new Point(0, 0)); var elementBounds = new Rect(elementPosition.X, elementPosition.Y, element.ActualWidth, element.ActualHeight); return elementBounds.Contains(mousePosition); } } }