Created
March 10, 2016 10:12
-
-
Save windwp/2fe41f01d864e5f38a75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uint EVENT_SYSTEM_END = 0x00FF; | |
const uint EVENT_MIN = 0x00000001; | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
if (windowEventHook == IntPtr.Zero) | |
{ | |
var threadId = 0; | |
var processId = Process.GetCurrentProcess().Id; | |
windowEventHook = SetWinEventHook( | |
EVENT_MIN, | |
EVENT_SYSTEM_END, | |
IntPtr.Zero, | |
WindowEventCallback, | |
0, 0, | |
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS | |
); | |
} | |
} | |
private void WindowEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) | |
{ | |
LogText("Event" + eventType); | |
} | |
void LogText(string message) | |
{ | |
richTextBox1.Text += message + Environment.NewLine; | |
} | |
private static IntPtr windowEventHook; | |
private delegate void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); | |
[DllImport("user32.dll", SetLastError = true)] | |
private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, int dwflags); | |
[DllImport("user32.dll", SetLastError = true)] | |
private static extern int UnhookWinEvent(IntPtr hWinEventHook); | |
private const int WINEVENT_INCONTEXT = 4; | |
private const int WINEVENT_OUTOFCONTEXT = 0; | |
private const int WINEVENT_SKIPOWNPROCESS = 2; | |
private const int WINEVENT_SKIPOWNTHREAD = 1; | |
private const int EVENT_SYSTEM_FOREGROUND = 3; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment