Click here to Skip to main content
11,581,852 members (77,146 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WPF Win7
Hi all,

public class ActiveWindow
{
    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;
 
    public ActiveWindow()
    {
        _winEventProc = new WinEventDelegate(WinEventProc);
        m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND,
            EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _winEventProc,
            0, 0, WINEVENT_OUTOFCONTEXT);
    }
 
    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();
    }
 
    ~ActiveWindow()
    {
        UnhookWinEvent(m_hhook);
    }
}

when i switch between the active windows i get the callback
but when i maximize a minimized window i don't get a call back,

i find a work throw to solve this problem, but i am seeking for better solution

any help will be appreciated
Posted 12-Feb-12 9:23am
Edited 13-Feb-12 17:26pm
v7
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why not use the CBTProc callback function instead? You can use it for all the events you are interested in (and even more).
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644977%28v=vs.85%29.aspx[^]

Good luck!
  Permalink  
Comments
Espen Harlinn at 14-Feb-12 11:25am
   
Good idea :)
Mohamed_Ahmed at 15-Feb-12 2:34am
   
nice, but only i need the call back after the minimized or maximized not before,
but thank you very much for your response
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

i need to use either the EVENT_SYSTEM_MINIMIZESTART or EVENT_SYSTEM_MINIMIZEEND event constant to receive notification of window objects being minimized.

i Used the eventMin and eventMax parameters of the SetWinEventHook function to indicate that i am interested in receiving notifications for one of these events and EVENT_SYSTEM_FOREGROUND.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 345
1 Sergey Alexandrovich Kryukov 220
2 Andy Lanng 114
3 virusstorm 109
4 Kornfeld Eliyahu Peter 90
0 OriginalGriff 1,358
1 Sergey Alexandrovich Kryukov 970
2 Abhinav S 671
3 F-ES Sitecore 505
4 Suvendu Shekhar Giri 448


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 15 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100