項(xiàng)目中要實(shí)現(xiàn)Flex打開文件夾選擇框(Flex做為桌面程序的UI),沒辦法,如果不用AIR只能在下面加一層Container了。網(wǎng)上搜來搜去差不多都是講FSCommand怎樣與VC++交互,可是FSCommand不能及時(shí)返回值呀。經(jīng)過一番摸索,終于調(diào)通了ExternalInterface在VC++中的處理流程,看代碼。
- void CMyBicapDlg::OnFlashCallShockwaveflash1(LPCTSTR request)
- {
-
-
-
-
- TiXmlDocument request_xml;
- request_xml.Parse(request);
- const char* request_name = request_xml.RootElement()->Attribute("name");
-
- if (strcmp(request_name,"savedVideosDirectory") == 0 || strcmp(request_name,"bufferDirectory") == 0 || strcmp(request_name,"preferredExportDirectory") == 0)
- {
-
- CoInitialize(NULL);
- BROWSEINFO bi;
- bi.hwndOwner = this->GetSafeHwnd();
- bi.pidlRoot = NULL;
- bi.pszDisplayName = NULL;
- bi.lpszTitle = NULL;
- bi.ulFlags = BIF_BROWSEFORCOMPUTER|BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;
- bi.lpfn = NULL;
- LPCITEMIDLIST pidl = SHBrowseForFolder(&bi);
- if(pidl != NULL)
- {
- TCHAR tpath[MAX_PATH] = _T("");
- BOOL bresult = SHGetPathFromIDList(pidl, tpath);
- if (bresult)
- {
- std::string re_value = "<string>";
- re_value = re_value+tpath+"</string>";
- m_FlashPlayer.SetReturnValue(re_value.c_str());
- }
- }
-
- CoUninitialize();
- }
-
- }
void CMyBicapDlg::OnFlashCallShockwaveflash1(LPCTSTR request){// TODO: Add your control notification handler code here// "<invoke name='%s' returntype='xml'><arguments><string>%s</string></arguments></invoke>"http:// parse requestTiXmlDocument request_xml;request_xml.Parse(request);const char* request_name = request_xml.RootElement()->Attribute("name");if (strcmp(request_name,"savedVideosDirectory") == 0 || strcmp(request_name,"bufferDirectory") == 0 || strcmp(request_name,"preferredExportDirectory") == 0){// choose pathCoInitialize(NULL);BROWSEINFO bi;bi.hwndOwner = this->GetSafeHwnd();bi.pidlRoot = NULL;bi.pszDisplayName = NULL;bi.lpszTitle = NULL;bi.ulFlags = BIF_BROWSEFORCOMPUTER|BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;bi.lpfn = NULL;LPCITEMIDLIST pidl = SHBrowseForFolder(&bi);if(pidl != NULL){TCHAR tpath[MAX_PATH] = _T("");BOOL bresult = SHGetPathFromIDList(pidl, tpath);if (bresult){std::string re_value = "<string>";re_value = re_value+tpath+"</string>";m_FlashPlayer.SetReturnValue(re_value.c_str());}}CoUninitialize();}}
首先,需要在項(xiàng)目中嵌入Flash player插件,網(wǎng)上有很多例子。另外Flex也要寫好代碼,這里略掉。
-
添 加一個(gè)ExternalInterface的事件處理函數(shù),對(duì)于Flash player來講就是FlashCall事件(跟FSCommand不同的),這里的事件處理函數(shù)是void CMyBicapDlg::OnFlashCallShockwaveflash1(LPCTSTR request)。沒有返回值(下面會(huì)講到),參數(shù)是一個(gè)XML格式的字符串。格式是"<invoke name='%s' returntype='xml'><arguments><string>%s</string>< /arguments></invoke>",去查查幫助就知道了。
-
處理request:標(biāo)準(zhǔn)C++沒有處理XML的庫,我去下載了tinyxml,小巧好用。下面就是按照個(gè)人需要處理request了,我這里是,打開一個(gè)文件夾選擇對(duì)話框然后選擇一個(gè)路徑。
-
返 回值。事件處理函數(shù)是沒有返回值的,但是flash player提供了一個(gè)方法:m_FlashPlayer.SetReturnValue(re_value.c_str());,專門傳遞返回值,格式 是<string>%s</string>(也可以是別的AS結(jié)構(gòu),具體看幫助)。
需要提醒的是,在處理期間要block掉Flex,ExternalInterface.call是有返回值的,如果不阻塞Flex,可能返回就是NULL,呵呵,不知道深層原因。另外,反過來調(diào)用格式也是一樣的。
調(diào)試環(huán)境:win xp, VC++6.0, Flex builder 2.0
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。