Symbian學習筆記(14)
而我在UniNews中只使用了它最基本的用法,下面給出代碼:
首先,在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控件,其它兩個是構(gòu)造時的所需要的參數(shù)。而這個類派生于接口 MBrCtlLoadEventObserver,所以實現(xiàn)它的方法void HandleBrowserLoadEventL(TBrCtlDefs::TBrCtlLoadEvent aLoadEvent,TUint aSize,TUint16 aTransactionId);
在實現(xiàn)文件CPP中,我們需要構(gòu)造它:
// 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();
}
在構(gòu)造函數(shù)中我們初始化那兩個參數(shù):
// No implementation required
iCapabilities=TBrCtlDefs::ECapabilityDisplayScrollBar|TBrCtlDefs::ECapabilityLoadHttpFw;
iCommandBase=TBrCtlDefs::ECommandIdBase;
iBrowser=NULL;
}
刪除的時候記得將它的事件監(jiān)聽器都注銷掉:
// No implementation required
if(iBrowser)...{
iBrowser->RemoveLoadEventObserver(this);
}
delete iBrowser;
iBrowser=NULL;
}
此外,它跟其它控件一樣,在Resize時要處理一下,并且它也需要聲明自己是一個組件等等的。
而方法HandleBrowserLoadEventL只需要簡單地重繪一下即可。
真正的使用在這兒呢,很簡單:
...{
if(iBrowser)...{
TFileName fname;
fname.Format(KContentFile,id);
iBrowser->LoadUrlL(fname);
}
}
就是一句話 LoadUrlL就可以了,這個URL可以是http:// 也可以是 file://,很方便。
不過經(jīng)常我們是需要將內(nèi)存里的內(nèi)容加載顯示出來,那就稍稍多做一點工作:
...{
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:// 開頭主要是用于歷史記錄作個標簽罷了。而內(nèi)容格式是text/html,不過要換成TDataType類型。而字符集使用UTF8。
我試了一下,覺得加載到內(nèi)存再顯示的效果比直接加載文件要快(主要是指切換頁面時)。
另外,這個控件有個BUG,在退出時會有內(nèi)存泄露,按網(wǎng)上的說法,在構(gòu)造后激活它即可,但是我試了也沒有效果!
【編輯推薦】