S60 C++編程 初學筆記(1)標籤控制項

來源:互聯網
上載者:User

      剛開始接觸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;
            }
        }
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.