Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

自定义按钮

From Wiki
Jump to: navigation, search
Article Metadata

文章
翻译:
翻译自: Custom button
hoolee
最后由 hamishwillee 在 30 May 2013 编辑

这里讲述了如何生成一个自定义的图片或label按钮控件。它可以被点击,当焦点在按钮控件时,可以按OK键点击。这时控件会通知observer有了点击事件。

这个主要是为了那些从UIQ移植到S60,并缺少按钮控件的程序准备的。


需要的头文件:

#include <gulicon.h> //CGulIcon
#include <fbs.h> //CFbsBitmap
#include <COECNTRL.H> //CCoeControl
#include <coecobs.h> //MCoeControlObserver
#include <aknscontrolcontext.h> //MAknsControlContext

Library required:

LIBRARY   egul.lib //CGulIcon
LIBRARY fbscli.lib //CFbsBitmap

Here is the header file

class CCustomButtonControl;
 
class MCoeButtonControlObserver
{
public:
virtual void HandleButtonClickedEventL(
const CCustomButtonControl* aControl)=0;
};
 
class CCustomButtonControl: public CCoeControl, MCoeControlObserver
{
public:
 
static CCustomButtonControl* NewL(const TRect& aRect,
MCoeButtonControlObserver*
aButtonObserver,
const CCoeControl* aParent);
~CCustomButtonControl();
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aType);
void HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType);
 
CCustomButtonControl(const TRect& aRect):iRect(aRect)
{
}
 
TTypeUid::Ptr MopSupplyObject(TTypeUid aId);
 
void Draw(const TRect& aRect) const;
// takes the ownership
void SetFocusedBackgroundImage(CFbsBitmap* aBackgroundImage)
{
iFocusedBackgroundImage=aBackgroundImage;
}
// takes the ownership
void SetDeFocusedBackgroundImage(CFbsBitmap* aBackgroundImage)
{
iDeFocusedBackgroundImage=aBackgroundImage;
}
 
void SetIcon(CGulIcon* aIcon) // takes the ownership
{
if(iIcon)
{
delete iIcon;
}
iIcon=aIcon;
}
 
void SetLabel(const TDesC& aDes)
{
iLabel.Delete(0,iLabel.Length());
iLabel.FillZ();
iLabel.Copy(aDes.Left(50));
}
 
void MakeVisible(TBool aVisible)
{
CCoeControl::MakeVisible(aVisible);
}
 
void SetFocus(TBool aFocus)
{
iFocused=aFocus;
DrawDeferred();
}
 
TBool IsFocused()
{
return iFocused;
}
private:
void ConstructL(const TRect& aRect,
MCoeButtonControlObserver* aButtonObserver,
const CCoeControl* aParent);
private:
MAknsControlContext* iBackGround;
MCoeButtonControlObserver* iButtonObserver;
CFbsBitmap* iFocusedBackgroundImage;
CFbsBitmap* iDeFocusedBackgroundImage;
TRect iRect;
CGulIcon* iIcon;
TBuf<50> iLabel;
TBool iFocused;
};

源文件

#include "aknappui.h"
#include "aknsbasicbackgroundcontrolcontext.h"
 
CCustomButtonControl* CCustomButtonControl::NewL(const TRect& aRect,
MCoeButtonControlObserver* aButtonObserver,
const CCoeControl* aParent)
{
CCustomButtonControl* self = new(ELeave) CCustomButtonControl(aRect);
CleanupStack::PushL(self);
self->ConstructL(aRect,aButtonObserver,aParent);
CleanupStack::Pop(); // self
return self;
}
 
CCustomButtonControl::~CCustomButtonControl()
{
if(iBackGround)
{
delete iBackGround;
iBackGround = NULL;
}
if(iFocusedBackgroundImage)
{
delete iFocusedBackgroundImage;
iFocusedBackgroundImage=NULL;
}
if(iDeFocusedBackgroundImage)
{
delete iDeFocusedBackgroundImage;
iDeFocusedBackgroundImage=NULL;
}
 
if(iIcon)
{
delete iIcon;
}
}
 
void CCustomButtonControl::ConstructL(const TRect& aRect,
MCoeButtonControlObserver* aButtonObserver,
const CCoeControl* aParent)
{
iFocusedBackgroundImage=NULL;
iDeFocusedBackgroundImage=NULL;
iIcon=NULL;
iLabel.FillZ();
iFocused=EFalse;
if (aParent == NULL)
{
CreateWindowL();
iAvkonAppUi->AddToStackL( this);
}
else
{
// Part of a compound control, so just share
// the parent's window
SetContainerWindowL(*aParent);
}
if(aButtonObserver)
iButtonObserver=aButtonObserver;
else
iButtonObserver=NULL;
SetRect(aRect);
iBackGround = CAknsBasicBackgroundControlContext::NewL(
KAknsIIDQsnBgAreaMain, iRect, EFalse );
SetRect(aRect);
ActivateL();
 
}
 
