Search code examples
.netwinformsappearance

Keep Window Inactive In Appearance Even When Activated


Is there a way to keep a window inactive looking, even if it contains focus? I have two forms (A and B). After the user interacts with A, I transfer focus back to B. The result of the focus transfers (the user clicking on the A, then focus being transferred back to B) is that form A blinks from active to inactive. This looks ugly (especially on Vista where A momentarily gets a bigger shadow). How can I make A stay inactive looking so this blinking will not happen?


Solution

  • At last, I found the answer!

    WARNING: DO NOT abuse this answer's technique. Doing so will confuse your users and will be harmful to the computing experience in general. The technique described below can be very useful under certain circumstances (e.g implementing IntelliSense-like behavior), but please be judicious in your usage of it.

    The WM_NCACTIVATE message is sent to a window to change the state of its non-client area (i.e. border and titlebar) to inactive or active. The wParam of the message indicates whether the state will be inactive or active. If the wParam is true (a value of 1), the window will look active. If the wParam is false (a value of 0), the window will look inactive. To force a window to stay either inactive or active, override the wParam by setting it to the corresponding value (0 or 1), and you will be all set!

    private const int WM_NCACTIVATE = 0x0086;
    
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_NCACTIVATE)
        {
            // Use this to make it always look inactive:
            m.WParam = (IntPtr)0;
    
            // Alternately, use this to make it always look active:
            m.WParam = (IntPtr)1;
        }
    
        base.WndProc(ref m);
    }