对话框的练习
写一对话框的 rss 资源
RESOURCE DIALOG r_my_dlg { flags = EEikDialogFlagNoDrag | EEikDialogFlagNoTitleBar | EEikDialogFlagFillAppClientRect|EEikDialogFlagCbaButtons |EEikDialogFlagWait |EEikDialogFlagNotifyEsc; buttons = r_my_dia_button; items={ DLG_LINE { id = ESimpleDlgCtlLabel; type = EEikCtLabel; control = LABEL{ txt = "this is a simple dialog"; }; }, DLG_LINE { id = 10; type = EEikCtEdwin; control = EDWIN { width=10; lines =1 ; maxlength = 10; }; }; } { buttons = { CBA_BUTTON{ id = 6; txt = "ok"; }, CBA_BUTTON { id = 7; txt = "close"; } }; } |
调用代码
CAknDialog* dl = new(ELeave)CMyDialog;
/* dl->PrepareLC(R_MY_DLG);
CEikLabel* label = static_cast<CEikLabel*>(dl->Control(ESimpleDlgCtlLabel));
label->SetTextL(_L("hello"));
if (dl->RunLD())
*/
if (dl->ExecuteLD(R_MY_DLG))
{
iEikonEnv->InfoMsg(_L("Button ok"));
}
else
{
iEikonEnv->InfoMsg(_L("Button cancel")); } |
class CMySimpleDialog : public CAknDialog
{
protected: // Functions from base classes
void PreLayoutDynInitL();
void PostLayoutDynInitL();
TBool OkToExitL(TInt aButtonId);
};
#include "CMyDialog.h"
#include <eiklabel.h>
#include "Dlgtest.hrh"
#include <akndialog.h>
#include <eikenv.h>
CMyDialog::CMyDialog()
{
// TODO Auto-generated constructor stub
}
void CMyDialog::PreLayoutDynInitL()
{
CEikLabel* label = static_cast<CEikLabel*>(Control(ESimpleDlgCtlLabel));
label->SetTextL(_L("aaa"));
}
void CMyDialog::PostLayoutDynInitL()
{
}
TBool CMyDialog::OkToExitL(TInt aButtonId)
{
TBool ret = CAknDialog::OkToExitL(aButtonId);
CEikonEnv* env = CEikonEnv::Static();
if (aButtonId == 6)
{
env->InfoMsg(_L("ok to exit"));
}
else
{
env->InfoMsg(_L("cancel to exit"));
ret = EFalse;
}
return ret;
}
CMyDialog::~CMyDialog()
{
// TODO Auto-generated destructor stub } |
如果是自定义的对话框同时继承了 OkToExit 方法,可以通过改写这个方法的返回值来决定是不是需要关闭这个对话框,
返回 ETrue 则关闭对话框,否则继续显示对话框,注意对于等待的对话框来说,代码会停在那里,非等待的对话框来说,
代码继续执行,但对话框不消失