0

What i mean is for example i'm now watching the visual studio window then i click on the bottom on the Chrome icon and now i'm in the Chrome window .

I want it to detect that the window i'm in now is changed . Not if i clicked on the chrome icon or on the visual studio icon but to detect somehow that i changed the window i'm watching/active/using now .

For example this code :

In the top of my Form1 i added :

public delegate void ActiveWindowChangedHandler(object sender, String windowHeader, IntPtr hwnd);
        public event ActiveWindowChangedHandler ActiveWindowChanged;
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

        delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
            IntPtr hwnd, int idObject, int idChild, uint dwEventThread,
            uint dwmsEventTime);

        const uint WINEVENT_OUTOFCONTEXT = 0;
        const uint EVENT_SYSTEM_FOREGROUND = 3;

        [DllImport("user32.dll")]
        static extern bool UnhookWinEvent(IntPtr hWinEventHook);

        [DllImport("user32.dll")]
        static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax,
            IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc,
            uint idProcess, uint idThread, uint dwFlags);

        IntPtr m_hhook;
        private WinEventDelegate _winEventProc;

Then in the constructor :

_winEventProc = new WinEventDelegate(WinEventProc);
            m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND,
                EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _winEventProc,
                0, 0, WINEVENT_OUTOFCONTEXT);

Then added events and functions :

void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd,
            int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
       {
            if (eventType == EVENT_SYSTEM_FOREGROUND)
            {
               if (ActiveWindowChanged != null)
                  ActiveWindowChanged(this,GetActiveWindowTitle(hwnd),hwnd);
            }
       }

       private string GetActiveWindowTitle(IntPtr hwnd)
       {
          StringBuilder Buff = new StringBuilder(500);
          GetWindowText(hwnd, Buff, Buff.Capacity);
          return Buff.ToString();
       }

       ~Form1()
       { 
        UnhookWinEvent(m_hhook);
       }

But i'm not sure how to get it work and how to use it and if it's the right code at all .

Could you show me please an example of a code how to do it ?

Revuen Ben Dror
  • 237
  • 7
  • 17
  • possible duplicate of [How Can I Monitor Which Window Currently Has Keyboard Focus](http://stackoverflow.com/questions/44241/how-can-i-monitor-which-window-currently-has-keyboard-focus). Also http://stackoverflow.com/q/2966413/56778 – Jim Mischel May 29 '13 at 22:18
  • possible duplicate of [Is there Windows system event on active window changed?](http://stackoverflow.com/questions/4407631/is-there-windows-system-event-on-active-window-changed) – Ben Voigt May 30 '13 at 04:24

1 Answers1

0

Yes, you can use SetWinEventHook function.

hEvent = SetWinEventHook(EVENT_SYSTEM_FOREGROUND , 
    EVENT_SYSTEM_FOREGROUND , NULL, 
    WinEventProcCallback, 0, 0, 
    WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);

.......

VOID CALLBACK WinEventProcCallback ( HWINEVENTHOOK hWinEventHook, DWORD dwEvent, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
    /* your code here */
}

Quoted DReJ from here.

Community
  • 1
  • 1
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70