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按钮。

 

 

RESOURCE LOCALISABLE_APP_INFO r_httpenginetest_localisable_app_info
{
 short_caption = "HttpEngine";
 caption_and_icon =
 CAPTION_AND_ICON_INFO
     {
     caption = "HttpEngineTest";
     number_of_icons = 2;
     icon_file = "//resource//apps//HttpEngineTestaif.mbm";
     };
}

RESOURCE RSS_SIGNATURE {}

RESOURCE TBUF { buf=""; }

RESOURCE EIK_APP_INFO
{
    menubar = r_httpenginetest_menubar;
    cba = R_AVKON_SOFTKEYS_OPTIONS_BACK;
}

 

这样写会报告“系统错误”并看不到应用程序的icon,看来签名、文档名缓冲还是要放前面,如下:

RESOURCE RSS_SIGNATURE {}

RESOURCE TBUF { buf=""; }

RESOURCE LOCALISABLE_APP_INFO r_httpenginetest_localisable_app_info
{
 short_caption = "HttpEngine";
 caption_and_icon =
 CAPTION_AND_ICON_INFO
     {
     caption = "HttpEngineTest";
     number_of_icons = 2;
     icon_file = "//resource//apps//HttpEngineTestaif.mbm";
     };
}

RESOURCE EIK_APP_INFO
{
    menubar = r_httpenginetest_menubar;
    cba = R_AVKON_SOFTKEYS_OPTIONS_BACK;
}

其实上面这样改,在程序运行到BaseConstructL(EAknEnableSkin)时会报告“User 23”系统错误,原因是我们把LOCALISABLE_APP_INFO放到了EIK_APP_INFO前面去了,应该像下面这样改:

 

NAME MHWA

#include <eikon.rh>
#include <avkon.rsg>
#include <avkon.rh>
#include <avkon.mbg>
#include <appinfo.rh>

RESOURCE RSS_SIGNATURE {}

RESOURCE TBUF r_default_document_name
    {
    buf="";
    }

RESOURCE EIK_APP_INFO
{
    menubar = r_httpenginetest_menubar;
    cba = R_AVKON_SOFTKEYS_OPTIONS_BACK;
}

RESOURCE LOCALISABLE_APP_INFO r_httpenginetest_localisable_app_info
{
 short_caption = "HttpEngine";
 caption_and_icon =
 CAPTION_AND_ICON_INFO
     {
     caption = "HttpEngineTest";
     number_of_icons = 2;
     icon_file = "//resource//apps//HttpEngineTestaif.mbm";
     };
}

你可能感兴趣的:(Symbian开发笔记(一))