原文地址:http://www.cnblogs.com/jason-jiang/archive/2006/11/03/549244.html
symbian基本類總結(jié)
類總結(jié):
四大天王:CaknApplication,CeikDocument,CAknAppUi,CAknView
void CAknAppUi::DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane )
在顯示menu pane之前調(diào)用,主要是用來(lái)初始化菜單顯示的具體項(xiàng)目。
aResourceId 是資源的具體ID,如R_SMS_MENU。
aMenuPane 通過(guò)調(diào)用aMenuPane->SetItemDimmed(菜單項(xiàng)目資源ID,EFalse);來(lái)顯示或隱藏該菜單選項(xiàng)。注意:Etrue為隱藏。
1、話框類:CEikDialog (OK/CANCEL)
主要成員函數(shù)有:
void PreLayoutDynInitL();//處理在對(duì)話框出現(xiàn)之前的初始化動(dòng)作
TBool OkToExitL( TInt aButtonId );//對(duì)OK按的處理
Void HandleControlStateChangL(Tint aControlId);//監(jiān)聽(tīng)對(duì)話框上控件改動(dòng),有點(diǎn)類似與Appui類的void CAknAppUi::HandleCommandL(TInt aCommand)。
//構(gòu)造方式:
CMmssSendDialog* iSendDialog = new ( ELeave ) CMmssSendDialog;
iSendDialog->SetMopParent( this );
iSendDialog->ExecuteLD( R_MMSSEND_DIALOG );
//-------------------------------定義一個(gè)對(duì)話框資源---------------------------
RESOURCE DIALOG r_mmssend_dialog
{
flags = EEikDialogFlagNoDrag | // 無(wú)法拖曳
EEikDialogFlagNoTitleBar | //無(wú)標(biāo)題欄
EEikDialogFlagFillAppClientRect | //將應(yīng)用程序客戶區(qū)填滿
EEikDialogFlagCbaButtons | //使用CBA按鈕
EEikDialogFlagModeless; //不接受按鈕事件
//以上可以參見(jiàn)SDK :Developer Library ? API Reference ? C++ API reference ? UIKLAFGT
buttons = R_AVKON_SOFTKEYS_OPTIONS_EXIT;
form = r_mmssend_form;
}
// ---------------------------------------------------------
//默認(rèn)的單行顯示模式
// ---------------------------------------------------------
//可以設(shè)置為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; //是一個(gè)編輯文本框 Editor window
//實(shí)際上這個(gè)是枚舉類型,可參看SDK:
//Developer Library ? API Reference ? C++ API reference ? UIKLAFGT ? UIKLAFGT Resource Constants ? TEikStockControls
prompt = qtn_mmssend_recipient_prompt;// 這個(gè)控件的label顯示的字符串
id = EMmsRecipientEditor;
control = EDWIN
{
flags = EEikEdwinNoHorizScrolling | EEikEdwinResizable;
width = qtn_mmssend_recipient_width;
maxlength = qtn_mmssend_recipient_maxlenght;
default_input_mode = EAknEditorNumericInputMode;//數(shù)字輸入模式
};
},
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 );//這條語(yǔ)句一般在ConstructL()中
void CGraphicsAppView::StartTimer()//開(kāi)始啟動(dòng)時(shí)鐘
{
if ( !iPeriodicTimer->IsActive() )
{iPeriodicTimer->Start( 1, 1,
TCallBack( CGraphicsAppView::Period, this ) );//TcallBack是一個(gè)方法回調(diào)函數(shù),從使用來(lái)看,他只能回調(diào)類中的靜態(tài)方法。
}
}
TInt CGraphicsAppView::Period( TAny* aPtr )//周期啟動(dòng)函數(shù),注意,這是個(gè)靜態(tài)函數(shù),但static只在頭文件中才做了申明。
{
( static_cast<CGraphicsAppView*>( aPtr ) )->DoPeriodTask();
return ETrue;
}
void CGraphicsAppView::DoPeriodTask()//周期真正在做的事情
{
// Update the screen
CWindowGc& gc = SystemGc();
gc.Activate( *DrawableWindow() );//如果要求清屏操作。增加gc.Clear();
UpdateDisplay();///////////////////這個(gè)函數(shù)是周期需要實(shí)現(xiàn)的東西
gc.Deactivate();
}
void CGraphicsAppView::StopTiem()//停止時(shí)鐘
{
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); // 設(shè)定時(shí)鐘請(qǐng)求為1秒
User::WaitForRequest(timerStatus); // 等待這個(gè)請(qǐng)求
// 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(); //設(shè)置時(shí)間為當(dāng)前系統(tǒng)時(shí)間
showTime(time);//顯示當(dāng)前時(shí)間
//----------------以下代碼是人為給時(shí)間加10秒--------------
TTimeIntervalSeconds timeIntervalSeconds(10);
time += timeIntervalSeconds;
showTime(time); // print the time the request should complete
//---------------------------------------------------------
timer.At(timerStatus,time); //設(shè)定時(shí)鐘請(qǐng)求為10秒
User::WaitForRequest(timerStatus); //等待這個(gè)請(qǐng)求
// 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是所有字符類的祖先
標(biāo)準(zhǔn)C語(yǔ)言
Symbian OS
讓一個(gè)字符串進(jìn)入2進(jìn)制代碼
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相當(dāng)于不變的字符串常量.
b)TPtr相當(dāng)與String類型。Tbuf相當(dāng)于char[]。前者與后者的唯一區(qū)別是,后者需要指定分配的棧空間大小。
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;
/* 數(shù)據(jù)類型轉(zhuǎn)換*/
TBuf 轉(zhuǎn)換為 TPtrC16
TBuf<32> tText(_L("2004/11/05 05:44:00"));
TPtrC16 tPtrSecond=tText.Mid(17,2);
TPtrC16 轉(zhuǎn)換為 TBufC16
TPtrC16 tPtrSecond=tText.Mid(17,2);
TBufC16<10> bufcs(tPtrSecond);
TBufC16 轉(zhuǎn)換為 TPtr16
TBufC16<10> bufcs(tPtrSecond);
TPtr16 f=bufcs.Des();
TPtr16 轉(zhuǎn)換為 TBuf
TBuf<10> bufSecond;
bufSecond.Copy(f);
TBuf 轉(zhuǎn)換為 TPtr16
TBuf<10> bufSecond(_L("abc"));
TPtr16 f;
f.Copy(bufSecond);
TBuf 轉(zhuǎn)換為 TInt
TInt aSecond;
TLex iLexS(bufSecond);
iLexS.Val(aSecond);
TInt 轉(zhuǎn)換為 TBuf
TBuf<32> tbuf;
TInt i=200;
tbuf.Num(i);
1.串轉(zhuǎn)換成數(shù)字
TBuf16<20> buf(_L( "123" ) );
TLex lex( buf );
TInt iNum;
lex.Val( iNum );
2.數(shù)字轉(zhuǎn)換成串
TBuf16<20> buf;
TInt iNum = 20;
buf.Format( _L( "%d" ) , iNum );
3.將symbian串轉(zhuǎn)換成char串
char* p = NULL;
TBuf8<20> buf( _L( "aaaaa" ) );
p = (char *)buf.Ptr();
4.UTF-8轉(zhuǎn)換成UNICODE
CnvUtfConverter::ConvertToUnicodeFromUtf8( iBuf16 , iBuf8 );
5.UNICODE轉(zhuǎn)換成UTF-8
CnvUtfConverter::ConvertFromUnicodeToUtf8( iBuf8 , iBuf16 );
6.將char串轉(zhuǎn)換成symbian串
char* cc = "aaaa";
TPtrC8 a;
a.Set( (const TUint8*)cc , strlen(cc) );
7、將TPtrc8與TPtrc16之間轉(zhuǎn)化
// 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主要應(yīng)用是初始化某個(gè)內(nèi)存空間。用來(lái)對(duì)一段內(nèi)存空間全部設(shè)置為某個(gè)字符。
memcpy是用于COPY源空間的數(shù)據(jù)到目的空間中,用來(lái)做內(nèi)存拷貝可以拿它拷貝任何數(shù)據(jù)類型的對(duì)象。
strcpy只能拷貝字符串了,它遇到'\0'就結(jié)束拷貝。
strcpy
原型:extern char *strcpy(char *dest,char *src);
用法:#include <string.h>
功能:把src所指由NULL結(jié)束的字符串復(fù)制到dest所指的數(shù)組中。
說(shuō)明:src和dest所指內(nèi)存區(qū)域不可以重疊且dest必須有足夠的空間來(lái)容納src的字符串。
返回指向dest的指針。
memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指內(nèi)存區(qū)域復(fù)制count個(gè)字節(jié)到dest所指內(nèi)存區(qū)域。
說(shuō)明:src和dest所指內(nèi)存區(qū)域不能重疊,函數(shù)返回指向dest的指針。
memset
原型:extern void *memset(void *buffer, int c, int count);
用法:#include <string.h>
功能:把buffer所指內(nèi)存區(qū)域的前count個(gè)字節(jié)設(shè)置成字符c。
說(shuō)明:返回指向buffer的指針。
4、文件類和流操作
Location: s32file.h
文件模擬路徑在C:\Symbian\8.0a\epoc32\wins下面。有C、D兩個(gè)分區(qū)。
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) 與文件服務(wù)器建立通信:
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)建立文件存儲(chǔ)
TParse filestorename;// The class uses the full filename structure supported by Symbian
fsSession.Parse(aName,filestorename);
/*------------------------------------------------------------------------------------------------
TDesC& aName。可以通過(guò)以下方式給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); // 設(shè)定存儲(chǔ)種類
4)將外部數(shù)據(jù)寫(xiě)入流::(記憶方式:>>指向就是數(shù)據(jù)流向)//假設(shè):TSimple anXxx;
RStoreWriteStream outstream;
TStreamId id = outstream.CreateLC(*store);
//----------------------------將標(biāo)量寫(xiě)入數(shù)據(jù)流------------------
outstream<< anXxx;
或者 aStream.WriteInt8L(anXxx);
實(shí)際上這里使用了流的擴(kuò)展化:(當(dāng)輸出不是普通的元數(shù)據(jù)時(shí),使用這個(gè)擴(kuò)展化)這是一個(gè)虛函數(shù)的重載
void TSimple::ExternalizeL(RWriteStream& aStream) const
{
aStream << iTheEnum;
aStream << iBuffer;
aStream.WriteInt32L(iIntValue);
aStream.WriteUint32L(iUintValue);
aStream.WriteReal64L(iRealValue);
}
//------------------------------------------------------------------------------------------------
// 以下是將流改動(dòng)提交到文件服務(wù)器。
outstream.CommitL();
5)將流讀到外部數(shù)據(jù):
RStoreReadStream instream;
store->SetRootL(id);//可以將上面的已經(jīng)存在的流作為流的根。好處是不必再創(chuàng)建流ID。實(shí)際上也就節(jié)省了內(nèi)存。
// 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;//寫(xiě)入類對(duì)象數(shù)據(jù)。
//---------------------------------------------------------------------
void TSimple::InternalizeL(RReadStream& aStream)
{
aStream >> iTheEnum;
aStream >> iBuffer;
iIntValue = aStream.ReadInt32L();
iUintValue = aStream.ReadUint32L();
iRealValue = aStream.ReadReal64L();
}
//------------------------------輸出流到其他數(shù)據(jù)元或類對(duì)象中----------------------------
anXxx = TXxx(aStream.ReadInt8L());
6)關(guān)閉文件服務(wù)通信
fsSession.Close()
5. 活動(dòng)調(diào)度表
由于使用多線程來(lái)處理異步請(qǐng)求比較消耗系統(tǒng)資源,所以Symbian 使用了活動(dòng)對(duì)象(Active Object)來(lái)解決異步請(qǐng)求的問(wèn)題。
活動(dòng)規(guī)劃器(active scheduler)用于處理由活動(dòng)對(duì)象提出的異步請(qǐng)求。它檢測(cè)活動(dòng)對(duì)象提出的異步請(qǐng)求,并安排活動(dòng)對(duì)象的請(qǐng)求完成事件的執(zhí)行順序。活動(dòng)規(guī)劃器僅用一個(gè)事件處理線程來(lái)規(guī)劃各個(gè)活動(dòng)對(duì)象提出的事件請(qǐng)求,所以它要比多線程實(shí)現(xiàn)異步請(qǐng)求占用更少的資源。
1、 首先應(yīng)該創(chuàng)建一個(gè)活動(dòng)規(guī)劃器對(duì)象,并把它安裝到當(dāng)前線程
CActiveScheduler* scheduler = new(ELeave) CActiveScheduler();//創(chuàng)建一個(gè)活動(dòng)規(guī)劃器
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);// 安裝活動(dòng)規(guī)劃器。
TRAPD(error,doInstanceL()); //具體安排的函數(shù)處理。
在具體的安排函數(shù)中一定要啟動(dòng)這個(gè)規(guī)劃器
CActiveScheduler::Start();//這句話告訴活動(dòng)規(guī)劃器該等待對(duì)象的狀態(tài)的改變
2、 把自己加入活動(dòng)規(guī)劃器:一般這是一個(gè)類。可以在類的構(gòu)造函數(shù)中申明下面代碼。
CActiveScheduler::Add(this);
//該類必須有一個(gè)繼承來(lái)自public CActive, public MmsvSessionObserver
//在構(gòu)造函數(shù)時(shí),也可以宣布優(yōu)先級(jí)別:TclassA::classA() : CActive(0)
3、返回改變事實(shí):
SetActive(); / / CActive類對(duì)象提交異步請(qǐng)求。
//這個(gè)請(qǐng)求說(shuō)明對(duì)象的改變完成。就會(huì)觸發(fā)CActive::RunL()
4、這里的CActiveScheduler只管理了一個(gè)CActive對(duì)象,就是timeCount,可以用類似的方法實(shí)現(xiàn)多個(gè)CActive,并且都加入CActiveScheduler,CActiveScheduler將會(huì)等待所有加入它的CActive的狀態(tài)的改變,其中有一個(gè)的狀態(tài)改變就會(huì)去執(zhí)行對(duì)應(yīng)的活動(dòng)對(duì)象的處理函數(shù),當(dāng)狀態(tài)同時(shí)發(fā)生的時(shí)候,會(huì)通過(guò)對(duì)象的優(yōu)先級(jí)來(lái)決定先調(diào)用誰(shuí)的RunL函數(shù).CActiveScheduler也是非搶占式的,當(dāng)一個(gè)RunL函數(shù)還沒(méi)有執(zhí)行完的時(shí)候,如果另一個(gè)CActive的狀態(tài)改變,會(huì)等待RunL執(zhí)行完以后再執(zhí)行另一個(gè)CActive的處理函數(shù).
6、線程:
1、 創(chuàng)建一個(gè)等待的線程:
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); //創(chuàng)建一個(gè)信號(hào)量,等待線程的正常結(jié)束
res=thread.Create(KCountServerName, // create new server thread
CCountServServer::ThreadFunction, // 線程啟動(dòng)的主函數(shù)
KDefaultStackSize,
KDefaultHeapSize,
KDefaultHeapSize,
&semaphore // 最后是主函數(shù)的需要的參數(shù)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();
}