Namespaces

Variants
Actions

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.

自定义控件: 从资源构造(二)

From Wiki
Jump to: navigation, search
Article Metadata

代码示例
文章
dougcn 在 06 Jul 2008 创建
最后由 hamishwillee 在 30 May 2013 编辑
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

Contents

概要

这个代码片段演示如何从资源创建CCoeControl。本例扩展了代码片段自定义控件: 定义(一)

CMyControl::ConstructFromResourceL()用与CMyControl::ConstructL()同样的方式初始化控件,但参数从TResourceReader获得。资源读取器是一个从资源文件流读取字节的工具类;因此,读取必须以同样的顺序且数据大小必须与那些定义在资源结构声明中的相匹配。

MMP 文件

下面的能力(capabilities)和库(libraries)是必须的:

LIBRARY bafl.lib //TResourceReader

头文件

添加一个当从资源创建类时被调用的新函数,以及添加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中.


从资源创建部件

创建资源读取器,经由缺省构造函数创建部件,然后调用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通过已定义资源参数被创建。

参见

自定义控件系列:

This page was last modified on 30 May 2013, at 04:41.
80 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.

×