Click here to Skip to main content
13,560,066 members
Click here to Skip to main content
Add your own
alternative version


112 bookmarked
Posted 4 Jul 2006
Licenced CPOL

Transparent Flash Control in Plain C++

, 17 Jul 2006
Rate this:
Please Sign up or sign in to vote.
OLE container implementation for hosting a Transparent Flash Player control using plain C++. No ATL, MFC or other libraries are used.


This article illustrates the implementation of a simple OLE container, used for hosting a transparent or windowed Flash Player object.

Part 1. OLE Interfaces

The simplest OLE control implementation for an ActiveX object should consist of several interfaces: IOleClientSite, IOleInPlaceSiteWindowless, IOleInPlaceFrame, and IStorage.

My implementation of the OLE container is called COleContainerWnd. It is declared as follows:

template<CLASS TObj> class COleContainerWnd :
virtual public IOleClientSite,
virtual public IOleInPlaceSiteWindowless,
virtual public IOleInPlaceFrame,
virtual public IStorage

where TObj is the desired interface of an ActiveX object.

Part 2. Flash object

Importing the Flash object into VC++ is done using the #import command:

#import "c:\\windows\\system32\\macromed\\flash\\flash.ocx" named_guids

The named_guids directive is used to generate the CLSID_ShockwaveFlash class ID. If you are running under Windows 2000, change the system folder to "winnt".

As a result of the #import command, the compiler will generate the flash.tli and flash.tlh files containing the Flash Player interface declarations.

Part 3. CFlashWnd derivative class

This class is based on COleContainer, and uses ShockwaveFlashObjects::IShockwaveFlash as the template parameter:

class CFlashWnd :
public COleContainerWnd,
public ShockwaveFlashObjects::_IShockwaveFlashEvents

It also implements the _IShockwaveFlashEvents interface to receive fscommand() events from a Flash movie.

The creation of the CFlashWnd object:

g_flashWnd = new CFlashWnd;
g_hWnd, g_hInst);

The first parameter is the class ID of the Flash Player object. The second parameter is the extended window style, which should be set to WS_EX_LAYERED for a transparent Flash control and 0 for non-transparent. The third is the window style, then goes the owner window and the application instance.

The HWND handle for the OLE container can be retrieved by using the GetHWND() function.

Part 4. Inside CFlashWnd::Create()

First, the window class is registered. Then, a window with the specified styles is created.

The OleCreate function is used to create an instance of the IOleObject object. It passes COleContainer's IOleClientSite and IStorage to the IOleObject object.

Then, OleSetContainedObject is called to inform the object of its embedded state.

The IShockwaveFlash interface is obtained from IOleObject using QueryInterface. IViewObjectEx is obtained in the same way.

If a windowless control is created, then the container needs the IOleInPlaceObjectWindowless interface to dispatch messages to the object since the object does not have its own window. In other words, the IOleInPlaceObject interface is required to draw the object.

IOleObject::DoVerb() is used to show the object and switch it to the running state.

hr = m_lpO->DoVerb(OLEIVERB_SHOW, NULL, (IOleClientSite *)this, 0, NULL, NULL);

Now the Flash Player object is fully created.

Part 5. Transparent window drawing

It is not quite trivial to draw semitransparent translucent windows. The algorithm follows:

  1. Create a WS_POPUP window with the WS_EX_LAYERED style.
  2. Create a 32 bit DIB Section using the CreateDIBSection() function and select it to any compatible DC. It will be an off-screen plain to render window contents to.
  3. Render the window contents, preserving the alpha channel.
  4. Call the UpdateLayeredWindow() function to draw the window to the screen.

To render the Flash player contents, I use the OleDraw helper function, which internally calls the IViewObject::Draw() method:

hr = OleDraw(lpV, DVASPECT_TRANSPARENT, hdcDraw, &rTotal);
  • lpV - the IViewObject interface of the Flash Player control
  • hdcDraw - off-screen plain
  • rTotal - client rectangle of the container window

The DVASPECT_TRANSPARENT drawing aspect tells the object to draw its contents using alpha blending.

