Categories: Lang-CN | Symbian C++ | UI | S60 | Code Examples
This page was last modified 13:57, 6 July 2008.
自定义控件: 从资源构造(二)
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 |
概要
这个代码片段演示如何从资源创建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通过已定义资源参数被创建。
参见
自定义控件系列:
- 自定义控件: 定义(一) 定义自定义控件
- 自定义控件: 容器控件(三) 创建容器控件
- 自定义控件: 聚焦(四) 处理摁键事件和改变活动自定义控件焦点
- 自定义控件: 滚动条(五) 给自定义控件添加滚动条
- 自定义控件: 在Dialog中(六) 把自定义控件加到CAknDialog
- Image:CustomControl.zip 样例代码补丁
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 请教:关于自定义List风格 | hszr | Symbian | 2 | 2008-05-11 07:44 |

