This page was last modified 06:51, 9 July 2008.
CS001059 - 使用CAknGlobalMsgQuery显示一个全局消息查询框
From Forum Nokia Wiki
| ID | CS001059 | Creation date | July 3, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N93 |
| Category | Symbian C++ | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): CAknGlobalMsgQuery, StringLoader, CAknGlobalMsgQuery::NewL(),
CAknGlobalMsgQuery::ShowMsgQueryL(), StringLoader::LoadLC() |
概述
这个代码片段演示如何创建一个全局的消息查询框CAknGlobalMsgQuery。通过调用NewL()方法来构造一个全局查询框。需要构造一个活动对象以便处理对话框的关闭。此例使用通用类CGlobalQueryHandlerAO,这个类也用在其它全局查询代码片段中。类CGlobalQueryContainer用于调出全局查询框,它也是观察者,实现了接口MGlobalQueryObserver。
此片段能用自签名。
MMP文件
下面的库文件是必须的:
LIBRARY cone.lib //CEikonEnv, CCoeEnv LIBRARY avkon.lib //Avkon resources LIBRARY CommonEngine.lib //StringLoader LIBRARY euser.lib //CActive, CleanupStack
资源文件
.rss
#include <eikon.rh> #include <avkon.rsg> #include <avkon.rh> //... RESOURCE TBUF r_global_query_header_text { buf = "<header text here>"; } RESOURCE TBUF r_global_query_message_text { buf = "<message text here>"; }
头文件
- 观察者接口
#ifndef GLOBALQUERYOBSERVER_H #define GLOBALQUERYOBSERVER_H class MGlobalQueryObserver { public: // 保持这些方法短暂运行,因为在活动对象内调用。 virtual void ProcessOkOptionL() = 0; virtual void ProcessCancelOptionL() = 0; virtual void ProcessDoneOptionL() = 0; virtual void ProcessSelectedItemL(const TInt aItem) = 0; }; #endif // GLOBALQUERYOBSERVER_H
- 处理器——活动对象
#ifndef GLOBALQUERYHANDLERAO_H #define GLOBALQUERYHANDLERAO_H #include <e32base.h> //CActive #include "GlobalQueryObserver.h" class CGlobalQueryHandlerAO : public CActive { public: ~CGlobalQueryHandlerAO(); static CGlobalQueryHandlerAO* NewL(MGlobalQueryObserver& aObserver); static CGlobalQueryHandlerAO* NewLC(MGlobalQueryObserver& aObserver); public: void StartHandler(); private: CGlobalQueryHandlerAO(MGlobalQueryObserver& aObserver) : CActive (EPriorityStandard), iObserver(aObserver){}; void ConstructL(); private: // from CActive void RunL(); void DoCancel(); private: // data MGlobalQueryObserver& iObserver; }; #endif //GLOBALQUERYHANDLERAO_H
- 容器/观察者
#ifndef GLOBALQUERYCONTAINER_H #define GLOBALQUERYCONTAINER_H #include <coecntrl.h> // CCoeControl #include "GlobalQueryObserver.h" #include <aknglobalmsgquery.h> //CAknGlobalMsgQuery class CGlobalQueryHandlerAO; class CGlobalQueryContainer : public CCoeControl, MGlobalQueryObserver { public: //... void MakeExampleQueryL(); public: // from MGlobalQueryObserver void ProcessOkOptionL(); void ProcessCancelOptionL(); void ProcessDoneOptionL(); //not used void ProcessSelectedItemL(const TInt aItem); //not used //... private: CGlobalQueryHandlerAO* iQueryHandlerAO; CAknGlobalMsgQuery* iGlobalMsgQuery; }; #endif // GLOBALQUERYCONTAINER_H
源代码文件
- 处理器——活动对象
#include "GlobalQueryHandlerAO.h" #include <avkon.hrh> //EAknSoftkeyOk, EAknSoftkeyCancel, EAknSoftkeyDone CGlobalQueryHandlerAO::~CGlobalQueryHandlerAO() { Cancel(); } void CGlobalQueryHandlerAO::ConstructL() { CActiveScheduler::Add(this); } CGlobalQueryHandlerAO* CGlobalQueryHandlerAO::NewLC(MGlobalQueryObserver& aObserver) { CGlobalQueryHandlerAO* self = new (ELeave) CGlobalQueryHandlerAO(aObserver); CleanupStack::PushL(self); self->ConstructL(); return self; } CGlobalQueryHandlerAO* CGlobalQueryHandlerAO::NewL(MGlobalQueryObserver& aObserver) { CGlobalQueryHandlerAO* self = CGlobalQueryHandlerAO::NewLC(aObserver); CleanupStack::Pop(self); return self; } void CGlobalQueryHandlerAO::DoCancel(){} void CGlobalQueryHandlerAO::StartHandler() { SetActive(); } void CGlobalQueryHandlerAO::RunL() { if (iStatus == EAknSoftkeyOk) iObserver.ProcessOkOptionL(); else if(iStatus == EAknSoftkeyCancel) iObserver.ProcessCancelOptionL(); else if(iStatus == EAknSoftkeyDone) iObserver.ProcessDoneOptionL(); else iObserver.ProcessSelectedItemL(iStatus.Int()); Cancel(); }
- 容器/观察者
#include "GlobalQueryContainer.h" //CGlobalQueryContainer #include "GlobalQueryHandlerAO.h" //CGlobalQueryHandlerAO #include <GlobalQuery_0xED0C36BE.rsg> //Resources #include <StringLoader.h> // StringLoader void CGlobalQueryContainer::ConstructL(const TRect& aRect) { CreateWindowL(); SetRect(aRect); ActivateL(); MakeExampleQueryL(); } void CGlobalQueryContainer::MakeExampleQueryL() { //start the query handler and show global message query iQueryHandlerAO = CGlobalQueryHandlerAO::NewL(*this); iQueryHandlerAO->StartHandler(); iGlobalMsgQuery = CAknGlobalMsgQuery::NewL(); HBufC* headerText = StringLoader::LoadLC(R_GLOBAL_QUERY_HEADER_TEXT); HBufC* messageText = StringLoader::LoadLC(R_GLOBAL_QUERY_MESSAGE_TEXT); //show the global message query iGlobalMsgQuery->ShowMsgQueryL(iQueryHandlerAO->iStatus, *messageText, R_AVKON_SOFTKEYS_OK_CANCEL, *headerText, KNullDesC); CleanupStack::PopAndDestroy(2); //messageText, headerText } CGlobalQueryContainer::~CGlobalQueryContainer() { //... delete iQueryHandlerAO; delete iGlobalMsgQuery; } void CGlobalQueryContainer::ProcessOkOptionL() { //OK pressed, do something iEikonEnv->InfoWinL(_L("OK"),_L("Pressed")); } void CGlobalQueryContainer::ProcessCancelOptionL() { //Cancel pressed, do something... iEikonEnv->InfoWinL(_L("Cancel"),_L("Pressed")); } void CGlobalQueryContainer::ProcessDoneOptionL() { //not used } void CGlobalQueryContainer::ProcessSelectedItemL(const TInt /*aItem*/) { //not used }
后置条件
本例代码演示如何创建全局消息查询框、处理查询框的关闭、以及活动对象处理器如何通知观察者类对话框已关闭。

