Skip to content

Instantly share code, notes, and snippets.

@windwp
Created March 10, 2016 10:12
Show Gist options
  • Save windwp/2fe41f01d864e5f38a75 to your computer and use it in GitHub Desktop.
Save windwp/2fe41f01d864e5f38a75 to your computer and use it in GitHub Desktop.
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