From 75e0ded34bba7778aeb52082fcf82670a84436b6 Mon Sep 17 00:00:00 2001 From: Min Date: Mon, 6 Jun 2022 09:40:29 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=98=A4=ED=94=84/=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=EC=8B=9C=20=EC=9E=90=EB=8F=99=20=EC=A0=80=EC=9E=A5=20?= =?UTF-8?q?=EC=95=88=EB=90=98=EB=8D=98=20=EB=AC=B8=EC=A0=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pctory/Form1.cs | 39 +++++++++++++++++++++++++++++++++------ pctory/Program.cs | 9 ++++++++- pctory/WinApi.cs | 6 +++++- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/pctory/Form1.cs b/pctory/Form1.cs index 3eff080..0d3f687 100644 --- a/pctory/Form1.cs +++ b/pctory/Form1.cs @@ -35,11 +35,8 @@ private void InvalidateText() dataGridView1.Rows.Clear(); dataGridView1.Rows.AddRange(viewInitializer.ProcInfoList2DVGRows(tracer.ProcInfoList)); - Application.ApplicationExit += (sender, e) => - { - closer = true; - tracer.StopTrace(); - }; + + } public Form1() { @@ -56,13 +53,42 @@ public Form1() tsmiTracerRun_Click(null, null); daytrace = new DayTrace(FileAutoOutput_DayChange); + Application.ApplicationExit += new EventHandler((sender, e) => + { + closer = true; + tracer.StopTrace(); + FileAutoOutput_Closing(sender, e); + }); + + Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler((sender, e) => + { + closer = true; + tracer.StopTrace(); + FileAutoOutput_Closing(sender, e); + }); + + Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEndingEventHandler((sender, e) => + { + closer = true; + tracer.StopTrace(); + FileAutoOutput_Closing(sender, e); + }); + } + + protected override void WndProc(ref Message m) + { + if (WinApi.Message.WM_QUERYENDSESSION == m.Msg) + { + tracer.StopTrace(); + FileAutoOutput_Closing(null, null); + } + base.WndProc(ref m); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (!closer) e.Cancel = true; - else FileAutoOutput_Closing(sender, e); Hide(); } @@ -159,6 +185,7 @@ private void noti_MouseDoubleClick(object sender, MouseEventArgs e) private void OpenForm() { Show(); + Activate(); } private void 열기ToolStripMenuItem_Click(object sender, EventArgs e) diff --git a/pctory/Program.cs b/pctory/Program.cs index 291532e..d7f7913 100644 --- a/pctory/Program.cs +++ b/pctory/Program.cs @@ -6,6 +6,7 @@ using System.IO; using System.Threading; +using System.Runtime.InteropServices; using System.Diagnostics; using System.Text; @@ -45,7 +46,13 @@ static void Main(string[] args) }else if (args[0] == "--view") { - Application.Run(new fViewer(null)); + if(isFirst) Application.Run(new fViewer(null)); + else + { + IntPtr hWnd = WinApi.FindWindow(null, "Log View"); + WinApi.SetForegroundWindow(hWnd); + } + } vMutex.Close(); return; diff --git a/pctory/WinApi.cs b/pctory/WinApi.cs index 5d50816..38e11f8 100644 --- a/pctory/WinApi.cs +++ b/pctory/WinApi.cs @@ -13,8 +13,9 @@ internal class WinApi { public static class Message { + public const uint WM_QUERYENDSESSION = 0x11; public const uint WM_COPYDATA = 0x4A; - + public const uint WM_SETFOCUS = 0x07; } public enum EventCode : int @@ -121,6 +122,9 @@ public enum SetWinEventHookFlags [DllImport("user32.dll", SetLastError = false)] public static extern bool UnhookWinEvent(int hWinEventHook); + [DllImport("user32.dll", SetLastError = false)] + public static extern bool SetForegroundWindow(IntPtr hWnd); + // ref. https://ehdrn.tistory.com/295 public struct COPYDATASTRUCT {