随笔 - 224  文章 - 41  trackbacks - 0
<2010年3月>
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

享受编程

常用链接

留言簿(11)

随笔分类(159)

随笔档案(224)

文章分类(2)

文章档案(4)

经典c++博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜

原文地址:http://www.cnblogs.com/jason-jiang/archive/2006/11/03/549244.html
symbian基本类总结

类总结:
四大天王:CaknApplication,CeikDocument,CAknAppUi,CAknView

void CAknAppUi::DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane )

在显示menu pane之前调用,主要是用来初始化菜单显示的具体项目。

aResourceId 是资源的具体ID,如R_SMS_MENU。

aMenuPane 通过调用aMenuPane->SetItemDimmed(菜单项目资源ID,EFalse);来显示或隐藏该菜单选项。注意:Etrue为隐藏。

1、话框类:CEikDialog   (OK/CANCEL)
主要成员函数有:

void PreLayoutDynInitL();//处理在对话框出现之前的初始化动作

TBool OkToExitL( TInt aButtonId );//对OK按的处理

Void HandleControlStateChangL(Tint aControlId);//监听对话框上控件改动,有点类似与Appui类的void CAknAppUi::HandleCommandL(TInt aCommand)。

//构造方式:

    CMmssSendDialog* iSendDialog = new ( ELeave ) CMmssSendDialog;

       iSendDialog->SetMopParent( this );

    iSendDialog->ExecuteLD( R_MMSSEND_DIALOG );

//-------------------------------定义一个对话框资源---------------------------



RESOURCE DIALOG r_mmssend_dialog

    {

    flags =   EEikDialogFlagNoDrag |    // 无法拖曳

                       EEikDialogFlagNoTitleBar |  //无标题栏

                       EEikDialogFlagFillAppClientRect | //将应用程序客户区填满

                       EEikDialogFlagCbaButtons |  //使用CBA按钮

EEikDialogFlagModeless;   //不接受按钮事件

//以上可以参见SDK  :Developer Library ? API Reference ? C++ API reference ? UIKLAFGT

    buttons = R_AVKON_SOFTKEYS_OPTIONS_EXIT;

    form = r_mmssend_form;

    }



// ---------------------------------------------------------

//默认的单行显示模式

  
// ---------------------------------------------------------

//可以设置为double行显示


RESOURCE FORM r_mmssend_form

    {

    flags = EEikFormEditModeOnly |

EEikFormUseDoubleSpacedFormat;

//Specify a style of form optionally. The default setting is single line display.
//1、EEikFormUseDoubleSpacedFormat : Double line display.
//2、EEikFormHideEmptyFields : To make empty data fields Invisible.
//3、EEikFormShowBitmaps : To display a bitmap on a label.
//4、EEikFormEditModeOnly : To display the form in edit mode only.

    items =

        {

        DLG_LINE

            {

type = EEikCtEdwin;    //是一个编辑文本框 Editor window

//实际上这个是枚举类型,可参看SDK:

//Developer Library ? API Reference ? C++ API reference ? UIKLAFGT ? UIKLAFGT Resource Constants ? TEikStockControls

    prompt = qtn_mmssend_recipient_prompt;// 这个控件的label显示的字符串

            id = EMmsRecipientEditor;

            control = EDWIN

                {

                flags = EEikEdwinNoHorizScrolling | EEikEdwinResizable;

                width = qtn_mmssend_recipient_width;

                maxlength = qtn_mmssend_recipient_maxlenght;

                default_input_mode = EAknEditorNumericInputMode;//数字输入模式

                };
},


           DLG_LINE

            {

            type = EEikCtEdwin;

               prompt = qtn_mmssend_subject_prompt;

            id = EMmsSubjectEditor;

            control = EDWIN

                {

                flags = EEikEdwinNoHorizScrolling | EEikEdwinResizable;

                width = qtn_mmssend_subject_width;

                maxlength = qtn_mmssend_subject_maxlenght;

                default_input_mode = EAknEditorTextInputMode;//文本输入模式

                };
            }
           };

    }

2、周期类:
1、Cperiodic
==================================================================

CPeriodic* iPeriodicTimer;

iPeriodicTimer = CPeriodic::NewL( CActive::EPriorityStandard );//这条语句一般在ConstructL()中

void CGraphicsAppView::StartTimer()//开始启动时钟

    {

    if ( !iPeriodicTimer->IsActive() )

        {iPeriodicTimer->Start( 1, 1,

            TCallBack( CGraphicsAppView::Period, this ) );//TcallBack是一个方法回调函数,从使用来看,他只能回调类中的静态方法。

        }

    }

