HOWTO: Display CAnimateCtrl with Transparent Background

Last reviewed: January 29, 1998
Article ID: Q179907
The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK) on the following platforms:

         - Microsoft Windows NT version 4.0
         - Microsoft Windows 95
    
  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.0a, 4.1, 4.2,
          4.2b, 5.0
    

SUMMARY

This article shows how to display .avi files with a transparent background. The CAnimateCtrl control supports .avi files with a transparent background with a limitation of 16 colors. The first pixel in the first frame of an .avi file determines the background color of the CAnimateCtrl control.

MORE INFORMATION

To display an .avi file on CDialog or its derived classes, you need to set the style of CAnimateCtrl to ACM_TRANSPARENT. If the color of the first pixel in the first frame of an .avi file is different from the background color of the CDialog object, then the background color of the CDialog object is used as the background color of the .avi file. Thus, the CAminateCtrl control will have a transparent background.

To display an .avi file on CView or its derived classes, you must intercept the WM_CTLCOLOR message in the CView class and return a null brush (not to be confused with just returning NULL) for the CAnimateCtrl. In addition, you must set the style of CAnimateCtrl to ACM_TRANSPARENT. Otherwise, the default dialog box background color will be used as the background color for the animation control.

For example, if a CView derived class is CTestanimctrlView, you need to add the ACM_TRANSPARENT style to the CAnimateCtrl, add ON_WM_CTLCOLOR into the message map, and add an OnCtlColor message handler into the CTestanimctrlView class as described in the following steps:

Sample Code

Add the following code into the .h file of CTestanimctrlView:

   CAnimateCtrl* m_pMyAnimateCtrl;
      afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);

Add the following code into the .cpp file of CTestanimctrlView:

  1. Add the following code in CTestanimctrlView::OnInitialUpdate:

       // Create my animate control.
       m_pMyAnimateCtrl = new CAnimateCtrl;
    
          UINT styles = WS_CHILD |  ACS_TRANSPARENT |  ACS_AUTOPLAY;
          m_pMyAnimateCtrl->Create(styles, CRect(100, 50, 300, 300), this,
             ID_MYANIM);   // here ID_MYANIM is the ID of m_pMyAnimateCtrl
    
    

  2. Add ON_MN_CTLCOLOR() into the message map:

    BEGIN_MESSAGE_MAP(CTestanimctrlView, CView)

          //{{AFX_MSG_MAP(CTestanimctrlView)
          ON_WM_CTLCOLOR()
    
    END_MESSAGE_MAP()

  3. Add the message handler for WM_CTLCOLOR:

    HBRUSH CCTestanimctrlView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT

          nCtlColor)
    
    {

          UINT id = pWnd->GetDlgCtrlID();
          if (id == ID_MYANIM)
    
             return (HBRUSH)GetStockObject(NULL_BRUSH);
    
          HBRUSH hbr = CView::OnCtlColor(pDC, pWnd, nCtlColor);
          return hbr;
       }
    
    

  4. Free up the memory used by the animate control:

          CTestanimctrlView::~CTestanimctrlView()
          {
    
             if (m_pMyAnimateCtrl) {
                m_pMyAnimateCtrl->DestroyWindow();
                delete m_pMyAnimateCtrl;
             }
          }
    
Version : win95; WINNT:4.0


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 29, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.