可以把swf文件放到资源中,在需要执行swf文件时,
1 把swf从资源中加载到内存中
2 把swf的内容传给flash 控件,然后开始播放。
具体的方法是:从flash对象中获取IID_IPersistStreamInit,用这个接口来把swf的文件内容传递给flash控件
IUnknown* u = _flash.GetControlUnknown();
if(u != NULL)
{
IPersistStreamInit* psi;
HRESULT hr = u->QueryInterface(IID_IPersistStreamInit, reinterpret_cast(&psi));
if(SUCCEEDED(hr))
{
HRSRC res = FindResource(NULL, MAKEINTRESOURCE(IDR_SWF), "SWF");
HGLOBAL h = LoadResource(NULL, res);
unsigned int len = SizeofResource(NULL, res);
if(! load_movie_from_stream(psi, reinterpret_cast(LockResource(h)), len))
{
}
psi->Release();
}
}
bool load_movie_from_stream(IPersistStreamInit* psi, unsigned char* data, unsigned int len)
{
bool successful = false;
bool compressed = false;
unsigned char* buffer = NULL;
unsigned int movie_size;
ULARGE_INTEGER size, cb;
LARGE_INTEGER li;
memmove(&movie_size, data + 4, 4);
__try
{
//compressed?
if(memcmp(data, "CWS", 3) == 0)
{
compressed = true;
unsigned long d_len;
buffer = new unsigned char[movie_size];
memmove(buffer, data, 8);
buffer[0] = 'F';
int r = uncompress(buffer + 8, &d_len, data + 8, len - 8);
if(r != Z_OK)
{
__leave;
}
}
else
{
buffer = data;
}
//save movie template
psi->GetSizeMax(&size);
HGLOBAL h1 = GlobalAlloc(GMEM_MOVEABLE, size.LowPart);
IStream* s1;
CreateStreamOnHGlobal(h1, FALSE, &s1);
psi->Save(s1, TRUE);
//insert movie into template
HGLOBAL h2 = GlobalAlloc(GMEM_MOVEABLE, size.LowPart + movie_size);
IStream* s2;
CreateStreamOnHGlobal(h2, FALSE, &s2);
//magic flag. "gUfU" means no movie loaded, "fUfU" means movie loaded
s2->Write("f", 1, NULL);
li.HighPart = 0;
li.LowPart = 1;
s1->Seek(li, SEEK_SET, NULL);
cb.HighPart = 0;
cb.LowPart = 3;
s1->CopyTo(s2, cb, NULL, NULL);
//write movie size
s2->Write(&movie_size, 4, NULL);
//write movie
s2->Write(buffer, movie_size, NULL);
cb.HighPart = 0;
cb.LowPart = size.LowPart - 4;
s1->CopyTo(s2, cb, NULL, NULL);
//load movie
li.HighPart = 0;
li.LowPart = 0;
s2->Seek(li, SEEK_SET, NULL);
psi->Load(s2);
s1->Release();
s2->Release();
GlobalFree(h1);
GlobalFree(h2);
successful = true;
}
__finally
{
if(compressed && buffer != NULL)
{
delete[] buffer;
}
}
return successful;
}