TInt CGraphicsAppView::Period( TAny* aPtr )//周期启动函数,注意,这是个静态函数,但static只在头文件中才做了申明。

    {

    ( static_cast<CGraphicsAppView*>( aPtr ) )->DoPeriodTask();

    return ETrue;

    }

void CGraphicsAppView::DoPeriodTask()//周期真正在做的事情

    {

    // Update the screen

    CWindowGc& gc = SystemGc();

    gc.Activate( *DrawableWindow() );//如果要求清屏操作。增加gc.Clear();

    UpdateDisplay();///////////////////这个函数是周期需要实现的东西

    gc.Deactivate();

    }

void CGraphicsAppView::StopTiem()//停止时钟

    {

    if ( iPeriodicTimer->IsActive() )

        {

        iPeriodicTimer->Cancel();

        }

    }

2、Rtimer
        RTimer timer;

        TRequestStatus timerStatus;  // ... its associated request status

   timer.CreateLocal();         // Always created for this thread.

for (TInt i=0; i<10; i++)

        {    // issue and wait for single request

        timer.After(timerStatus,1000000);  // 设定时钟请求为1秒

        User::WaitForRequest(timerStatus); // 等待这个请求

             // display the tick count

        _LIT(KFormat3,"Tick %d\n");

        console->Printf(KFormat3, i);

        }

3、Ttime
  TTime time; // time in microseconds since 0AD nominal Gregorian

   _LIT(KTxt2,"The time now is, ");

   console->Printf(KTxt2);

   time.HomeTime(); //设置时间为当前系统时间

   showTime(time);//显示当前时间

//----------------以下代码是人为给时间加10秒--------------

TTimeIntervalSeconds timeIntervalSeconds(10);

   time += timeIntervalSeconds;

   showTime(time); // print the time the request should complete

//---------------------------------------------------------

timer.At(timerStatus,time); //设定时钟请求为10秒

User::WaitForRequest(timerStatus); //等待这个请求

        // say it's over, and set and print the time again

   _LIT(KTxt4,"Your 10 seconds are up\nThe time now is, ");

   console->Printf(KTxt4);

   time.HomeTime(); // set time to now

   showTime(time); // print the time

        // close timer

   timer.Close(); // close timer

              

3、字符串类:
TDesC是所有字符类的祖先


标准C语言
Symbian OS

让一个字符串进入2进制代码
Static char hellorom[]=”hello”
_LIT(khellorom,”hello”)

在栈中获得字符串的指针
Const char* helloptr=hellorom
TPtrC helloptr=khellorom

获得在栈中字符串的指针
Char hellostack[sizeof(hellorom)];

Strcpy(hellostack,hellorom);
TBufC<5> hellostack=khellorom;

获得在堆中字符串的指针
Char* helloheap=

(char *)malloc(sizeof(hellorom));

strcpy(helloheap,hellorom);
HBufC* helloheap=

Khellorom.AllocLC();


a)TPtrC相当于不变的字符串常量.

b)TPtr相当与String类型。Tbuf相当于char[]。前者与后者的唯一区别是,后者需要指定分配的栈空间大小。

C)HBufC* 与char*类似。分配的是堆上的空间。

HBufC* textResource;

//两种字符串附值方法

textResource = StringLoader::LoadLC( R_HEWP_TIME_FORMAT_ERROR );

textResource =iEikonEnv->AllocReadResourceL(R_EXAMPLE_TEXT_HELLO);

TBuf<32> timeAsText;

timeAsText = *textResource;

/* 数据类型转换*/


TBuf  转换为 TPtrC16
    TBuf<32> tText(_L("2004/11/05 05:44:00"));
    TPtrC16 tPtrSecond=tText.Mid(17,2);

TPtrC16 转换为 TBufC16
    TPtrC16 tPtrSecond=tText.Mid(17,2);
    TBufC16<10> bufcs(tPtrSecond);

TBufC16 转换为  TPtr16
    TBufC16<10> bufcs(tPtrSecond);
    TPtr16 f=bufcs.Des();

TPtr16 转换为 TBuf
    TBuf<10> bufSecond;
    bufSecond.Copy(f);

TBuf 转换为 TPtr16
    TBuf<10> bufSecond(_L("abc"));
    TPtr16 f;
    f.Copy(bufSecond);

TBuf 转换为 TInt
    TInt aSecond;
    TLex iLexS(bufSecond);
    iLexS.Val(aSecond);

TInt 转换为 TBuf
    TBuf<32> tbuf;
    TInt i=200;
    tbuf.Num(i);

1.串转换成数字
   TBuf16<20> buf(_L( "123" ) );
    TLex lex( buf );
    TInt iNum;
    lex.Val( iNum );
