天天看点

使用CDocumentHandler打开文件

概述 

  这个代码片段演示如何使用Document Handler API(CDocumentHandler)来打开文件。下面演示了两种不同的技术: 

在该文件的处理应用程序中独立打开 

在调出应用程序中嵌入打开文件 

MMP文件 

下面的能力和库文件是必须的: 

CAPABILITY  SwEvent  // TApaTask::SwitchOpenFile() (仅独立版) 

LIBRARY  apgrfx.lib    // TApaTaskList, TApaTask (仅独立版) 

LIBRARY  apmime.lib    // TDataType 

LIBRARY  commonui.lib  // CDocumentHandler 

头文件(独立版) 

#include <DocumentHandler.h> 

class CMyAppUi : public CAknAppUi 

    { 

    // ... 

    private:  // Private functions 

        void LaunchFileL(const TDesC& aFilename); 

        void RefreshDocumentFileL(const TUid& aUid, const TDesC& aFileName); 

    private:  // Data 

        CDocumentHandler* iDocHandler; 

    } 

源代码文件(独立版)

欲在独立的文件处理中打开文件,使用CDocumentHandler::OpenFileL()方法。另外,下面的代码演示若已经在处理器中打开则如何更新文件。 

void CMyAppUi::ConstructL() 

    // Create the document handler 

    iDocHandler = CDocumentHandler::NewL(CEikonEnv::Static()->Process()); 

void CMyAppUi::LaunchFileL(const TDesC& aFilename) 

    TDataType emptyDataType = TDataType(); 

    // Open a file in a standalone handler application 

    iDocHandler->OpenFileL(aFilename, emptyDataType); 

    TUid handlerUid; 

    TInt err = KErrNone; 

    err = iDocHandler->HandlerAppUid(handlerUid); 

    if (!err) 

        { 

        RefreshDocumentFileL(handlerUid, aFilename); 

        } 

/** 

* 刷新独立处理器中的已打开文件。若文件尚未打开则什么也不做。 

*/ 

void CMyAppUi::RefreshDocumentFileL(const TUid& aUid, const TDesC& aFileName) 

    TApaTaskList taskList(iCoeEnv->WsSession()); 

    TApaTask task = taskList.FindApp(aUid); 

    // If the standalone handler is already running, then update the file 

    if (task.Exists()) 

        User::LeaveIfError(task.SwitchOpenFile(aFileName)); 

头文件(嵌入版) 

#include <aknserverapp.h>  // MAknServerAppExitObserver 

class CMyAppUi : public CAknAppUi, public MAknServerAppExitObserver 

    private:  // Functions from base classes 

        /** 

         * From MAknServerAppExitObserver. 

         * Handles the exit of a connected server application. 

         */ 

        void HandleServerAppExit(TInt aReason); 

        void LaunchFileEmbeddedL(const TDesC& aFilename); 

    }; 

源代码文件(嵌入版) 

以嵌入方式调出文件,使用CDocumentHandler::OpenFileEmbeddedL()方法: 

void CMyAppUi::LaunchFileEmbeddedL(const TDesC& aFilename) 

    //Set the exit observer so HandleServerAppExit will be called 

    iDocHandler->SetExitObserver(this);   

    //Open a file embedded 

    iDocHandler->OpenFileEmbeddedL(aFilename, emptyDataType);             

void CMyAppUi::HandleServerAppExit(TInt aReason) 

    //Handle closing the handler application 

    MAknServerAppExitObserver::HandleServerAppExit(aReason); 

后置条件 

由aFilename所指的文件用CDocumentHandler打开。

<a href="http://wiki.forum.nokia.com/index.php/%E4%BD%BF%E7%94%A8CDocumentHandler%E6%89%93%E5%BC%80%E6%96%87%E4%BB%B6">http://wiki.forum.nokia.com/index.php/%E4%BD%BF%E7%94%A8CDocumentHandler%E6%89%93%E5%BC%80%E6%96%87%E4%BB%B6</a>

继续阅读