Search code examples
c#winformsnotifyiconnotification-area

Returning to Window previously selected before clicking Notification Area Icon


I have built an application that has a Notification Area Icon, which when clicked brings up a form that is designed for a single click, after which the user can then return to what they where doing before hand.

Currently, I am using this.Hide() to remove the form (either when the desired event in the form occurs, or when the form is Deactivated), but when I do, Windows sets the users focus to the Task Bar. How can I get it to return the Users focus to the Window they where on before clicking on the Notification Area Icon?


Solution

  • I have managed to find out how. I used the following code:

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern int SetForegroundWindow(IntPtr hwnd);
    
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();
    
    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern IntPtr GetWindow(IntPtr hwnd, int wFlag);
    
    private void switchToLast() {
        IntPtr thisWindow = GetForegroundWindow();
        IntPtr lastWindow = GetWindow(thisWindow, 3);
        SetForegroundWindow(lastWindow);
        this.Hide();
    }
    

    I found numerous references to the problem, and this is what I have so far. The main difference between this and other examples I have found is the line GetWindow(thisWindow, 3), where the second was 2 in other examples, I have changed this to 3. I believe this is because having it set to 2 was getting the Pointer to the TaskBar.