2.数字转换成串
   TBuf16<20> buf;
   TInt iNum = 20;
   buf.Format( _L( "%d" ) , iNum  );
3.将symbian串转换成char串
    char* p = NULL;
    TBuf8<20> buf( _L( "aaaaa" ) );
    p = (char *)buf.Ptr();

4.UTF-8转换成UNICODE
    CnvUtfConverter::ConvertToUnicodeFromUtf8( iBuf16 , iBuf8 );
5.UNICODE转换成UTF-8
    CnvUtfConverter::ConvertFromUnicodeToUtf8( iBuf8 , iBuf16 );


6.将char串转换成symbian串
    char* cc = "aaaa";
    TPtrC8 a;
    a.Set( (const TUint8*)cc , strlen(cc) );

7、将TPtrc8与TPtrc16之间转化

// Get a iBuf8 from a iBuf16 (data are not modified)
TPtrC8 ptr8(reinterpret_cast<const TUint8*>(iBuf16.Ptr()),(iBuf16.Size()*2));
iBuf8=ptr8;

// Get a iBuf16 from a iBuf8 (data are not modified)
TPtrC16 ptr16(reinterpret_cast<const TUint16*>(iBuf8.Ptr()),(iBuf8.Size()/2));
iBuf16=ptr16;


The second one takes each character and convert it to the other format. The 16-bit to 8-bit conversion may not always succeed in this case:

Code:

// Get a iBuf8 from a iBuf16 (data are modified)
CnvUtfConverter::ConvertFromUnicodeToUtf8(iBuf8,iBuf16);

// Get a iBuf16 from a iBuf8 (data are modified)
CnvUtfConverter::ConvertToUnicodeFromUtf8(iBuf16,iBuf8);


This second method requires to include the utf.h header and to link against charconv.lib.

/*memset   memcpy   strcpy */

memset主要应用是初始化某个内存空间。用来对一段内存空间全部设置为某个字符。
memcpy是用于COPY源空间的数据到目的空间中,用来做内存拷贝可以拿它拷贝任何数据类型的对象。
strcpy只能拷贝字符串了,它遇到'\0'就结束拷贝。



strcpy
原型:extern char *strcpy(char *dest,char *src);
用法:#include <string.h>
功能:把src所指由NULL结束的字符串复制到dest所指的数组中。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
       返回指向dest的指针。
      
memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指内存区域复制count个字节到dest所指内存区域。
说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。

memset
原型:extern void *memset(void *buffer, int c, int count);
用法:#include <string.h>
功能:把buffer所指内存区域的前count个字节设置成字符c。
说明:返回指向buffer的指针。








4、文件类和流操作
Location: s32file.h

文件模拟路径在C:\Symbian\8.0a\epoc32\wins下面。有C、D两个分区。

RFs fs;
User::LeaveIfError(fs.Connect());
RFile file
User::LeaveIfError(file.Open(fs, _L("C:\\file.foo"), EFileWrite));
TBuf8<256> buf;
file.Read(buf, 256);
file.Seek(ESeekStart, 911);
file.Write(_L8("Some thing you wanna write..."));
file.Close();
1)      与文件服务器建立通信:

RFs fsSession;

TInt fsret = fsSession.Connect(); // start a file session

       if (fsret != KErrNone)

              {console->Printf(KTxtConnectFailed,fsret);

              User::Leave(fsret);

              }

  2)确定文件路径存在

fsSession.MkDirAll(KFullNameOfFileStore); // make sure directory exists

      



  3)建立文件存储

TParse    filestorename;// The class uses the full filename structure supported by Symbian

       fsSession.Parse(aName,filestorename);

/*------------------------------------------------------------------------------------------------

TDesC& aName。可以通过以下方式给aNAME赋值:
    _LIT(aName,"C:\\epoc32ex\\data\\SimpleClassToSimpleStream.dat");

----------------------------------------------------------------------------------------------*/

                            // construct file store object - the file to contain the

                            // the store replaces any existing file of the same name.

       CFileStore*     //如果EFileRead为读出流

store = CDirectFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);

store->SetTypeL(KDirectFileStoreLayoutUid); // 设定存储种类



4)将外部数据写入流::(记忆方式:>>指向就是数据流向)//假设:TSimple anXxx;

RStoreWriteStream outstream;

       TStreamId id = outstream.CreateLC(*store);

//----------------------------将标量写入数据流------------------

outstream<< anXxx;

    或者 aStream.WriteInt8L(anXxx);

    实际上这里使用了流的扩展化:(当输出不是普通的元数据时,使用这个扩展化)这是一个虚函数的重载

