Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 11:55, 28 March 2008.

CS000860 - Custom control: Construct from resource

From Forum Nokia Wiki


ID CS000860 Creation date March 25, 2008
Platform S60 3rd Edition, FP1 Tested on devices Nokia N95
Category Symbian C++ Subcategory UI


Keywords (APIs, classes, methods, functions): CCoeControl,TResourceReader

Overview

This code snippet shows how to create CCoeControl from a resource. This example extends the code snippet CS000859 - Custom control.

CMyControl::ConstructFromResourceL() initiates the control in the same way as the CMyControl::ConstructL() method, but the parameters are obtained from TResourceReader. The resource reader is a utility class that reads bytes from a resource file stream; therefore, reading must be in the same order and the data sizes must match those defined in the resource structure declaration.

MMP file

The following capabilities and libraries are required:

LIBRARY bafl.lib //TResourceReader

Header

Add a new function that is called when creating the class from the resource and add the BARSREAD.H include. The default constructor CMyControl() should be moved to public.

#include <BARSREAD.H>
 
public:
    CMyControl();
    void ConstructFromResourceL(TResourceReader& aReader);

Source

This code reads text for CEikLabel from the resource.

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 component resource customcontrol.rh file

Resource config parameters of the CMyControl component

STRUCT CUSTOMCONTROL
{
LTEXT txt;
}

Resource .rss file

Make the following changes to the Multiviews example application resource file 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 is defined in multiviews.rls.


Creating component from resource

Create the resource reader, create the component via the default constuctor, and call 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

Postconditions

CMyControl is created by defined resource parameters.

See also

Custom Control Series:

Related Discussions
Thread Thread Starter Forum Replies Last Post
EDWIN Control in a Symbian Application vinayashrestha Symbian User Interface 2 2007-10-25 10:36
two dialog Rx-lee General Symbian C++ 5 2004-11-30 03:52
custom textfiled? dakoz Mobile Java General 6 2006-05-02 14:14
CAknNoteDialog handling in active object nishantghai Symbian User Interface 2 2007-04-13 13:28
AVKON_LIST_QUERY in rss crashes resource compiler tal.shahar@mobile-mx.com General Symbian C++ 8 2007-09-09 07:38
 
Powered by MediaWiki