65.9K
CodeProject is changing. Read more.
Home

Class for Synchronizing the Active State of multiple windows.

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Mar 11, 2002

2 min read

viewsIcon

44243

downloadIcon

526

Class for Synchronizing the Active State of multiple windows.


  • Download demo and source code - 30 Kb

    Introduction

    With the class ISyncActiveImpl, of this article, you can have windows that synchronize the active state with a owner window.

    Features implemented:

    • Windows that can synchronize the active state with a owner window;
    • Windows that automatically hide when the application is deactivated and when the owner window is minimized or hidden;
    • Windows that are automatically disabled when owner window is disabled.

    Usage

    I will base these examples in the source-code of the demo application included in this article.

    1. Include the ISyncActiveImpl.h header file in your source-file.

    2. Derive your class from ISyncActiveImpl<>.

      class CMainDlg :
        public CDialogImpl<CMainDlg>
      , public ISyncActiveImpl<CMainDlg>
      , public CMessageFilter
    3. In your class constructor make sure that you instantiate ISyncActiveImpl<> class with the right arguments for your type of window.

      For example, for creating a dialog or frame window:

      CMainDlg()
        // this dialog does not sync the active state with
      
        // owner and it must be always visible.
      
        : ISyncActiveImpl<CMainDlg>(FALSE, TRUE)
        {
        }
      

      Or for creating a floating or toolbar window:

      CFloatingWindow()
        // this window will synchronize the active state with
      
        // owner and will automatically be hidden when owner
      
        // is hidden/minimized or when application looses the
      
        // active state.
      
        : ISyncActiveImpl<CFloatingWindow>(TRUE)
        {
        }
      
    4. In the message map of your class add a CHAIN_MSG_MAP macro.

      BEGIN_MSG_MAP(CMainDlg)
        CHAIN_MSG_MAP(ISyncActiveImpl<CMainDlg>)
        .
        .
        .
      END_MSG_MAP()

      NOTE: Make sure that you insert it on the first entry of the message map. This is needed because the ISyncActiveImpl<> class will need to intercept some general messages (i.e. WM_CREATE).

    Under the hood

    The key for synchronizing the active state of a window lies on the WM_NCACTIVATE message. By handling or sending this message we can force the painting of a window in the active or inactive state.

    The automatically hide of a window is archived by handling the WM_ACTIVATEAPP message.

    The other two important messages that need to be handled are WM_ACTIVATE and WM_SHOWWINDOW, you can find information about them in source-code and at MSDN.

    For ending, I just want to say that when implementing this class I have run into two "strange" activation problems, read all about them in the source-code.

    History

    • 11 mar 2002
      • first public version.

    Do you have any question or comment? Contact me!