void TSimple::ExternalizeL(RWriteStream& aStream) const

       {

       aStream << iTheEnum;

       aStream << iBuffer;

       aStream.WriteInt32L(iIntValue);

       aStream.WriteUint32L(iUintValue);

       aStream.WriteReal64L(iRealValue);

       }  

//------------------------------------------------------------------------------------------------

     // 以下是将流改动提交到文件服务器。

       outstream.CommitL();

5)将流读到外部数据:

       RStoreReadStream instream;

store->SetRootL(id);//可以将上面的已经存在的流作为流的根。好处是不必再创建流ID。实际上也就节省了内存。

// Commit changes to the store

       store->CommitL();

// Construct and open the input stream object. We want to access the root stream from the store in this example.

       instream.OpenLC(*store,store->Root());

TSimple thesimple;

       instream >> thesimple;//写入类对象数据。

    //---------------------------------------------------------------------

void TSimple::InternalizeL(RReadStream& aStream)

       {

       aStream >> iTheEnum;

       aStream >> iBuffer;

      iIntValue  = aStream.ReadInt32L();

       iUintValue = aStream.ReadUint32L();

       iRealValue = aStream.ReadReal64L();

       }  

    //------------------------------输出流到其他数据元或类对象中----------------------------

     anXxx = TXxx(aStream.ReadInt8L());

  

6)关闭文件服务通信

fsSession.Close()

    

5. 活动调度表
由于使用多线程来处理异步请求比较消耗系统资源,所以Symbian 使用了活动对象(Active Object)来解决异步请求的问题。
         活动规划器(active scheduler)用于处理由活动对象提出的异步请求。它检测活动对象提出的异步请求,并安排活动对象的请求完成事件的执行顺序。活动规划器仅用一个事件处理线程来规划各个活动对象提出的事件请求,所以它要比多线程实现异步请求占用更少的资源。

1、   首先应该创建一个活动规划器对象,并把它安装到当前线程

       CActiveScheduler* scheduler = new(ELeave) CActiveScheduler();//创建一个活动规划器
   CleanupStack::PushL(scheduler);
   CActiveScheduler::Install(scheduler);// 安装活动规划器。
   TRAPD(error,doInstanceL());   //具体安排的函数处理。

在具体的安排函数中一定要启动这个规划器

CActiveScheduler::Start();//这句话告诉活动规划器该等待对象的状态的改变

2、 把自己加入活动规划器:一般这是一个类。可以在类的构造函数中申明下面代码。
                      CActiveScheduler::Add(this);

     //该类必须有一个继承来自public CActive, public MmsvSessionObserver

//在构造函数时,也可以宣布优先级别:TclassA::classA()  : CActive(0)    

3、返回改变事实:              

SetActive();  / / CActive类对象提交异步请求。

//这个请求说明对象的改变完成。就会触发CActive::RunL()

      4、这里的CActiveScheduler只管理了一个CActive对象,就是timeCount,可以用类似的方法实现多个CActive,并且都加入CActiveScheduler,CActiveScheduler将会等待所有加入它的CActive的状态的改变,其中有一个的状态改变就会去执行对应的活动对象的处理函数,当状态同时发生的时候,会通过对象的优先级来决定先调用谁的RunL函数.CActiveScheduler也是非抢占式的,当一个RunL函数还没有执行完的时候,如果另一个CActive的状态改变,会等待RunL执行完以后再执行另一个CActive的处理函数.



6、线程:
1、  创建一个等待的线程:

     TInt res=KErrNone;

     // create server - if one of this name does not already exist

     TFindServer findCountServer(KCountServerName);

     TFullName name;

     if (findCountServer.Next(name)!=KErrNone) // we don't exist already

         {

RThread thread;

         RSemaphore semaphore;

         semaphore.CreateLocal(0); //创建一个信号量,等待线程的正常结束

         res=thread.Create(KCountServerName,   // create new server thread

              CCountServServer::ThreadFunction, // 线程启动的主函数

              KDefaultStackSize,

              KDefaultHeapSize,

              KDefaultHeapSize,

              &semaphore // 最后是主函数的需要的参数passed as TAny* argument to thread function

              );

         if (res==KErrNone) // thread created ok - now start it going

              {

              thread.SetPriority(EPriorityNormal);

              thread.Resume(); // start it going

              semaphore.Wait(); // wait until it's initialized

              thread.Close(); // we're no longer interested in the other thread

              }

         else // thread not created ok

              {

              thread.Close(); // therefore we've no further interest in it

              }

         semaphore.Close();

         }
posted on 2010-03-29 13:23 漂漂 阅读(530) 评论(0)  编辑 收藏 引用 所属分类: symbian开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理