乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      自定義控件

       昵稱135705 2009-04-20

      一、定義

      概述

      Symbian OS提供了大量現成的控件,但是有時還不夠用。你可以通過從CCoeControl基類派生構造你自己的UI控件。

      本例演示如何定義你自己的有一個CEikLabel的簡單自定義控件。 派生自CCoeControl 并實現下述基本方法:

      • 構造
      • 布局
      • 大小
      • 繪制控件
      • 被包含的控件(若有的話)

       

      MMP文件

      下面的庫文件是必須的:

      LIBRARY euser.lib

      LIBRARY apparc.lib

      LIBRARY cone.lib

      LIBRARY eikcore.lib

      LIBRARY avkon.lib

      LIBRARY commonengine.lib

      LIBRARY eikcoctl.lib

      LIBRARY gdi.lib

      頭文件

      #include <coecntrl.h>

      #include <eiklabel.h>

         

      class CMyControl : public CCoeControl

      {

      public:

      static CMyControl* NewL(const TRect& aRect,const CCoeControl* aParent=NULL);

      static CMyControl* NewLC(const TRect& aRect,const CCoeControl* aParent=NULL);

      virtual ~CMyControl();

         

      public: // from CCoeControl

      TSize MinimumSize();

         

      private: // from CCoeControl

      void Draw(const TRect& aRect) const;

      void SizeChanged();

         

      private: // own methods

      CMyControl();

      void ConstructL(const TRect& aRect,const CCoeControl* aParent = NULL);

         

      private: // data

      CEikLabel* iStatusText;

      };

      源代碼

      CMyControl* CMyControl::NewL(const TRect& aRect,const CCoeControl* aParent)

      {

      CMyControl* self = CMyControl::NewLC(aRect,aParent);

      CleanupStack::Pop(self);

      return self;

      }

         

      CMyControl* CMyControl::NewLC(const TRect& aRect,const CCoeControl* aParent)

      {

      CMyControl* self = new(ELeave) CMyControl();

      CleanupStack::PushL(self);

      self->ConstructL(aRect,aParent);

      return self;

      }

         

      CMyControl::CMyControl()

      {

      }

         

      CMyControl::~CMyControl()

      {

      // NOTE: Does not delete iStatusText because we do not own it

      }

         

      void CMyControl::ConstructL(const TRect& aRect,const CCoeControl* aParent)

      {

      // No owner, so create an own window

      if(aParent == NULL)

      {

      CreateWindowL();

      }

      // Use Parent's window

      else

      {

      // This is component in a compound control

      SetContainerWindowL(*aParent);

      }

         

      // Initialize component array

      InitComponentArrayL();

         

      // Create contained controls

      iStatusText = new (ELeave) CEikLabel;

      iStatusText->SetContainerWindowL(*this);

      iStatusText->SetTextL(_L("HelloWorld"));

         

      // Store component to component array

      Components().AppendLC(iStatusText);

      CleanupStack::Pop(iStatusText);

         

      SetRect(aRect); // or

      //SetExtentToWholeScreen(); //NOTE: Can not see CBA buttons

         

      // The application should call this function on

      // all controls that are not components in a compound control

      if(aParent == NULL)

      {

      ActivateL();

      }

      }

         

      TSize CMyControl::MinimumSize()

      {

      // Get CEikLabel minium size and grow it

      // that is this control MinimumSize.

      // Custom control also needs a few other methods so it can be laid out

      // and drawn. For example, custom controls usually implement MinimumSize(),

      // SizeChanged() and Draw() methods.

         

      // When using control in container control, set the minium size very small

      TRect rect = iStatusText->MinimumSize();

      rect.Grow(TSize(2,2));

      return rect.Size();

         

      // When using the control in a dialog, set the control size large

      //return Rect().Size();

      }

         

      void CMyControl::SizeChanged()

      {

      // Responds to size changes to set the size and position of the contents

      // of this control. For a simple control this might include text or

      // graphics. For a compound control this sets the size and position of the

      // component. It has an empty default implementation and should be

      // implemented by the CCoeControl-derived class.

      // The function is called whenever SetExtent(), SetSize(), SetRect(),

      // SetCornerAndSize(), or SetExtentToWholeScreen() are called on

      // the control.

      if (iStatusText)

      {

      TRect labelRect(Rect());

      labelRect.Shrink(TSize(5,2));

      iStatusText->SetRect(labelRect);

      }

      }

         

      void CMyControl::Draw(const TRect& /*aRect*/) const

      {

      CWindowGc& gc = SystemGc();

      gc.SetBrushColor(KRgbBlue);

      gc.Clear(Rect());

      }

       

      使用CCoeControl

      自定義控件CMyControl被添加到了MultiViews例子(S60 3rd FP1)中,在.cpp中作如下修改:

      void CMultiViewsView1::DoActivateL( const TVwsViewId& /*aPrevViewId*/,

      TUid /*aCustomMessageId*/,

      const TDesC8& /*aCustomMessage*/)

      {

      iControl = CMyControl::NewL(ClientRect());

      }

         

      void CMultiViewsView1::DoDeactivate()

      {

      if (iControl)

      {

      AppUi()->RemoveFromStack(iControl);

      delete iControl;

      iControl = NULL;

      }

      }

         

      void CMultiViewsView1::HandleSizeChange( TInt aType )

      {

      if( iControl )

      {

      iControl->HandleResourceChange( aType );

      if ( aType==KEikDynamicLayoutVariantSwitch )

      {

      TRect rect;

      AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);

      iControl->SetRect(rect);

      }

      }

      }

       

      后置條件

      CMyControl可以放在另一個CCoeControl里(復合控件),或作為單獨的控件而沒有父控件。

       

       

      從資源構造:

      概要

      這個代碼片段演示如何從資源創(chuàng)建CCoeControl。本例擴展了代碼片段自定義控件: 定義(一)。

      CMyControl::ConstructFromResourceL()用與CMyControl::ConstructL()同樣的方式初始化控件,但參數從TResourceReader獲得。資源讀取器是一個從資源文件流讀取字節(jié)的工具類;因此,讀取必須以同樣的順序且數據大小必須與那些定義在資源結構聲明中的相匹配。

      MMP 文件

      下面的能力(capabilities)和庫(libraries)是必須的:

      LIBRARY bafl.lib //TResourceReader

      頭文件

      添加一個當從資源創(chuàng)建類時被調用的新函數,以及添加BARSREAD.H包含頭文件。缺省構造函數CMyControl()應被移至public范圍。

      #include <BARSREAD.H>

         

      public:

      CMyControl();

      void ConstructFromResourceL(TResourceReader& aReader);

      源代碼

      這段代碼為CEikLabel從資源讀取文本。

      void CMyControl::ConstructFromResourceL(TResourceReader& aReader)

      {

      // No parent owner, so create an own window

      CreateWindowL();

         

      // Initialize component array

      InitComponentArrayL();

         

      // Create contained controls

      iStatusText = new (ELeave) CEikLabel;

      iStatusText->SetContainerWindowL(*this);

         

      // Read label from resource

      TPtrC label = aReader.ReadTPtrC16();

      iStatusText->SetTextL(label);

       

      // Store component to component array

      Components().AppendLC(iStatusText);

      CleanupStack::Pop(iStatusText);

         

      // Set component rect to CMultiViewsAppUi::ClientRect()

      CMultiViewsAppUi* appui = (static_cast<CMultiViewsAppUi*>(iEikonEnv->AppUi()));

      appui->ClientRect();

      if (appui)

      {

      SetRect(appui->ClientRect());

      }

       

      ActivateL();

      }

      CMyControl部件資源customcontrol.rh文件

      CMyControl部件的資源配置參數:

      STRUCT CUSTOMCONTROL

      {

      LTEXT txt;

      }

      資源.rss文件

      對Multiviews樣例應用資源文件作如下變更: multiviews.rss.

      // New include that is our own component resource config

      #include "customcontrol.rh"

         

      // Defining resource for our component.

      RESOURCE CUSTOMCONTROL r_custom_control

      {

      txt = STRING_r_custom_control;

      }

      STRING_r_custom_control定義在multiviews.rls中.

       

      從資源創(chuàng)建部件

      創(chuàng)建資源讀取器,經由缺省構造函數創(chuàng)建部件,然后調用ConstructFromResourceL():

      // Creating control from resource

      TResourceReader reader;

      iEikonEnv->CreateResourceReaderLC(reader, R_CUSTOM_CONTROL);

      iContainer2 = new (ELeave) CMyControl;

      iContainer2->ConstructFromResourceL(reader);

      iContainer2->SetRect(ClientRect());

      CleanupStack::PopAndDestroy(); // reader

      后置條件

      CMyControl通過已定義資源參數被創(chuàng)建。

       

      容器控件:

      概述

      這個代碼片段演示如何創(chuàng)建一個擁有自定義控件的容器控件。例子演示如何用一個矩形框聚焦第一個部件及如何改變焦點。

      本例擴展已存代碼片段自定義控件: 定義(一); 容器控件存儲其控件到CCoeControlArray中。

      頭文件

      class CMyContainerControl : public CCoeControl

      {

      public:

      static CMyContainerControl* NewL(const TRect& aRect);

      static CMyContainerControl* NewLC(const TRect& aRect);

      virtual ~CMyContainerControl();

         

      private: // from CCoeControl

      void Draw(const TRect& aRect) const;

      void SizeChanged();

         

      public: // own methods

      // NOTE: Transfer ownership to CMyContainerControl

      void AddControlL(CCoeControl* aControl,TInt aControlId);

      void UpdateControls();

       

      private: // own methods

      CMyContainerControl();

      void ConstructL(const TRect& aRect);

      };

       

      源代碼

      CMyContainerControl* CMyContainerControl::NewL(const TRect& aRect)

      {

      CMyContainerControl* self = CMyContainerControl::NewLC(aRect);

      CleanupStack::Pop(self);

      return self;

      }

         

      CMyContainerControl* CMyContainerControl::NewLC(const TRect& aRect)

      {

      CMyContainerControl* self = new(ELeave) CMyContainerControl();

      CleanupStack::PushL(self);

      self->ConstructL(aRect);

      return self;

      }

         

      CMyContainerControl::CMyContainerControl()

      {

      }

         

      CMyContainerControl::~CMyContainerControl()

      {

      }

         

      void CMyContainerControl::ConstructL(const TRect& aRect)

      {

      // No parent owner, so create an own window

      CreateWindowL();

       

      // Initialize component array

      InitComponentArrayL();

       

      SetRect(aRect);

       

      ActivateL();

      }

         

      void CMyContainerControl::SizeChanged()

      {

      UpdateControls();

      }

         

      void CMyContainerControl::UpdateControls()

      {

      TPoint position;

         

      // Goes through all components of this container control

      CCoeControlArray::TCursor cursor = Components().Begin();

      CCoeControl* ctrl = NULL;

      while ((ctrl = cursor.Control<CCoeControl>()) != NULL)

      {

      // If control is not visible, do not set it's position

      if (!ctrl->IsVisible())

      {

      cursor.Next();

      continue;

      }

       

      // Set position

      ctrl->SetPosition(position);

         

      // Set size

      TSize size = ctrl->MinimumSize();

      size.SetSize(Rect().Width(),size.iHeight);

      ctrl->SetSize(size);

         

      // Calculate position

      position.iY += size.iHeight;

       

      // Does control fit to view?

      if (position.iY >= Rect().iBr.iY)

      {

      ctrl->MakeVisible(EFalse);

      }

      else

      {

      ctrl->MakeVisible(ETrue);

      }

       

      cursor.Next();

      }

      }

         

      void CMyContainerControl::Draw(const TRect& /*aRect*/) const

      {

      CWindowGc& gc = SystemGc();

      gc.SetBrushColor(KRgbBlack);

      gc.Clear(Rect());

      }

         

      void CMyContainerControl::AddControlL(CCoeControl* aControl,TInt aControlId)

      {

      // NOTE: Transfer ownership of CCoeControl to CMyContainerControl

      // Add control into container control

      Components().AppendLC(aControl,aControlId);

      CleanupStack::Pop(aControl);

         

      // Focus first component

      if (Components().Count()==1)

      {

      aControl->SetFocus(ETrue);

      }

         

      // Update control's position

      UpdateControls();

      }

      SizeChanged函數

      當CMyContainerControl的大小改變時(例如: 當SetRect()被調用時),部件的位置必須再次計算。

      void CMyContainerControl::SizeChanged()

      {

      // Sets new position of the components

      UpdateControls();

      }

      聚焦部件

      對CMyControl部件做如下變更:

      • CMyControl::Draw()必須調用DrawFocusFrame()以便繪制焦點矩形
      • CMyControl::DrawFocusFrame()是一個新方法,在其中繪制焦點框

      void CMyControl::Draw(const TRect& aRect) const

      {

      CWindowGc& gc = SystemGc();

      gc.SetBrushColor(KRgbBlue);

      gc.Clear(Rect());

       

      DrawFocusFrame(aRect);

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多