InkCanvasForClass/Ink Canvas/MainWindow.xaml.cs

227 lines
10 KiB
C#
Raw Normal View History

2023-05-29 12:35:52 +08:00
using Ink_Canvas.Helpers;
using iNKORE.UI.WPF.Modern;
2023-04-05 16:05:03 +08:00
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Media;
2024-05-02 20:21:36 +08:00
using System.Diagnostics;
2023-04-05 16:05:03 +08:00
using File = System.IO.File;
2024-03-16 12:00:51 +08:00
using MessageBox = System.Windows.MessageBox;
2024-05-04 19:46:20 +08:00
using System.Runtime.InteropServices;
using System.Windows.Interop;
2023-04-05 16:05:03 +08:00
2023-12-22 00:14:15 +08:00
namespace Ink_Canvas {
public partial class MainWindow : Window {
2023-04-05 16:05:03 +08:00
#region Window Initialization
2023-12-22 00:14:15 +08:00
public MainWindow() {
/*
Topmost == false / currentMode != 0
PPT BtnPPTSlideShowEnd.Visibility
*/
2023-04-05 16:05:03 +08:00
InitializeComponent();
2023-12-22 00:14:15 +08:00
BlackboardLeftSide.Visibility = Visibility.Collapsed;
BlackboardCenterSide.Visibility = Visibility.Collapsed;
BlackboardRightSide.Visibility = Visibility.Collapsed;
BorderTools.Visibility = Visibility.Collapsed;
2023-04-05 16:05:03 +08:00
BorderSettings.Visibility = Visibility.Collapsed;
2023-12-22 00:14:15 +08:00
LeftSidePanelForPPTNavigation.Visibility = Visibility.Collapsed;
RightSidePanelForPPTNavigation.Visibility = Visibility.Collapsed;
BorderSettings.Margin = new Thickness(0, 150, 0, 150);
TwoFingerGestureBorder.Visibility = Visibility.Collapsed;
BoardTwoFingerGestureBorder.Visibility = Visibility.Collapsed;
2023-04-05 16:05:03 +08:00
BorderDrawShape.Visibility = Visibility.Collapsed;
2023-12-22 00:14:15 +08:00
BoardBorderDrawShape.Visibility = Visibility.Collapsed;
2023-04-05 16:05:03 +08:00
GridInkCanvasSelectionCover.Visibility = Visibility.Collapsed;
2024-05-01 18:23:32 +08:00
//if (!App.StartArgs.Contains("-o"))
2023-12-22 00:14:15 +08:00
ViewBoxStackPanelMain.Visibility = Visibility.Collapsed;
ViewBoxStackPanelShapes.Visibility = Visibility.Collapsed;
ViewboxFloatingBar.Margin = new Thickness((SystemParameters.WorkArea.Width - 284) / 2, SystemParameters.WorkArea.Height - 60, -2000, -200);
ViewboxFloatingBarMarginAnimation(100);
2023-04-05 16:05:03 +08:00
2023-12-22 00:14:15 +08:00
try {
if (File.Exists("debug.ini")) Label.Visibility = Visibility.Visible;
} catch (Exception ex) {
LogHelper.WriteLogToFile(ex.ToString(), LogHelper.LogType.Error);
}
try {
if (File.Exists("Log.txt")) {
FileInfo fileInfo = new FileInfo("Log.txt");
long fileSizeInKB = fileInfo.Length / 1024;
if (fileSizeInKB > 512) {
try {
File.Delete("Log.txt");
LogHelper.WriteLogToFile("The Log.txt file has been successfully deleted. Original file size: " + fileSizeInKB + " KB", LogHelper.LogType.Info);
} catch (Exception ex) {
LogHelper.WriteLogToFile(ex + " | Can not delete the Log.txt file. File size: " + fileSizeInKB + " KB", LogHelper.LogType.Error);
}
}
}
} catch (Exception ex) {
LogHelper.WriteLogToFile(ex.ToString(), LogHelper.LogType.Error);
}
2023-04-05 16:05:03 +08:00
InitTimers();
timeMachine.OnRedoStateChanged += TimeMachine_OnRedoStateChanged;
timeMachine.OnUndoStateChanged += TimeMachine_OnUndoStateChanged;
inkCanvas.Strokes.StrokesChanged += StrokesOnStrokesChanged;
Microsoft.Win32.SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
2023-12-22 00:14:15 +08:00
try {
if (File.Exists("SpecialVersion.ini")) SpecialVersionResetToSuggestion_Click();
} catch (Exception ex) {
LogHelper.WriteLogToFile(ex.ToString(), LogHelper.LogType.Error);
}
CheckColorTheme(true);
2023-04-05 16:05:03 +08:00
}
#endregion
#region Ink Canvas Functions
2024-05-02 20:21:36 +08:00
2023-04-05 16:05:03 +08:00
Color Ink_DefaultColor = Colors.Red;
DrawingAttributes drawingAttributes;
2023-12-22 00:14:15 +08:00
private void loadPenCanvas() {
2024-05-02 20:21:36 +08:00
try
{
double alpha = Settings.Canvas.InkAlpha;
Trace.WriteLine(alpha);
2023-04-05 16:05:03 +08:00
//drawingAttributes = new DrawingAttributes();
drawingAttributes = inkCanvas.DefaultDrawingAttributes;
drawingAttributes.Color = Ink_DefaultColor;
drawingAttributes.Height = 2.5;
drawingAttributes.Width = 2.5;
drawingAttributes.IsHighlighter = false;
drawingAttributes.FitToCurve = Settings.Canvas.FitToCurve;
2023-04-05 16:05:03 +08:00
inkCanvas.EditingMode = InkCanvasEditingMode.Ink;
inkCanvas.Gesture += InkCanvas_Gesture;
2023-12-22 00:14:15 +08:00
} catch { }
2023-04-05 16:05:03 +08:00
}
//ApplicationGesture lastApplicationGesture = ApplicationGesture.AllGestures;
DateTime lastGestureTime = DateTime.Now;
2023-12-22 00:14:15 +08:00
private void InkCanvas_Gesture(object sender, InkCanvasGestureEventArgs e) {
2023-04-05 16:05:03 +08:00
ReadOnlyCollection<GestureRecognitionResult> gestures = e.GetGestureRecognitionResults();
2023-12-22 00:14:15 +08:00
try {
foreach (GestureRecognitionResult gest in gestures) {
2023-04-05 16:05:03 +08:00
//Trace.WriteLine(string.Format("Gesture: {0}, Confidence: {1}", gest.ApplicationGesture, gest.RecognitionConfidence));
2023-12-22 00:14:15 +08:00
if (StackPanelPPTControls.Visibility == Visibility.Visible) {
if (gest.ApplicationGesture == ApplicationGesture.Left) {
2023-04-05 16:05:03 +08:00
BtnPPTSlidesDown_Click(BtnPPTSlidesDown, null);
}
2023-12-22 00:14:15 +08:00
if (gest.ApplicationGesture == ApplicationGesture.Right) {
2023-04-05 16:05:03 +08:00
BtnPPTSlidesUp_Click(BtnPPTSlidesUp, null);
}
}
}
2023-12-22 00:14:15 +08:00
} catch { }
2023-04-05 16:05:03 +08:00
}
2023-12-22 00:14:15 +08:00
private void inkCanvas_EditingModeChanged(object sender, RoutedEventArgs e) {
2023-04-05 16:05:03 +08:00
var inkCanvas1 = sender as InkCanvas;
if (inkCanvas1 == null) return;
2023-12-22 00:14:15 +08:00
if (Settings.Canvas.IsShowCursor) {
if (inkCanvas1.EditingMode == InkCanvasEditingMode.Ink || drawingShapeMode != 0) {
2023-04-05 16:05:03 +08:00
inkCanvas1.ForceCursor = true;
2023-12-22 00:14:15 +08:00
} else {
2023-04-05 16:05:03 +08:00
inkCanvas1.ForceCursor = false;
}
2023-12-22 00:14:15 +08:00
} else {
2023-04-05 16:05:03 +08:00
inkCanvas1.ForceCursor = false;
}
if (inkCanvas1.EditingMode == InkCanvasEditingMode.Ink) forcePointEraser = !forcePointEraser;
}
#endregion Ink Canvas
#region Definations and Loading
public static Settings Settings = new Settings();
public static string settingsFileName = "Settings.json";
bool isLoaded = false;
2024-05-04 19:46:20 +08:00
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
private const int GWL_EX_STYLE = -20;
private const int WS_EX_APPWINDOW = 0x00040000, WS_EX_TOOLWINDOW = 0x00000080;
2023-12-22 00:14:15 +08:00
private void Window_Loaded(object sender, RoutedEventArgs e) {
2024-05-04 19:46:20 +08:00
var helper = new WindowInteropHelper(this).Handle;
//Performing some magic to hide the form from Alt+Tab
SetWindowLong(helper, GWL_EX_STYLE, (GetWindowLong(helper, GWL_EX_STYLE) | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW);
2023-04-05 16:05:03 +08:00
loadPenCanvas();
//加载设置
LoadSettings(true);
// HasNewUpdateWindow hasNewUpdateWindow = new HasNewUpdateWindow();
2023-12-22 00:14:15 +08:00
if (Environment.Is64BitProcess) {
GroupBoxInkRecognition.Visibility = Visibility.Collapsed;
}
2023-04-05 16:05:03 +08:00
ThemeManager.Current.ApplicationTheme = ApplicationTheme.Light;
2023-05-29 12:35:52 +08:00
SystemEvents_UserPreferenceChanged(null, null);
2023-04-05 16:05:03 +08:00
2023-12-22 00:14:15 +08:00
//TextBlockVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
2023-04-05 16:05:03 +08:00
LogHelper.WriteLogToFile("Ink Canvas Loaded", LogHelper.LogType.Event);
isLoaded = true;
}
2023-12-22 00:14:15 +08:00
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
2023-04-05 16:05:03 +08:00
LogHelper.WriteLogToFile("Ink Canvas closing", LogHelper.LogType.Event);
2023-12-22 00:14:15 +08:00
if (!CloseIsFromButton && Settings.Advanced.IsSecondConfimeWhenShutdownApp) {
2023-04-05 16:05:03 +08:00
e.Cancel = true;
if (MessageBox.Show("是否继续关闭 InkCanvasForClass这将丢失当前未保存的墨迹。", "InkCanvasForClass", MessageBoxButton.OKCancel, MessageBoxImage.Warning) == MessageBoxResult.OK) {
if (MessageBox.Show("真的狠心关闭 InkCanvasForClass吗", "InkCanvasForClass", MessageBoxButton.OKCancel, MessageBoxImage.Error) == MessageBoxResult.OK) {
if (MessageBox.Show("是否取消关闭 InkCanvasForClass", "InkCanvasForClass", MessageBoxButton.OKCancel, MessageBoxImage.Error) != MessageBoxResult.OK) {
2023-04-05 16:05:03 +08:00
e.Cancel = false;
}
}
}
}
2023-12-22 00:14:15 +08:00
if (e.Cancel) {
2023-04-05 16:05:03 +08:00
LogHelper.WriteLogToFile("Ink Canvas closing cancelled", LogHelper.LogType.Event);
}
}
2023-12-22 00:14:15 +08:00
private void Window_Closed(object sender, EventArgs e) {
2023-04-05 16:05:03 +08:00
LogHelper.WriteLogToFile("Ink Canvas closed", LogHelper.LogType.Event);
}
2023-12-22 00:14:15 +08:00
private async void AutoUpdate() {
AvailableLatestVersion = await AutoUpdateHelper.CheckForUpdates();
2023-12-22 00:14:15 +08:00
if (AvailableLatestVersion != null) {
bool IsDownloadSuccessful = false;
IsDownloadSuccessful = await AutoUpdateHelper.DownloadSetupFileAndSaveStatus(AvailableLatestVersion);
2023-12-22 00:14:15 +08:00
if (IsDownloadSuccessful) {
if (!Settings.Startup.IsAutoUpdateWithSilence) {
if (MessageBox.Show("InkCanvasForClass 新版本安装包已下载完成,是否立即更新?", "InkCanvasForClass New Version Available", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) {
2023-12-22 00:14:15 +08:00
AutoUpdateHelper.InstallNewVersionApp(AvailableLatestVersion, false);
}
} else {
timerCheckAutoUpdateWithSilence.Start();
}
2023-04-05 16:05:03 +08:00
}
2023-12-22 00:14:15 +08:00
} else {
AutoUpdateHelper.DeleteUpdatesFolder();
2023-04-05 16:05:03 +08:00
}
2023-12-22 00:14:15 +08:00
}
2023-04-05 16:05:03 +08:00
#endregion Definations and Loading
}
2024-05-01 18:23:32 +08:00
}