From 4d8fbef455874ce5284f2737fab96477bc0be1cf Mon Sep 17 00:00:00 2001 From: CJKmkp <2564608840@qq.com> Date: Fri, 1 May 2026 00:27:29 +0800 Subject: [PATCH] =?UTF-8?q?add:debug=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/App.xaml.cs | 2 +- Ink Canvas/Helpers/DebugConsoleManager.cs | 96 +++++++++++++++++++ Ink Canvas/Helpers/LogHelper.cs | 1 + Ink Canvas/MainWindow.xaml.cs | 6 ++ Ink Canvas/Resources/Settings.cs | 3 + .../SettingsViews/Pages/DebugPage.xaml | 43 +++++++++ .../SettingsViews/Pages/DebugPage.xaml.cs | 34 +++++++ .../SettingsViews/SettingsWindow.xaml.cs | 2 +- 8 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 Ink Canvas/Helpers/DebugConsoleManager.cs create mode 100644 Ink Canvas/Windows/SettingsViews/Pages/DebugPage.xaml create mode 100644 Ink Canvas/Windows/SettingsViews/Pages/DebugPage.xaml.cs diff --git a/Ink Canvas/App.xaml.cs b/Ink Canvas/App.xaml.cs index 15d72943..73dd9745 100644 --- a/Ink Canvas/App.xaml.cs +++ b/Ink Canvas/App.xaml.cs @@ -261,7 +261,7 @@ namespace Ink_Canvas Current.MainWindow.SourceInitialized -= MainWindow_SourceInitialized; Current.MainWindow.SourceInitialized += MainWindow_SourceInitialized; } - catch (Exception ex) + catch (Exception) { } } diff --git a/Ink Canvas/Helpers/DebugConsoleManager.cs b/Ink Canvas/Helpers/DebugConsoleManager.cs new file mode 100644 index 00000000..ab48118b --- /dev/null +++ b/Ink Canvas/Helpers/DebugConsoleManager.cs @@ -0,0 +1,96 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace Ink_Canvas.Helpers +{ + public static class DebugConsoleManager + { + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool AllocConsole(); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool FreeConsole(); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern IntPtr GetConsoleWindow(); + + [DllImport("user32.dll")] + private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); + + [DllImport("user32.dll")] + private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); + + [DllImport("user32.dll")] + private static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags); + + [DllImport("kernel32.dll")] + private static extern bool SetConsoleTitle(string lpConsoleTitle); + + private const int SW_HIDE = 0; + private const int SW_SHOW = 5; + private const uint SC_CLOSE = 0xF060; + private const uint MF_BYCOMMAND = 0x00000000; + + private static bool _allocated; + + public static bool IsVisible { get; private set; } + + public static void Show() + { + try + { + if (!_allocated) + { + if (GetConsoleWindow() == IntPtr.Zero) + { + if (!AllocConsole()) return; + } + _allocated = true; + + Console.OutputEncoding = Encoding.UTF8; + SetConsoleTitle("InkCanvasForClass - Debug Console"); + + // 移除关闭菜单,避免用户点 X 时直接结束进程 + var hWnd = GetConsoleWindow(); + if (hWnd != IntPtr.Zero) + { + var hMenu = GetSystemMenu(hWnd, false); + if (hMenu != IntPtr.Zero) DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND); + } + } + else + { + var hWnd = GetConsoleWindow(); + if (hWnd != IntPtr.Zero) ShowWindow(hWnd, SW_SHOW); + } + IsVisible = true; + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"[DebugConsoleManager] Show failed: {ex.Message}"); + } + } + + public static void Hide() + { + try + { + var hWnd = GetConsoleWindow(); + if (hWnd != IntPtr.Zero) ShowWindow(hWnd, SW_HIDE); + IsVisible = false; + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"[DebugConsoleManager] Hide failed: {ex.Message}"); + } + } + + public static void WriteLine(string line) + { + if (!IsVisible) return; + try { Console.WriteLine(line); } + catch { } + } + } +} \ No newline at end of file diff --git a/Ink Canvas/Helpers/LogHelper.cs b/Ink Canvas/Helpers/LogHelper.cs index 0b65f5a9..fe0d63a3 100644 --- a/Ink Canvas/Helpers/LogHelper.cs +++ b/Ink Canvas/Helpers/LogHelper.cs @@ -83,6 +83,7 @@ namespace Ink_Canvas.Helpers } } string logLine = string.Format("{0} [T{1}] [{2}] [{3}] {4}", DateTime.Now.ToString("O"), threadId, strLogType, callerInfo, str); + DebugConsoleManager.WriteLine(logLine); ProcessProtectionManager.WithWriteAccess(file, () => { using (StreamWriter sw = new StreamWriter(file, true)) diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index b1b59dcd..9f0b4e06 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -1180,6 +1180,12 @@ namespace Ink_Canvas LoadSettings(true); ApplyLanguageFromSettings(); + // 启动时根据设置恢复调试控制台显示状态 + if (Settings?.Advanced != null && Settings.Advanced.IsDebugConsoleEnabled) + { + Helpers.DebugConsoleManager.Show(); + } + _ = TelemetryUploader.UploadTelemetryIfNeededAsync(); LoadCustomBackgroundColor(); diff --git a/Ink Canvas/Resources/Settings.cs b/Ink Canvas/Resources/Settings.cs index 23fd8a8c..4cb7135a 100644 --- a/Ink Canvas/Resources/Settings.cs +++ b/Ink Canvas/Resources/Settings.cs @@ -684,6 +684,9 @@ namespace Ink_Canvas [JsonProperty("isSaveLogByDate")] public bool IsSaveLogByDate { get; set; } = true; + [JsonProperty("isDebugConsoleEnabled")] + public bool IsDebugConsoleEnabled { get; set; } = false; + [JsonProperty("isEnableFullScreenHelper")] public bool IsEnableFullScreenHelper { get; set; } diff --git a/Ink Canvas/Windows/SettingsViews/Pages/DebugPage.xaml b/Ink Canvas/Windows/SettingsViews/Pages/DebugPage.xaml new file mode 100644 index 00000000..22ee9807 --- /dev/null +++ b/Ink Canvas/Windows/SettingsViews/Pages/DebugPage.xaml @@ -0,0 +1,43 @@ + + + + + + 4 + + + + + + + + + + + + \ No newline at end of file diff --git a/Ink Canvas/Windows/SettingsViews/Pages/DebugPage.xaml.cs b/Ink Canvas/Windows/SettingsViews/Pages/DebugPage.xaml.cs new file mode 100644 index 00000000..22681ddc --- /dev/null +++ b/Ink Canvas/Windows/SettingsViews/Pages/DebugPage.xaml.cs @@ -0,0 +1,34 @@ +using Ink_Canvas.Helpers; +using Ink_Canvas.Windows.SettingsViews.Helpers; +using System.Windows; +using Page = iNKORE.UI.WPF.Modern.Controls.Page; + +namespace Ink_Canvas.Windows.SettingsViews.Pages +{ + public partial class DebugPage : Page + { + private bool _isLoaded; + + public DebugPage() + { + InitializeComponent(); + Loaded += (s, e) => + { + ToggleSwitchDebugConsole.IsOn = SettingsManager.Settings.Advanced.IsDebugConsoleEnabled; + _isLoaded = true; + }; + Unloaded += (s, e) => _isLoaded = false; + } + + private void ToggleSwitchDebugConsole_Toggled(object sender, RoutedEventArgs e) + { + if (!_isLoaded) return; + bool isOn = ToggleSwitchDebugConsole.IsOn; + SettingsManager.Settings.Advanced.IsDebugConsoleEnabled = isOn; + SettingsManager.SaveSettingsToFile(); + + if (isOn) DebugConsoleManager.Show(); + else DebugConsoleManager.Hide(); + } + } +} \ No newline at end of file diff --git a/Ink Canvas/Windows/SettingsViews/SettingsWindow.xaml.cs b/Ink Canvas/Windows/SettingsViews/SettingsWindow.xaml.cs index 61299266..5fe3a7da 100644 --- a/Ink Canvas/Windows/SettingsViews/SettingsWindow.xaml.cs +++ b/Ink Canvas/Windows/SettingsViews/SettingsWindow.xaml.cs @@ -50,7 +50,7 @@ namespace Ink_Canvas.Windows.SettingsViews { "RandomDrawPage", typeof(RandomDrawPage) }, { "CanvasPage", typeof(CanvasPage) }, { "InkRecognitionPage", typeof(InkRecognitionPage) }, - { "DebugPage", typeof(IconographyPage) }, + { "DebugPage", typeof(DebugPage) }, { "AboutPage", typeof(AboutPage) }, { "Settings", typeof(SettingsPage) }, { "PluginPage", typeof(PluginPage) },