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
|
public partial class Form1 : Form
{
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
[DllImport("user32.dll")]
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
uint idThread, uint dwFlags);
[DllImport("user32.dll")]
static extern bool UnhookWinEvent(IntPtr hWinEventHook);
private enum SystemEvents : uint
{
EVENT_SYSTEM_CREATE = 3,
EVENT_SYSTEM_DESTROY = 0x8001,
EVENT_SYSTEM_MINIMIZESTART = 0x0016,
EVENT_SYSTEM_MINIMIZEEND = 0x0017,
EVENT_SYSTEM_FOREGROUND = 0x0003,
EVENT_SYSTEM_MENUSTART = 0x0004,
EVENT_SYSTEM_MENUEND = 0x0005,
}
public void WinEvenHooker(IntPtr even, uint type, IntPtr hwn, int obj, int child, uint dwthread, uint dwms)
{
//MessageBox.Show("Event Hooker");
if (type == (uint)SystemEvents.EVENT_SYSTEM_FOREGROUND)
{
Trace.WriteLine("Event foreground");
this.WindowState = FormWindowState.Normal;
}
}
IntPtr globalHook = IntPtr.Zero;
public Form1()
{
InitializeComponent();
FormClosing += new FormClosingEventHandler(Form1_FormClosing);
globalHook = SetWinEventHook((uint)SystemEvents.EVENT_SYSTEM_FOREGROUND, (uint)SystemEvents.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero,
WinEvenHooker, 0, 0, 0);
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnhookWinEvent(globalHook);
}
} |
Partager