简单自定义控件-在view下可以运行-在传统模式下运行显示空白
简单自定义控件-在view下可以运行-在传统模式下运行显示空白
问题描述
我写了一个自定义的控件
头文件
#include <coecntrl.h> class CSimControl : public CCoeControl { public: static CSimControl* NewL(const TRect& aRect,const CCoeControl* aParent); static CSimControl* NewLC(const TRect& aRect,const CCoeControl* aParent); public: CSimControl(); virtual ~CSimControl(); void Draw(const TRect& aRect) const; void SizeChanged(); private: void ConstructL(const TRect& aRect,const CCoeControl* aParent ); }; |
代码文件
#include "SimControl.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CSimControl::CSimControl() { } CSimControl::~CSimControl() { } CSimControl* CSimControl::NewL( const TRect& aRect,const CCoeControl* aParent ) { CSimControl* self = CSimControl::NewLC(aRect,aParent); CleanupStack::Pop(); return self; } CSimControl* CSimControl::NewLC( const TRect& aRect,const CCoeControl* aParent ) { CSimControl* self = new(ELeave)CSimControl(); CleanupStack::PushL(self); self->ConstructL(aRect,aParent); return self; } void CSimControl::Draw( const TRect& aRect ) const { CWindowGc& gc = SystemGc(); gc.Clear(aRect); gc.SetPenStyle(CGraphicsContext::ESolidPen); gc.SetPenColor(KRgbBlack); gc.SetBrushStyle(CGraphicsContext::ESolidBrush); gc.SetBrushColor(KRgbBlack); gc.DrawEllipse(aRect); } void CSimControl::SizeChanged() { } void CSimControl::ConstructL(const TRect& aRect,const CCoeControl* aParent ) { if (aParent == NULL) { CreateWindowL(); } else { SetContainerWindowL(*aParent); } SetRect(aRect); ActivateL(); } |
我在 view 下可以显示这个控件,但是在传统模式下无法运行,传统模式下的代码
#include "SimControlLX2Container.h" #include <eiklabel.h> // for example label control // ================= MEMBER FUNCTIONS ======================= // --------------------------------------------------------- // CSimControlLX2Container::ConstructL(const TRect& aRect) // EPOC two phased constructor // --------------------------------------------------------- // void CSimControlLX2Container::ConstructL(const TRect& aRect) { CreateWindowL(); iLabel = new (ELeave) CEikLabel; iLabel->SetContainerWindowL( *this ); iLabel->SetTextL( _L("Example View") ); iToDoLabel = new (ELeave) CEikLabel; iToDoLabel->SetContainerWindowL( *this ); iToDoLabel->SetTextL( _L("Add Your controls\n here") ); iSimControl = CSimControl::NewL(aRect,NULL); iSimControl->SetContainerWindowL(*this); SetRect(aRect); ActivateL(); } // Destructor CSimControlLX2Container::~CSimControlLX2Container() { delete iLabel; delete iToDoLabel; delete iSimControl; } // --------------------------------------------------------- // CSimControlLX2Container::SizeChanged() // Called by framework when the view size is changed // --------------------------------------------------------- // void CSimControlLX2Container::SizeChanged() { // TODO: Add here control resize code etc. iLabel->SetExtent( TPoint(10,10), iLabel->MinimumSize() ); iToDoLabel->SetExtent( TPoint(10,100), iToDoLabel->MinimumSize() ); } // --------------------------------------------------------- // CSimControlLX2Container::CountComponentControls() const // --------------------------------------------------------- // TInt CSimControlLX2Container::CountComponentControls() const { return 3; // return nbr of controls inside this container } // --------------------------------------------------------- // CSimControlLX2Container::ComponentControl(TInt aIndex) const // --------------------------------------------------------- // CCoeControl* CSimControlLX2Container::ComponentControl(TInt aIndex) const { switch ( aIndex ) { case 0: return iLabel; case 1: return iToDoLabel; case 2: return iSimControl; default: return NULL; } } // --------------------------------------------------------- // CSimControlLX2Container::Draw(const TRect& aRect) const // --------------------------------------------------------- // void CSimControlLX2Container::Draw(const TRect& aRect) const { CWindowGc& gc = SystemGc(); // TODO: Add your drawing code here // example code... gc.SetPenStyle( CGraphicsContext::ENullPen ); gc.SetBrushColor( KRgbGray ); gc.SetBrushStyle( CGraphicsContext::ESolidBrush ); gc.DrawRect( aRect ); } // --------------------------------------------------------- // CSimControlLX2Container::HandleControlEventL( // CCoeControl* aControl,TCoeEvent aEventType) // --------------------------------------------------------- // void CSimControlLX2Container::HandleControlEventL( CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/) { // TODO: Add your control event handler code here } // End of File |
运行后,屏幕一片空白
找了好长时间也不有找到原因,后来在网友的帮助下终于找到原因了
就是在 Container 中的 SizeChange 方法也要添加代码
void CSimControlLX2Container::SizeChanged() { // TODO: Add here control resize code etc. iLabel->SetExtent( TPoint(10,10), iLabel->MinimumSize() ); iToDoLabel->SetExtent( TPoint(10,100), iToDoLabel->MinimumSize() ); TSize aSize; aSize.SetSize(200,200); iSimControl->SetExtent(TPoint(10,10),aSize); } |
加粗部分是
现在还有一个缺点是,没有画全,不过没有关系,这个应该在后面就可以解决了
终于走出了一步了