原文地址: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)用,主要是用來初始化菜單顯示的具體項目。
aResourceId 是資源的具體ID,如R_SMS_MENU。
aMenuPane 通過調(diào)用aMenuPane->SetItemDimmed(菜單項目資源ID,EFalse);來顯示或隱藏該菜單選項。注意:Etrue為隱藏。
1、話框類:CEikDialog (OK/CANCEL)
主要成員函數(shù)有:
void PreLayoutDynInitL();//處理在對話框出現(xiàn)之前的初始化動作
TBool OkToExitL( TInt aButtonId );//對OK按的處理
Void HandleControlStateChangL(Tint aControlId);//監(jiān)聽對話框上控件改動,有點類似與Appui類的void CAknAppUi::HandleCommandL(TInt aCommand)。
//構(gòu)造方式:
CMmssSendDialog* iSendDialog = new ( ELeave ) CMmssSendDialog;
iSendDialog->SetMopParent( this );
iSendDialog->ExecuteLD( R_MMSSEND_DIALOG );
//-------------------------------定義一個對話框資源---------------------------
RESOURCE DIALOG r_mmssend_dialog
{
flags = EEikDialogFlagNoDrag | // 無法拖曳
EEikDialogFlagNoTitleBar | //無標(biāo)題欄
EEikDialogFlagFillAppClientRect | //將應(yīng)用程序客戶區(qū)填滿
EEikDialogFlagCbaButtons | //使用CBA按鈕
EEikDialogFlagModeless; //不接受按鈕事件
//以上可以參見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; //是一個編輯文本框 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;//數(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 );//這條語句一般在ConstructL()中
void CGraphicsAppView::StartTimer()//開始啟動時鐘
{
if ( !iPeriodicTimer->IsActive() )
{iPeriodicTimer->Start( 1, 1,
TCallBack( CGraphicsAppView::Period, this ) );//TcallBack是一個方法回調(diào)函數(shù),從使用來看,他只能回調(diào)類中的靜態(tài)方法。
}
}
TInt CGraphicsAppView::Period( TAny* aPtr )//周期啟動函數(shù),注意,這是個靜態(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();///////////////////這個函數(shù)是周期需要實現(xiàn)的東西
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); // 設(shè)定時鐘請求為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(); //設(shè)置時間為當(dāng)前系統(tǒng)時間
showTime(time);//顯示當(dāng)前時間
//----------------以下代碼是人為給時間加10秒--------------
TTimeIntervalSeconds timeIntervalSeconds(10);
time += timeIntervalSeconds;
showTime(time); // print the time the request should complete
//---------------------------------------------------------
timer.At(timerStatus,time); //設(shè)定時鐘請求為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是所有字符類的祖先
標(biāo)準(zhǔn)C語言
Symbian OS
讓一個字符串進(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)用是初始化某個內(nèi)存空間。用來對一段內(nèi)存空間全部設(shè)置為某個字符。
memcpy是用于COPY源空間的數(shù)據(jù)到目的空間中,用來做內(nèi)存拷貝可以拿它拷貝任何數(shù)據(jù)類型的對象。
strcpy只能拷貝字符串了,它遇到'\0'就結(jié)束拷貝。
strcpy
原型:extern char *strcpy(char *dest,char *src);
用法:#include <string.h>
功能:把src所指由NULL結(jié)束的字符串復(fù)制到dest所指的數(shù)組中。
說明:src和dest所指內(nèi)存區(qū)域不可以重疊且dest必須有足夠的空間來容納src的字符串。
返回指向dest的指針。
memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指內(nèi)存區(qū)域復(fù)制count個字節(jié)到dest所指內(nèi)存區(qū)域。
說明: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個字節(jié)設(shè)置成字符c。
說明:返回指向buffer的指針。
4、文件類和流操作
Location: s32file.h
文件模擬路徑在C:\Symbian\8.0a\epoc32\wins下面。有C、D兩個分區(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)建立文件存儲
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); // 設(shè)定存儲種類
4)將外部數(shù)據(jù)寫入流::(記憶方式:>>指向就是數(shù)據(jù)流向)//假設(shè):TSimple anXxx;
RStoreWriteStream outstream;
TStreamId id = outstream.CreateLC(*store);
//----------------------------將標(biāo)量寫入數(shù)據(jù)流------------------
outstream<< anXxx;
或者 aStream.WriteInt8L(anXxx);
實際上這里使用了流的擴(kuò)展化:(當(dāng)輸出不是普通的元數(shù)據(jù)時,使用這個擴(kuò)展化)這是一個虛函數(shù)的重載
void TSimple::ExternalizeL(RWriteStream& aStream) const
{
aStream << iTheEnum;
aStream << iBuffer;
aStream.WriteInt32L(iIntValue);
aStream.WriteUint32L(iUintValue);
aStream.WriteReal64L(iRealValue);
}
//------------------------------------------------------------------------------------------------
// 以下是將流改動提交到文件服務(wù)器。
outstream.CommitL();
5)將流讀到外部數(shù)據(jù):
RStoreReadStream instream;
store->SetRootL(id);//可以將上面的已經(jīng)存在的流作為流的根。好處是不必再創(chuàng)建流ID。實際上也就節(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;//寫入類對象數(shù)據(jù)。
//---------------------------------------------------------------------
void TSimple::InternalizeL(RReadStream& aStream)
{
aStream >> iTheEnum;
aStream >> iBuffer;
iIntValue = aStream.ReadInt32L();
iUintValue = aStream.ReadUint32L();
iRealValue = aStream.ReadReal64L();
}
//------------------------------輸出流到其他數(shù)據(jù)元或類對象中----------------------------
anXxx = TXxx(aStream.ReadInt8L());
6)關(guān)閉文件服務(wù)通信
fsSession.Close()
5. 活動調(diào)度表
由于使用多線程來處理異步請求比較消耗系統(tǒng)資源,所以Symbian 使用了活動對象(Active Object)來解決異步請求的問題。
活動規(guī)劃器(active scheduler)用于處理由活動對象提出的異步請求。它檢測活動對象提出的異步請求,并安排活動對象的請求完成事件的執(zhí)行順序。活動規(guī)劃器僅用一個事件處理線程來規(guī)劃各個活動對象提出的事件請求,所以它要比多線程實現(xiàn)異步請求占用更少的資源。
1、 首先應(yīng)該創(chuàng)建一個活動規(guī)劃器對象,并把它安裝到當(dāng)前線程
CActiveScheduler* scheduler = new(ELeave) CActiveScheduler();//創(chuàng)建一個活動規(guī)劃器
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);// 安裝活動規(guī)劃器。
TRAPD(error,doInstanceL()); //具體安排的函數(shù)處理。
在具體的安排函數(shù)中一定要啟動這個規(guī)劃器
CActiveScheduler::Start();//這句話告訴活動規(guī)劃器該等待對象的狀態(tài)的改變
2、 把自己加入活動規(guī)劃器:一般這是一個類。可以在類的構(gòu)造函數(shù)中申明下面代碼。
CActiveScheduler::Add(this);
//該類必須有一個繼承來自public CActive, public MmsvSessionObserver
//在構(gòu)造函數(shù)時,也可以宣布優(yōu)先級別:TclassA::classA() : CActive(0)
3、返回改變事實:
SetActive(); / / CActive類對象提交異步請求。
//這個請求說明對象的改變完成。就會觸發(fā)CActive::RunL()
4、這里的CActiveScheduler只管理了一個CActive對象,就是timeCount,可以用類似的方法實現(xiàn)多個CActive,并且都加入CActiveScheduler,CActiveScheduler將會等待所有加入它的CActive的狀態(tài)的改變,其中有一個的狀態(tài)改變就會去執(zhí)行對應(yīng)的活動對象的處理函數(shù),當(dāng)狀態(tài)同時發(fā)生的時候,會通過對象的優(yōu)先級來決定先調(diào)用誰的RunL函數(shù).CActiveScheduler也是非搶占式的,當(dāng)一個RunL函數(shù)還沒有執(zhí)行完的時候,如果另一個CActive的狀態(tài)改變,會等待RunL執(zhí)行完以后再執(zhí)行另一個CActive的處理函數(shù).
6、線程:
1、 創(chuàng)建一個等待的線程:
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)建一個信號量,等待線程的正常結(jié)束
res=thread.Create(KCountServerName, // create new server thread
CCountServServer::ThreadFunction, // 線程啟動的主函數(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();
}
posted on 2010-03-29 13:23
漂漂 閱讀(569)
評論(0) 編輯 收藏 引用 所屬分類:
symbian開發(fā)