wxWidgets调用指定程序打开文件代码

    由于利用系统的ShellExecute调用大大简化了代码,故在UI.cpp中删除这段代码,这里将其备份起来,以备以后之用。

////////////////////////////////////////////////////////////////////////// // 执行指定的指令 void CMainFrame::DoAsyncExec(const wxString& cmd) { wxProcess *process = new wxProcess( this ); long m_pidLast = wxExecute(cmd, wxEXEC_ASYNC, process); if ( !m_pidLast ) { wxLogError( _T("Execution of '%s' failed."), cmd.c_str() ); delete process; } else { wxLogStatus( _T("Process %ld (%s) launched."), m_pidLast, cmd.c_str() ); } } ////////////////////////////////////////////////////////////////////////// // 打开文件并执行之 bool CMainFrame::FileExec( wxString filename ) { static wxString s_filename; if ( filename.empty() ) return false; s_filename = filename; wxString ext = filename.AfterLast(_T('.')); wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); if ( !ft ) { wxLogError(_T("Impossible to determine the file type for extension '%s'"), ext.c_str()); return false; } wxString cmd; bool ok = ft->GetOpenCommand(&cmd, wxFileType::MessageParameters(filename)); delete ft; if ( !ok ) { wxLogError(_T("Impossible to find out how to open files of extension '%s'"), ext.c_str()); return false; } DoAsyncExec(cmd); return true; } ////////////////////////////////////////////////////////////////////////// // 打开文件夹或者查看文件 void CMainFrame::OnOpenF(wxCommandEvent& evt) { cur_treeid = m_treeCtrl->GetCurTreeId();// 取得当前树中激活的id号码,将其打开之 if ( !cur_treeid ) {//当前为空,则设置在根下添加文件夹 cur_treeid = rootid; } CTreeItemDataMgr *item = (CTreeItemDataMgr *)m_treeCtrl->GetItemData(cur_treeid);// 获得激活结点绑定的数据 if ( item ) { if (item->GetNodeData().filetype == 1) { // 说明打开的是文件夹 //FileTreeItem fti = item->GetNodeData(); //m_curid = m_pkg.GetTree()->GetNode(fti);// 获取当前结点的指针 //// 获取相对路径 //wxString spath = GetRelPath(cur_treeid, name); //char ansi_spath[MAX_PATH]; //strcpy_s( ansi_spath, MAX_PATH, spath.mb_str() ); //char ansi_sname[MAX_PATH]; //strcpy_s( ansi_sname, MAX_PATH,name.mb_str() ); //char ansi_path[MAX_PATH]; //strcpy_s( ansi_path, MAX_PATH, path.mb_str() ); //m_pkg.AddFolder( ansi_string, ansi_spath, m_curid ); //wxMessageBox(path); //CloseCurFile(); //OpenPKGFile( m_pkg_path ); wxMessageBox(wxT("打开文件夹!")); } else {// 说明打开的是文件 TCHAR TempDir[MAX_PATH]; if ( 0 == GetEnvironmentVariable(_T("TEMP"), TempDir, MAX_PATH) ) { wxMessageBox(wxT("临时文件创建失败!")); return; } char *temproot = m_pkg.wctoc( TempDir ); char tempfullroot[MAX_PATH];// 构造全路径 strcpy_s( tempfullroot, MAX_PATH, temproot ); if ( '//' != tempfullroot[strlen( tempfullroot ) - 1] ) { strcat_s( tempfullroot, MAX_PATH, "//" ); } char tmpfile[MAX_PATH]; strcpy_s( tmpfile, MAX_PATH, item->GetNodeData().FilePath ); m_pkg.ReleaseFile( tmpfile, temproot ); char tmpfilename[MAX_PATH]; strcpy_s( tmpfilename, MAX_PATH, item->GetNodeData().FileName ); strcat_s( tempfullroot, MAX_PATH, tmpfilename );// 连成绝对路径 TCHAR* tc_string = m_pkg.ctowc(tempfullroot); wxString wx_string(tc_string,wxConvUTF8);// 编码转化 FileExec( wx_string ); delete []temproot; delete []tc_string; //bool bRet = wxRemoveFile( wx_string ); } } else { wxMessageBox(wxT("请选择要打开的文件或者文件夹!")); } }

你可能感兴趣的:(wxWidgets调用指定程序打开文件代码)