|
|
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;
|
|
|
|
|
|
|
|
|
ScreenGrabber.OnCaptured = async (bitmap, type) => await ScreenshotCallbackAsync(bitmap, type, null);
|
|
|
ScreenGrabber.Capture(true);
|
|
|
|
|
|
});
|
|
|
|
|
|
GlobalHotKey.RegisterHotKey("Alt + D", () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
showDict();
|
|
|
};
|
|
|
contextMenu.Items.Add(ocrMenuItem);
|
|
|
contextMenu.Items.Add(EchoDictMenuItem);
|
|
|
contextMenu.Items.Add(EchoEditorMenuItem);
|
|
|
contextMenu.Items.Add(exitMenuItem);
|
|
|
_notifyIcon.ContextMenuStrip = contextMenu;
|
|
|
|
|
|
_notifyIcon.DoubleClick += (s, args) =>
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
byte[] bytes;
|
|
|
using (var ms = new MemoryStream())
|
|
|
{
|
|
|
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
|
bytes = ms.ToArray();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string tempDir = Path.GetTempPath();
|
|
|
|
|
|
string tempFile = Path.Combine(tempDir, Guid.NewGuid().ToString("N") + ".jpg");
|
|
|
File.WriteAllBytes(tempFile, bytes);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
} |