Symbian开发笔记(一)

CTest_1AppUi::HandleCommandL(TInt aCommand)
    {
    switch ( aCommand )
        {
        case EAknSoftkeyBack:
        case EEikCmdExit:
            {

                          _LIT(KQueryDelete,"Are you sure to delete it?");
                          CAknQueryDialog* dlg = new(ELeave) CAknQueryDialog(const_cast<TDesC&>(KQueryDelete()),CAknQueryDialog::EWarningTone);
                          dlg->ExecuteLD(R_DIALOG_DELETE_QUERY);
                          break;
            }
        case ETest_1CmdAppTest:
            {
                          // Info message shown only in the emulator
                          iEikonEnv->InfoMsg(_L("test"));
   
                          // Load localized message from the resource file
                          HBufC* message = CEikonEnv::Static()->AllocReadResourceLC(R_MESSAGE_TEXT);
                         

                          // Show information note dialog
                         CAknInformationNote* note = new (ELeave) CAknInformationNote;


                         note->ExecuteLD(message->Des());
                         CleanupStack::PopAndDestroy(message);
                         break;
            }
        // TODO: Add Your command handling code here

        default:
            break;     
        } 

这里面有几个知识点:

1:_LIT()宏所定义的常量并不是描述符,但可以通过使用一个重载的运算符()将其转换为const TDesC&类型的描述符。

2:CAknQueryDialog的第一个参数需要一个TDesC&类型的参数,所以我们需要把KQueryDelete()强制转换成非const类型的,这需要使用C++中的强制类型转换const_cast<TDesC&>。

3:千万注意要把const_cast<TDesC&>所接的数据加上()。

void CTest_1AppUi::HandleCommandL(TInt aCommand)
    {
    switch ( aCommand )
        {
        case EAknSoftkeyBack:
        case EEikCmdExit:
            {
                  _LIT(KQueryDelete,"Are you sure to delete it?");
                  CAknQueryDialog* dlg = new(ELeave) CAknQueryDialog(const_cast<TDesC&>(KQueryDelete()),CAknQueryDialog::EWarningTone);


                  TInt temp = dlg->ExecuteLD(R_DIALOG_DELETE_QUERY);


                  _LIT(KReturnValue,"the return value is :");

                  HBufC* iBuf = HBufC::NewLC(100);
                  iBuf->Des().Copy(KReturnValue);
                  iBuf->Des().AppendNum(temp);
                  CEikonEnv::Static()->InfoMsg(*iBuf);
                  CleanupStack::PopAndDestroy(iBuf);
                  break;
            }

        default:
            break;     
        }
    }

           从程序执行中我们可以看到,当我们点dlg的NO按钮时,temp的值为一个正数(程序中是3005),而当我们点dlg 的OK的时候,temp的值为0。

           所以我们在很多程序中可以看到类似下面的代码:

if(dlg->ExecuteLD(R_DIALOG_DELETE_QUERY))
    {
        TInt index = iListBox->CurrentItemIndex();
        TName name(db.iName);
        iUi->iDB->iArrayDB->Delete(index);
        iUi->iDB->SaveDBL();
        CEikonEnv::Static()->ReadResource(info, R_STR_DELETE_TIPS);
        tips.Format(info, &name);
        ShowNotify(tips);
        if(iUi->iDB->iArrayDB->Count()==0)
        {
            CEikonEnv::Static()->ReadResource(info, R_STR_NO_CD_TIPS);
            ShowNotify(info);
            CreateFunctionListBoxL();
        }
        else
        {
            CreateRecordListBoxL();
        }
    }

也就是判断我们是否点击了Dialog的OK按钮。



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1554698


你可能感兴趣的:(Symbian)