While implementing this, I met with a serious bug in Flash Player Control 8. This bug is only in this version. Players 7 and 9 are free of it. The bug is in the way Flash Control fills the alpha channel of a 32 bit device context. If at least 1 of 255 alpha values is applied to a pixel, the colors are mixed correctly, but the resulting alpha channel is set to 255, even if it was initially zero. This makes it impossible to create semitransparent windows. So I had to develop a solution to fix this bug. The solution is quite simple:

These equations are used by the Flash Player control for alpha blending:

R' = Rdst * (1 - alpha) + Rsrc * alpha
G' = Gdst * (1 - alpha) + Gsrc * alpha
B' = Bdst * (1 - alpha) + Bsrc * alpha

If I draw the contents of Flash onto a black surface, I get:

R'black = Rsrc * alpha
G'black = Gsrc * alpha
B'black = Bsrc * alpha

If I draw the contents of Flash onto a white surface, I get:

R'white = 255 * (1 - alpha) + Rsrc * alpha
G'white = 255 * (1 - alpha) + Rsrc * alpha
B'white = 255 * (1 - alpha) + Rsrc * alpha

Here is the system of equations:

R'black = Rsrc * alpha
R'white = 255 * (1 - alpha) + Rsrc * alpha

where alpha and Rsrc are unknown. After solving them, you will get:

(255-Alpha) = R'white - R'black
Alpha = 255 - (R'white - R'black)

So, a solution was found. Now, we can draw the contents of a Flash Player twice and then correct the corrupted alpha channel.

Part 6. Events

Flash Control events are handled using IDispatch. After the Flash control is created, we retrieve a IConnectionPointContainer and try to find the DIID__IShockwaveFlashEvents connection point:

hr = m_lpControl->QueryInterface(IID_IConnectionPointContainer, (void**)&m_lpConCont);
if (FAILED(hr))
    return FALSE;
hr = m_lpConCont->FindConnectionPoint(
      ShockwaveFlashObjects::DIID__IShockwaveFlashEvents, &m_lpConPoint);
if (FAILED(hr))
    return FALSE;
hr = m_lpConPoint->Advise((ShockwaveFlashObjects::_IShockwaveFlashEvents*)this, 
if (FAILED(hr))
    return FALSE;

After successful Advise(), we will receive the events in the IDispatch::Invoke method.

Part 7. DirectDraw (Updated)

The Flash control can slightly improve drawing performance by using a DirectDraw3 interface. The Flash object queries it through the ShockwaveFlashObjects::IServiceProvider::raw_RemoteQueryService method. It passes the IID_IDirectDraw3 GUID as guidService and RIID parameters. The implementation looks like this:

//include direct draw header (do not link ddraw.lib) 
#include <ddraw.h> 

//declare required GUIDs 
#ifndef DEFINE_GUID2 
#define DEFINE_GUID2(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ 

const GUID name \ 
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } 



//declare smart pointer type for IDirectDraw4 interface 
_COM_SMARTPTR_TYPEDEF(IDirectDraw4, IID_IDirectDraw4); 

