Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire event when user changes active process

Tags:

c#

winforms

Is there an event, or a way to create an event, that fires when the program user changes the active window to that of a different process?

If there is no such event, what is the best way to make something that is something like it?

I'm currently having a timer that runs Process.GetCurrentProcess() every 3 seconds, but I'm searching for better and more efficient ways of doing it and I don't want to lower the interval because of the risk of making the program take too much resources or that it takes too much time to constantly check for the active process.

I know that there is a lot of Windows built in functions that is basically hidden that I don't have enough knowledge to know of, so if anyone have any idea of something like this, it would be great if you could help me out.

like image 757
stripe103 Avatar asked Jun 20 '13 20:06

stripe103


People also ask

What are event args?

EventArgs is also the class you use when an event does not have any data associated with it. When you create an event that is only meant to notify other classes that something happened and does not need to pass any data, include the EventArgs class as the second parameter in the delegate. You can pass the EventArgs.

How to handle event in C#?

Event handlers in C#An event handler in C# is a delegate with a special signature, given below. The first parameter (sender) in the above declaration specifies the object that fired the event. The second parameter (e) of the above declaration holds data that can be used in the event handler.

What is sender in event C#?

About events, the sender parameter is always the object that generated the event (for example a Button in a Click event on a button). If you want to pass arbitrary data in a custom event, inherits from EventArgs and pass it as the second argument.


1 Answers

The SetWinEventHook API does exactly what you are looking for here. All you need to do is call this when your app starts up with the correct options and you should start to receive notifications whenever the user changes focus from any processes currently running on the desktop.

[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, uint dwflags);
[DllImport("user32.dll")]
internal static extern int UnhookWinEvent(IntPtr hWinEventHook);
internal delegate void WinEventProc(IntPtr hWinEventHook, uint iEvent, IntPtr hWnd, int      idObject, int idChild, int dwEventThread, int dwmsEventTime);

const uint WINEVENT_OUTOFCONTEXT = 0;
const uint EVENT_SYSTEM_FOREGROUND = 3;
private IntPtr winHook;
private WinEventProc listener;

public void StartListeningForWindowChanges()
{
    listener = new WinEventProc(EventCallback);
    //setting the window hook
    winHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, listener, 0, 0, WINEVENT_OUTOFCONTEXT);
}

public void StopListeningForWindowChanges()
{
    UnhookWinEvent(winHook);
}

private static void EventCallback(IntPtr hWinEventHook, uint iEvent, IntPtr hWnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime)
{
    // handle active window changed!
}
like image 107
James Avatar answered Sep 29 '22 17:09

James