TTypeUid::Ptr CCustomButtonControl::MopSupplyObject(TTypeUid aId)
{
if(aId.iUid == MAknsControlContext::ETypeId && iBackGround)
{
return MAknsControlContext::SupplyMopObject( aId, iBackGround);
}
return CCoeControl::MopSupplyObject( aId );
}
 
void CCustomButtonControl::HandleControlEventL(
CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
{
}
 
TKeyResponse CCustomButtonControl::OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aType)
{
TKeyResponse ret=EKeyWasNotConsumed;
if(iButtonObserver && IsFocused() && EEventKey == aType)
{
switch (aKeyEvent.iScanCode)
{
case EStdKeyDevice3:
{
iButtonObserver->HandleButtonClickedEventL(this);
ret=EKeyWasConsumed;
} break;
 
default:
break;
}
}
return ret;
}
 
 
void CCustomButtonControl::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
gc.Clear(aRect);
gc.SetPenStyle(CGraphicsContext::ESolidPen);
TSize pensize(1,1);
gc.SetPenSize(pensize);
gc.SetBrushColor(KRgbGray);
if(iFocused)
gc.SetPenColor(KRgbBlack);
else
gc.SetPenColor(KRgbGray);
TSize round(2,2);
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.DrawRoundRect(aRect,round);
 
gc.SetBrushStyle(CGraphicsContext::ENullBrush);
 
if(iDeFocusedBackgroundImage)
{
if(!iFocused)
{
TRect backImage(aRect);
backImage.Shrink(1,1);
gc.DrawBitmap(backImage, iDeFocusedBackgroundImage);
}
else if(iFocusedBackgroundImage)
{
TRect backImage(aRect);
backImage.Shrink(1,1);
gc.DrawBitmap(backImage, iFocusedBackgroundImage);
}
}
else if(iFocusedBackgroundImage)
{
TRect backImage(aRect);
backImage.Shrink(1,1);
gc.DrawBitmap(backImage, iFocusedBackgroundImage);
}
 
CFbsBitmap* image=NULL;
TRect imageRect;
if(iIcon)
{
image = iIcon->Bitmap();
if(image)
{
if(iLabel.Length()>0)
{
imageRect.SetRect(
aRect.iTl.iX+5,
(aRect.Height() - image->Header().iSizeInPixels.iHeight)/2,
aRect.iTl.iX+5+aRect.Height() -
image->Header().iSizeInPixels.iWidth,
((aRect.Height()-image->Header().iSizeInPixels.iHeight)/2) +
aRect.Height()-image->Header().iSizeInPixels.iHeight);
}
else
{
TInt Tx=aRect.iTl.iX;
TInt Ty=aRect.iTl.iY;
TInt Bx=aRect.Width();
TInt By=aRect.Height();
if(image->Header().iSizeInPixels.iWidth<aRect.Width())
{
Tx+=(aRect.Width()/2) -
(image->Header().iSizeInPixels.iWidth/2);
Bx=Tx+image->Header().iSizeInPixels.iWidth;
}
 
if(image->Header().iSizeInPixels.iHeight<aRect.Height())
{
 
Ty+=(aRect.Height()/2) -
(image->Header().iSizeInPixels.iHeight/2);
By=Ty+image->Header().iSizeInPixels.iHeight;
}
imageRect.SetRect(Tx,Ty,Bx,By);
}
TRect rect;
rect.iTl.iX=0;
rect.iTl.iY=0;
rect.iBr.iX=imageRect.Width();
rect.iBr.iY=imageRect.Height();
gc.BitBltMasked(imageRect.iTl,image,
rect,iIcon->Mask(),EFalse);
//gc.DrawBitmap(imageRect,image);
}
}
 
if(iLabel.Length()>0)
{
gc.SetPenStyle(CGraphicsContext::ESolidPen);
 
const CFont* font = CEikonEnv::Static()->LegendFont();
gc.UseFont(font);
TRect textRect(aRect);
TInt baseline;
if(image)
{
textRect.iTl.iX+=image->Header().iSizeInPixels.iWidth+2;
baseline=imageRect.iBr.iY-4;
}
else
{
baseline = textRect.Height()/2;
}
gc.DrawText(iLabel,textRect, baseline, CGraphicsContext::ECenter, 1);
}
}

Screenshot.jpg

This page was last modified on 30 May 2013, at 04:37.
52 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×