From c7cdf3d4df5a8c1a77b4ccf16f8fa2f8e840864a Mon Sep 17 00:00:00 2001 From: mashed-potatoes <37037851+mashed-potatoes@users.noreply.github.com> Date: Sun, 12 Jul 2020 21:19:59 +0500 Subject: [PATCH] Add `save log` item in window ctx --- PotatoNV-next/MainWindow.xaml | 27 ++++++------ PotatoNV-next/MainWindow.xaml.cs | 74 +++++++++++++++++++++++++++++++- PotatoNV-next/Utils/Log.cs | 9 ++++ 3 files changed, 96 insertions(+), 14 deletions(-) diff --git a/PotatoNV-next/MainWindow.xaml b/PotatoNV-next/MainWindow.xaml index 475ba2f..2190dd2 100644 --- a/PotatoNV-next/MainWindow.xaml +++ b/PotatoNV-next/MainWindow.xaml @@ -1,15 +1,16 @@ - + - + diff --git a/PotatoNV-next/MainWindow.xaml.cs b/PotatoNV-next/MainWindow.xaml.cs index 7ab72e1..58b4dbd 100644 --- a/PotatoNV-next/MainWindow.xaml.cs +++ b/PotatoNV-next/MainWindow.xaml.cs @@ -1,11 +1,55 @@ -using PotatoNV_next.Utils; +using Microsoft.Win32; +using PotatoNV_next.Utils; +using System; using System.Globalization; +using System.IO; +using System.Runtime.InteropServices; using System.Windows; +using System.Windows.Interop; namespace PotatoNV_next { public partial class MainWindow : Window { + #region Win32 interop handle + [DllImport("user32.dll")] + private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); + + [DllImport("user32.dll")] + private static extern bool InsertMenu(IntPtr hMenu, int wPosition, int wFlags, int wIDNewItem, string lpNewItem); + + public const int WM_SYSCOMMAND = 0x112; + public const int MF_SEPARATOR = 0x800; + public const int MF_BYPOSITION = 0x400; + public const int MF_STRING = 0x0; + + public const int TB_SAVE_LOGS = 1000; + + public IntPtr Handle + { + get + { + return new WindowInteropHelper(this).Handle; + } + } + + private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + { + if (msg == WM_SYSCOMMAND) + { + switch (wParam.ToInt32()) + { + case TB_SAVE_LOGS: + SaveLogs(); + handled = true; + break; + } + } + + return IntPtr.Zero; + } + #endregion + readonly Core core = new Core(); public MainWindow() @@ -15,10 +59,38 @@ namespace PotatoNV_next nvFrom.OnFormSubmit += NvFrom_OnFormSubmit; core.RunWorkerCompleted += Core_RunWorkerCompleted; + Loaded += MainWindow_Loaded; SetupLog(); } + private void MainWindow_Loaded(object sender, RoutedEventArgs e) + { + IntPtr systemMenuHandle = GetSystemMenu(Handle, false); + + InsertMenu(systemMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty); + InsertMenu(systemMenuHandle, 6, MF_BYPOSITION, TB_SAVE_LOGS, "Save log to file"); + + HwndSource source = HwndSource.FromHwnd(Handle); + source.AddHook(new HwndSourceHook(WndProc)); + } + + private static void SaveLogs() + { + var dialog = new SaveFileDialog + { + InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), + CheckPathExists = true, + DefaultExt = "log", + Filter = "Log file (*.log)|*.log" + }; + + if (dialog.ShowDialog() == true) + { + File.WriteAllText(dialog.FileName, Log.GetLog()); + } + } + private void NvFrom_OnFormSubmit(Controls.NVForm.FormEventArgs formEventArgs) { SetupLog(); diff --git a/PotatoNV-next/Utils/Log.cs b/PotatoNV-next/Utils/Log.cs index e4a801e..1443fd4 100644 --- a/PotatoNV-next/Utils/Log.cs +++ b/PotatoNV-next/Utils/Log.cs @@ -1,10 +1,12 @@ using System; +using System.Text; namespace PotatoNV_next.Utils { public class Log { public static bool PrintDebug { get; set; } = false; + private static StringBuilder builder = new StringBuilder(); public delegate void LogHandler(LogEventArgs logEventArgs); public static event LogHandler Notify; @@ -28,6 +30,8 @@ namespace PotatoNV_next.Utils var buffer = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} | {status.ToString().ToUpper(),-8} | {message}"; + builder.AppendLine(buffer); + if (status == Status.Debug) { if (!PrintDebug) @@ -86,6 +90,11 @@ namespace PotatoNV_next.Utils Value = value }); } + + public static string GetLog() + { + return builder.ToString(); + } } public class LogEventArgs : EventArgs