Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to create a custom control in Symbian C++

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Code ExampleTested with
Devices(s): Nokia N95
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition FP1
Keywords: CCoeControl
Created: tepaa (29 Feb 2008)
Last edited: hamishwillee (30 May 2013)



Symbian OS provides a large set of ready-made controls but sometimes that is not enough. You can build your own UI control by deriving CCoeControl base class for controls. This example shows how to define your own simple custom control that has the CEikLabel.

Start by deriving CCoeControl and implement basic methods

  • construction
  • layout
  • size
  • drawing the control
  • contained controls (if any)

MMP file

The following libraries are needed:

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
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
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);
return self;
CMyControl* CMyControl::NewLC(const TRect& aRect,const CCoeControl* aParent)
CMyControl* self = new(ELeave) CMyControl();
return self;
// 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)
// Use Parent's window
// This is component in a compound control
// Initialize component array
// Create contained controls
iStatusText = new (ELeave) CEikLabel;
// Store component to component array
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)
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();
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());
void CMyControl::Draw(const TRect& /*aRect*/) const
CWindowGc& gc = SystemGc();

Using CCoeControl

The CMyControl custom control was added to the (S60 3rd FP1) MultiViews example and these are the changes in .cpp.

void CMultiViewsView1::DoActivateL( const TVwsViewId& /*aPrevViewId*/,
TUid /*aCustomMessageId*/,
const TDesC8& /*aCustomMessage*/)
iControl = CMyControl::NewL(ClientRect());
void CMultiViewsView1::DoDeactivate()
if (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);


CMyControl can be inside another CCoeControl (compound control) or be as a single control without a parent.

See also

Custom Control Series:

Other useful articles:

This page was last modified on 30 May 2013, at 04:33.
73 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.