首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> Symbian >> Symbian入门 >> 正文
Symbian学习笔记(14)——使用Browser Control API
作者:SHARE & …    文章来源:SHARE & TOP    更新时间:2008-4-19 11:06:55
再把这个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,在退出时会有内存泄露,按网上的说法,在构造后激活它即可,但是我试了也没有效果?!
相关文章:
Symbian学习笔记(22)——关于皮肤的小结
Symbian学习笔记(21)——原来还有这个工具wsdl2cpp,访问webservice也很简单
Symbian学习笔记(20)——用gSOAP更简单地实现Web Services Client
Symbian学习笔记(19)——初探WebServices API的使用(下)
Symbian学习笔记(18)——初探WebServices API的使用(中)
Symbian学习笔记(17)——初探WebServices API的使用(上)
Symbian学习笔记(16)——解析XML文件(下)
Symbian学习笔记(15)——解析XML文件(上)
 

站点地图 | 加入收藏 | 联系站长 | 广告服务 |
QQ:280529124  Tel:0592-8271361 辽ICP备05021703号