剛開始接觸S60,感覺很多東西和MFC都很相似,但也有點摸不著頭腦,不知如何下手。
看了老半天,還是從最直觀的介面出著手。
CLabelAppUi:這個是UI組件類,暫時想到它的功能就是放置控制項。
其成員變數 CLabelContainer* iAppContainer; //容器指標
void CLabelAppUi::ConstructL()
{
BaseConstructL();
iAppContainer = new (ELeave) CLabelContainer;
iAppContainer->SetMopParent( this );
iAppContainer->ConstructL( ClientRect() );
AddToStackL( iAppContainer );
}
第二步看 CLabelContainer:
其成員變數我定義了2個:
private: //data
CEikLabel* iLabel; // example label
CEikLabel * iMyLabel; //colin
相關的建立並初始化操作,在ConstructL函數中完成:
void CLabelContainer::ConstructL(const TRect& aRect)
{
CreateWindowL();
iLabel = new (ELeave) CEikLabel;
iLabel->SetContainerWindowL( *this );
iLabel->SetTextL( _L("Simple Label") );//設定常值內容
iLabel->SetExtent( TPoint(10, 10), TSize (150, 30)); //設定位置
SetLabelStyle(1); //這個是自訂的函數,可設定Label的各種格式,可copy回去重用
iMyLabel = new (ELeave)CEikLabel;
iMyLabel->SetContainerWindowL(*this);
iMyLabel->SetTextL(_L("colin Come"));
iMyLabel->SetExtent(TPoint(15,15),TSize(150,30));
SetLabelStyle(2);
SetRect(aRect);
ActivateL();
}
關於大小改變的有個架構函數:
void CLabelContainer::SizeChanged()
{
// TODO: Add here control resize code etc.
//iLabel->SetExtent( TPoint(10,10), iLabel->MinimumSize() );
iLabel->SetExtent( TPoint(10, 10), TSize (150, 30));
iMyLabel->SetExtent( TPoint(15,15), TSize(150,30));
}
iLabel->MinimumSize(),通過這個函數可以靈活的選擇適應視窗大小。
當自己加入了新的控制項後,下面這個函數中的返回參數必須修改:
TInt CLabelContainer::CountComponentControls() const
{
return 2; // return nbr of controls inside this container
}
以下函數用於標識本容器中所擁有的控制項(架構函數,可以調換一下各個返回指標的位置,發現不同點):
CCoeControl* CLabelContainer::ComponentControl(TInt aIndex) const
{
switch ( aIndex )
{
case 0:
return iLabel;//;
case 1:
return iMyLabel;
default:
return NULL;
}
}
最後,給出自訂的設定標籤樣式的函數:
void CLabelContainer::SetLabelStyle (TInt aStyle)
{
switch (aStyle)
{
case 1:
{
iLabel ->SetAlignment (EHLeftVTop) ;
iLabel->SetFont( LatinBold12() );
iLabel->SetStrikethrough(EFalse);
iLabel->SetUnderlining(EFalse);
break;
}
case 2:
{
iLabel ->SetAlignment( EHCenterVCenter );
iLabel->SetFont( LatinBold19() );
iLabel->SetStrikethrough(EFalse);
iLabel->SetUnderlining(ETrue);
break;
}
case 3:
{
iLabel ->SetAlignment( EHRightVBottom );
iLabel->SetFont( LatinBold17() );
iLabel->SetStrikethrough(ETrue);
iLabel->SetUnderlining(EFalse);
break;
}
}
}