-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
165 lines (145 loc) · 5.54 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WinEventHook
{
public partial class WinEventHook : Form
{
internal delegate void WinEventProc(IntPtr hWinEventHook, int iEvent, IntPtr hWnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess,
int idThread, SetWinEventHookFlags dwflags);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool UnhookWinEvent(IntPtr hWinEventHook);
private WinEventProc listener = null;
private IntPtr hWinEventHook = IntPtr.Zero;
private int lastEventTime = 0;
const int EVENT_SYSTEM_FOREGROUND = 0x0003;
const int EVENT_SYSTEM_CONTEXTHELPEND = 0x000D;
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
internal enum SetWinEventHookFlags
{
WINEVENT_OUTOFCONTEXT = 0,
WINEVENT_SKIPOWNPROCESS = 2
}
public WinEventHook()
{
InitializeComponent();
}
private void EventCallback(IntPtr hWinEventHook, int iEvent, IntPtr hWnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime)
{
string event_string = event_to_str(iEvent);
if(event_string != null)
{
if (lastEventTime == 0)
{
txt_log.Text = txt_log.Text + Environment.NewLine + event_string + " at " + dwmsEventTime;
}
else
{
txt_log.Text = txt_log.Text + Environment.NewLine + event_string + " +" + (dwmsEventTime - lastEventTime) + "ms.";
}
}
lastEventTime = dwmsEventTime;
}
private void btc_action_Click(object sender, EventArgs e)
{
if(hWinEventHook == IntPtr.Zero)
{
if(listener == null)
{
listener = new WinEventProc(EventCallback);
}
List<Process> processes_find = findProcessesByName(txt_process_name.Text);
if(processes_find.Count != 1)
{
MessageBox.Show("Processes found: " + processes_find.Count);
return;
}
hWinEventHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_CONTEXTHELPEND, IntPtr.Zero, listener,
processes_find[0].Id, 0,SetWinEventHookFlags.WINEVENT_OUTOFCONTEXT | SetWinEventHookFlags.WINEVENT_SKIPOWNPROCESS);
txt_log.Text = "";
btn_action.Text = "Stop";
}
else
{
UnhookWinEvent(hWinEventHook);
hWinEventHook = IntPtr.Zero;
btn_action.Text = "Start";
}
}
private void txt_process_name_TextChanged(object sender, EventArgs e)
{
}
private void txt_log_TextChanged(object sender, EventArgs e)
{
}
static string event_to_str(int event_id)
{
if (event_id != 0x0009 && event_id != 0x0003)
{
return null;
}
switch (event_id)
{
case 0x0003:
return "SYS_FOREGROUND";
case 0x0004:
return "SYS_MENUSTART";
case 0x0006:
return "SYS_MENUPOPUPSTART";
case 0x0007:
return "SYS_MENUPOPUPEND";
case 0x0008:
return "SYS_CAPTURESTART";
case 0x0009:
return "SYS_CAPTUREEND";
case 0x000a:
return "SYS_MOVESIZESTART";
case 0x000b:
return "SYS_MOVESIZEEND";
case 0x0016:
return "SYS_MINIMIZESTART";
default:
return "" + event_id;
}
}
private void Win_Main_Dispose(object sender, EventArgs e)
{
if(hWinEventHook != IntPtr.Zero)
{
UnhookWinEvent(hWinEventHook);
hWinEventHook = IntPtr.Zero;
}
}
private void Win_Main_Load(object sender, EventArgs e)
{
SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
}
private List<Process> findProcessesByName(string name)
{
if (name.EndsWith(".exe"))
{
name = name.Substring(0, name.Length - 4);
}
List<Process> processes_find = new List<Process>();
Process[] ps = Process.GetProcesses();
foreach (Process p in ps)
{
if (p.ProcessName.Equals(name))
{
processes_find.Add(p);
}
}
return processes_find;
}
}
}