This page was last modified 23:53, 13 July 2008.
使用CDocumentHandler打开文件
From Forum Nokia Wiki
| ID | CS001052 | Creation date | July 1, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N95 8GB |
| Category | Symbian C++ | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): CDocumentHandler, TDataType, MAknServerAppExitObserver,
CDocumentHandler::OpenL(), CDocumentHandler::OpenFileEmbeddedL(), CDocumentHandler::HandlerAppUid(), TApaTaskList::FindApp (), TApaTask::Exists(), TApaTask::SwitchOpenFile(), CDocumentHandler::SetExitObserver(), MAknServerAppExitObserver::HandleServerAppExit() |
CS001052 使用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打开。

