Symbian中也可以开发内嵌浏览器控件,而且使用起来比较简单,效果也还可以接受。在SDK的example中,有个例子可以参考BrCtlSampleApp,里面有使用BrowserCtl的各种方式。
简单点说,Symbian提供了个class CBrCtlInterface,在browserengine.lib中。下面把我写的一个简单的控件代码贴上。
#include <charconv.h>
#include <utf.h>
#include "BroswerCtl.h"
void CBroswerCtl::ConstructL(const TRect& aRect)
{
CreateWindowL();
SetRect(aRect);
ActivateL();
iCommandBase = TBrCtlDefs::ECommandIdBase;
iBrCtlCapabilities = TBrCtlDefs::ECapabilityDisplayScrollBar | TBrCtlDefs::ECapabilityLoadHttpFw |
TBrCtlDefs::ECapabilityGraphicalHistory | TBrCtlDefs::ECapabilityGraphicalPage;
TRect rect(Position(), Size());
iBrCtlInterface = CreateBrowserControlL( this,
rect,
iBrCtlCapabilities,
iCommandBase,
NULL,
NULL,
NULL,
NULL,
NULL);
}
CBroswerCtl::~CBroswerCtl()
{
if (iBrCtlInterface)
{
delete iBrCtlInterface;
iBrCtlInterface = NULL;
}
}
TInt CBroswerCtl::CountComponentControls() const
{
if (iBrCtlInterface)
return 1;
return 0;
}
CCoeControl* CBroswerCtl::ComponentControl(TInt aIndex) const
{
switch (aIndex)
{
case 0:
return iBrCtlInterface; // Could be NULL
default:
return NULL;
}
}
void CBroswerCtl::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
gc.SetPenStyle(CGraphicsContext::ESolidPen);
gc.SetBrushColor(KRgbGray);
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.DrawRect(aRect);
gc.DiscardFont();
}
void CBroswerCtl::SizeChanged()
{
if (iBrCtlInterface)
{
iBrCtlInterface->SetRect(Rect());
}
}
void CBroswerCtl::LoadingContentWithBuffer(const TDesC& aBuffer)
{
_LIT(KHttpDummy, "http://dummy");
_LIT8(KDataType, "text/html");
HBufC8* temp8 = HBufC8::NewL(256);
TPtr8 bufferPtr(temp8->Des());
CnvUtfConverter::ConvertFromUnicodeToUtf8(bufferPtr, aBuffer);
TUid uid;
uid.iUid = KCharacterSetIdentifierIso88591;
TDataType dataType(KDataType);
iBrCtlInterface->LoadDataL(KHttpDummy, bufferPtr, dataType, uid);
}
void CBroswerCtl::LoadingContentWithFile(const TDesC& aFileName)
{
RFs rfs;
RFile file;
User::LeaveIfError(rfs.Connect());
CleanupClosePushL(rfs);
User::LeaveIfError(file.Open(rfs, aFileName, EFileShareReadersOnly));
CleanupClosePushL(file);
iBrCtlInterface->LoadFileL(file);
CleanupStack::PopAndDestroy(2); // file, rfs
}
void CBroswerCtl::LoadingContentWithUrl(const TDesC& aUrl)
{
iBrCtlInterface->LoadUrlL( aUrl );
}
void CBroswerCtl::CreateBasicBrowserControlL()
{
}
HBufC8* CBroswerCtl::ReadFileLC(const TDesC& aFileName)
{
RFs rfs;
RFile file;
User::LeaveIfError(rfs.Connect());
CleanupClosePushL(rfs);
User::LeaveIfError(file.Open(rfs, aFileName, EFileRead));
CleanupClosePushL(file);
TInt size;
User::LeaveIfError(file.Size(size));
HBufC8* buf = HBufC8::NewLC(size);
TPtr8 bufPtr(buf->Des());
User::LeaveIfError(file.Read(bufPtr));
CleanupStack::Pop(); // buf
CleanupStack::PopAndDestroy(2); // file, rfs
CleanupStack::PushL(buf);
return buf;
}
#ifndef BROSWERCTL_H
#define BROSWERCTL_H
#include <coecntrl.h>
#include "BrCtlInterface.h"
class CBroswerCtl : public CCoeControl
{
public:
void ConstructL(const TRect& aRect);
~CBroswerCtl();
TInt CountComponentControls() const;
CCoeControl* ComponentControl(TInt aIndex) const;
void Draw(const TRect& aRect) const;
void SizeChanged();
void LoadingContentWithBuffer(const TDesC& aBuffer);
void LoadingContentWithFile(const TDesC& aFileName);
void LoadingContentWithUrl(const TDesC& aUrl);
void CreateBasicBrowserControlL();
private:
HBufC8* ReadFileLC(const TDesC& aFileName);
private:
// Pointer to the browser control interface
CBrCtlInterface* iBrCtlInterface;
// Desired capabilities of the browser control
TUint iBrCtlCapabilities;
// Command Base
TInt iCommandBase;
};
#endif
其他View中把这个类作为一个控件使用即可。