使用CDocumentHandler打开文件

简介: 概述   这个代码片段演示如何使用Document Handler API(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()方法。另外,下面的代码演示若已经在处理器中打开则如何更新文件。 

#include <DocumentHandler.h> 
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 
#include <DocumentHandler.h> 
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); 

    private:  // Private functions 
        void LaunchFileEmbeddedL(const TDesC& aFilename); 

    private:  // Data 
        CDocumentHandler* iDocHandler; 
    }; 
源代码文件(嵌入版) 
以嵌入方式调出文件,使用CDocumentHandler::OpenFileEmbeddedL()方法: 

#include <aknserverapp.h>  // MAknServerAppExitObserver 
#include <DocumentHandler.h> 
void CMyAppUi::ConstructL() 
    { 
    // Create the document handler 
    iDocHandler = CDocumentHandler::NewL(CEikonEnv::Static()->Process()); 

    // ... 
    } 
void CMyAppUi::LaunchFileEmbeddedL(const TDesC& aFilename) 
    { 
    //Set the exit observer so HandleServerAppExit will be called 
    iDocHandler->SetExitObserver(this);   

    TDataType emptyDataType = TDataType(); 
    //Open a file embedded 
    iDocHandler->OpenFileEmbeddedL(aFilename, emptyDataType);             
    } 

void CMyAppUi::HandleServerAppExit(TInt aReason) 
    { 
    //Handle closing the handler application 
    MAknServerAppExitObserver::HandleServerAppExit(aReason); 
    } 
后置条件 
由aFilename所指的文件用CDocumentHandler打开。
 
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
目录
相关文章
|
1月前
|
C语言
使用fopen函数打开数据文件
使用fopen函数打开数据文件
14 0
|
网络协议 测试技术 Go
打开文件和关闭文件 | 学习笔记
快速学习打开文件和关闭文件
110 0
|
开发者 Python
文件的打开方式 | 学习笔记
快速学习 文件的打开方式
116 0
|
移动开发 Unix Linux
【C 语言】文件操作 ( fopen 文件打开方式详解 )(二)
【C 语言】文件操作 ( fopen 文件打开方式详解 )(二)
177 0
 【C 语言】文件操作 ( fopen 文件打开方式详解 )(二)
|
C语言
【C 语言】文件操作 ( fopen 文件打开方式详解 )(一)
【C 语言】文件操作 ( fopen 文件打开方式详解 )(一)
395 0
【C 语言】文件操作 ( fopen 文件打开方式详解 )(一)
|
C语言
【C 语言】文件操作 ( fopen 文件打开方式详解 )(三)
【C 语言】文件操作 ( fopen 文件打开方式详解 )(三)
297 0
【C 语言】文件操作 ( fopen 文件打开方式详解 )(三)
|
Windows
|
开发者 Python Windows
打开文件|学习笔记
快速学习 打开文件
|
Python
6.1 file 读文件
#!/usr/bin/env python # -*- coding:utf-8 -*- #@Time      :2017/10/27 21:54 #@Author    :zhouyuyao #@File      :file_demo1.
589 0