//the implementation of IServiceProvider interface 
HRESULT __stdcall CFlashWnd::raw_RemoteQueryService ( 
GUID * guidService, 
GUID * riid, 
IUnknown * * ppvObject ) 
    HRESULT hr; 

    if (IsEqualGUID(*guidService, IID_IDirectDraw3)) 
        if (!m_lpDD4) 
            m_lpDD4 = new IDirectDraw4Ptr; 
            hr = m_lpDD4->CreateInstance(CLSID_DirectDraw, NULL, 
            if (FAILED(hr)) 
                delete m_lpDD4; 
                m_lpDD4 = NULL; 
                return E_NOINTERFACE; 

        if (m_lpDD4 && m_lpDD4->GetInterfacePtr()) 
            *ppvObject = m_lpDD4->GetInterfacePtr(); 
            return S_OK; 

    return E_NOINTERFACE; 


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


About the Author

Makarov Igor
Web Developer
Russian Federation Russian Federation
Visual C++ developer, team leader (mailto:

You may also be interested in...


Comments and Discussions

QuestionCrash in 64 bit application Pin
Member 97340134-Sep-15 12:00
memberMember 97340134-Sep-15 12:00 
QuestionTransparent Flash Control in Plain C++ application is not working in Window 8 OS.Application is crashing. Pin
anilkumark23-Dec-12 20:29
memberanilkumark23-Dec-12 20:29 
AnswerRe: Transparent Flash Control in Plain C++ application is not working in Window 8 OS.Application is crashing. Pin
jtkim7217-Oct-15 20:11
memberjtkim7217-Oct-15 20:11 
Question64 bit flash Pin
Member 830542810-Oct-11 7:45
memberMember 830542810-Oct-11 7:45 
AnswerRe: 64 bit flash Pin
Member 115979935-Jun-15 22:48
memberMember 115979935-Jun-15 22:48 
QuestionHow to play youtube videos where URL is hidden??? ramanujamkv 1:11 23 Sep '11 Pin
ramanujamkv26-Sep-11 4:31
memberramanujamkv26-Sep-11 4:31 
GeneralSame source code now available with example for ActionScript support Pin
Fred van Lieshout8-Apr-11 3:09
memberFred van Lieshout8-Apr-11 3:09 
GeneralControlling Nested Timelines Pin
Knives8929-Dec-10 23:44
memberKnives8929-Dec-10 23:44 
GeneralDirty Rect Pin
Knives8929-Dec-10 23:27
memberKnives8929-Dec-10 23:27 
GeneralCouple of suggestions Pin
driverscience13-Oct-10 3:28
memberdriverscience13-Oct-10 3:28 
GeneralExcellent Article! ...regarding flash10g.ocx Pin
gyrtenudre25-Sep-10 9:12
membergyrtenudre25-Sep-10 9:12 
GeneralClick event Pin
codon128-Jul-10 5:25
membercodon128-Jul-10 5:25 
GeneralRe: Click event Pin
codon12-Aug-10 10:00
membercodon12-Aug-10 10:00 
GeneralGDI Object Leak Pin
Isaiah.net15-Apr-10 6:20
memberIsaiah.net15-Apr-10 6:20 
GeneralProblems with Flash 10.1 beta Pin
megatron2428-Mar-10 4:56
membermegatron2428-Mar-10 4:56 
QuestionA Question about how the event part works?to catch and deal the "fscommand()" messages generated by the swf file? Pin
wendywind23-Nov-09 19:48
memberwendywind23-Nov-09 19:48 
QuestionHow do you port this into MFC using dialogs? Pin
Rb Arguelles17-Nov-09 20:35
memberRb Arguelles17-Nov-09 20:35 
GeneralMemory hogging when ActionScript is added Pin
Boris Epelbaum30-Sep-09 4:11
memberBoris Epelbaum30-Sep-09 4:11 
Questionpuzzle on CreateWindowEx() Pin
xins01239-Jul-09 16:42
memberxins01239-Jul-09 16:42 
QuestionRe: puzzle on CreateWindowEx() Pin
xins01239-Jul-09 17:35
memberxins01239-Jul-09 17:35 
QuestionWhy the flash window can't move automatically follow its parent? [modified] Pin
xins01238-Jul-09 22:39
memberxins01238-Jul-09 22:39 
GeneralCrash with Adobe Flash Player 10b ( Pin
Mad_Gandalf13-May-09 23:16
memberMad_Gandalf13-May-09 23:16 
GeneralRe: Crash with Adobe Flash Player 10b ( Pin
RaPit16-May-09 13:28
memberRaPit16-May-09 13:28 
GeneralRe: Crash with Adobe Flash Player 10b ( Pin
Attila F22-Jul-09 21:55
memberAttila F22-Jul-09 21:55 
GeneralRe: Crash with Adobe Flash Player 10b ( Pin
Rb Arguelles10-Nov-09 21:42
memberRb Arguelles10-Nov-09 21:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180527.3 | Last Updated 18 Jul 2006
Article Copyright 2006 by Makarov Igor
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid