原文地址: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();
}