Symbian学习笔记(14):使用Browser Control API

再把这个Browser Control API也总结一下吧,只是做个引导,其实要掌握它的用法最好的方法是打开 9.1/S60_3rd/S60Ex/BrCtlSampleApp 这个例子来阅读,它几乎涵盖了这个API的所有使用方法。

而我在UniNews中只使用了它最基本的用法,下面给出代码:

首先,在H文件中声明一个控件成员:
#include  < coecntrl.h >
#include 
< brctlinterface.h >  
#include 
< brctldefs.h >
#include 
< brctllayoutobserver.h >  
#include 
< brctllinkresolver.h >

class  CUniNewsWebContainer :  public  CCoeControl, MCoeControlObserver,MBrCtlLoadEventObserver  {
public:
    
// Constructors and destructor        
    ~CUniNewsWebContainer();
    
static CUniNewsWebContainer* NewL(const TRect& aRect);
    
static CUniNewsWebContainer* NewLC(const TRect& aRect);

private:
    
// New functions
    void ConstructL(const TRect& aRect);
    CUniNewsWebContainer();

public:
    
// Functions from base classes
    TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType);
    
void HandleBrowserLoadEventL(TBrCtlDefs::TBrCtlLoadEvent aLoadEvent,TUint aSize,TUint16 aTransactionId);
    
void LoadContentL(TInt id);
    
private:
    
// Functions from base classes
    void SizeChanged();
    TInt CountComponentControls() 
const;
    CCoeControl
* ComponentControl(TInt aIndex) const;
    
void Draw(const TRect& aRect) const;
    
void HandleControlEventL(CCoeControl* aControl, TCoeEvent aEventType);

    HBufC8
* ReadFileLC(const TDesC& aFileName);
private:
//data
    CBrCtlInterface*    iBrowser;
    TUint                iCapabilities;
    TInt                 iCommandBase;
}
;
主要声明了三个成员,其中CBrCtlInterface是主要的browser控件,其它两个是构造时的所需要的参数。而这个类派生于接口MBrCtlLoadEventObserver,所以实现它的方法void HandleBrowserLoadEventL(TBrCtlDefs::TBrCtlLoadEvent aLoadEvent,TUint aSize,TUint16 aTransactionId);

在实现文件CPP中,我们需要构造它:
void  CUniNewsWebContainer::ConstructL( const  TRect &  aRect)  {
    
// Create a window for this application view
    CreateWindowL();
    SetRect(aRect);
    
    
//add your code here ...
    iBrowser=CreateBrowserControlL(this
            ,aRect
            ,iCapabilities
            ,iCommandBase
            ,NULL     
//softkey observer
            ,NULL    //link resolver
            ,NULL    //special load observer
            ,NULL    //layout observer
            ,NULL    //dialog provider
            );
    iBrowser
->ActivateL();
    
if(iBrowser){
        iBrowser
->AddLoadEventObserverL(this);
        iBrowser
->SetBrowserSettingL(TBrCtlDefs::ESettingsFontSize,TBrCtlDefs::EFontSizeLevelNormal);

        
    }
    
    ActivateL();    
}

在构造函数中我们初始化那两个参数:
CUniNewsWebContainer::CUniNewsWebContainer()  {
    
// No implementation required
    iCapabilities=TBrCtlDefs::ECapabilityDisplayScrollBar|TBrCtlDefs::ECapabilityLoadHttpFw;
    iCommandBase
=TBrCtlDefs::ECommandIdBase;
    iBrowser
=NULL;
}

删除的时候记得将它的事件监听器都注销掉:
CUniNewsWebContainer:: ~ CUniNewsWebContainer()  {
    
// No implementation required
    if(iBrowser){
        iBrowser
->RemoveLoadEventObserver(this);        
    }

    delete iBrowser;
    iBrowser
=NULL;
}

此外,它跟其它控件一样,在Resize时要处理一下,并且它也需要声明自己是一个组件等等的。
而方法HandleBrowserLoadEventL只需要简单地重绘一下即可。

真正的使用在这儿呢,很简单:
void  CUniNewsWebContainer::LoadContentL(TInt id)
{
    
if(iBrowser){    
        TFileName fname;
        fname.Format(KContentFile,id);
        iBrowser
->LoadUrlL(fname);
        }

}
        

就是一句话 LoadUrlL就可以了,这个URL可以是http:// 也可以是 file://,很方便。

不过经常我们是需要将内存里的内容加载显示出来,那就稍稍多做一点工作:
void  CUniNewsWebContainer::LoadContentL(TInt id)
{
    
if(iBrowser){    
        TFileName fname;
        fname.Format(KContentFile,id);
        
        HBufC8 
* buf=ReadFileLC(fname);
            
        _LIT(KURL,
"data:%d");
        TBuf
<32> url;
        url.Format(KURL,id);
        
        _LIT8(KDataType, 
"text/html");
        TDataType dataType(KDataType());
        TUid uid;
        uid.iUid 
= KCharacterSetIdentifierUtf8;
            
        iBrowser
->LoadDataL(url,*buf,dataType,uid);
        CleanupStack::PopAndDestroy();
    }

}

这里的URL用data:// 开头主要是用于历史记录作个标签罢了。而内容格式是text/html,不过要换成TDataType类型。而字符集使用UTF8。

我试了一下,觉得加载到内存再显示的效果比直接加载文件要快(主要是指切换页面时)。

另外,这个控件有个BUG,在退出时会有内存泄露,按网上的说法,在构造后激活它即可,但是我试了也没有效果?!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值