⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 point24container.cpp

📁 symbian平台下的24点游戏编程 很适合初学者的例子
💻 CPP
字号:
/*
========================================================================
 Name        : Point24Container.cpp
 Author      : luomao2000
 Copyright   : luomao2000@tom.com
Reserved
 Description : 
========================================================================
*/
#include <barsread.h>
#include <stringloader.h>
#include <gdi.h>
#include <eikedwin.h>
#include <eikenv.h>
#include <eiklabel.h>
#include <aknviewappui.h>
#include <eikappui.h>
#include <Point24.rsg>
#include <aknsskininstance.h> 
#include <aknsutils.h> 
#include <aknsdrawutils.h> 

#include "Point24Container.h"
#include "Point24ContainerView.h"
#include "Point24.hrh"
#include "Point24Container.hrh"

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CPoint24Container::CPoint24Container()
	{
	iEditResult = NULL;
	iLabelNumber = NULL;
	iEditNumber = NULL;
	iSkinContext = NULL;
	iPoint24 = NULL;
	}
/** 
 * Destroy child controls.
 */
CPoint24Container::~CPoint24Container()
	{
	delete iEditResult;
	iEditResult = NULL;
	delete iLabelNumber;
	iLabelNumber = NULL;
	delete iEditNumber;
	iEditNumber = NULL;
	delete iSkinContext;
	iSkinContext = NULL;
	delete iPoint24;
	iPoint24 = NULL;
	}
				
/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CPoint24Container
 */
CPoint24Container* CPoint24Container::NewL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CPoint24Container* self = CPoint24Container::NewLC( 
			aRect, 
			aParent, 
			aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CPoint24Container
 */
CPoint24Container* CPoint24Container::NewLC( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CPoint24Container* self = new ( ELeave ) CPoint24Container();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}
			
/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */ 
void CPoint24Container::ConstructL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	if ( !aParent )
	    {
		CreateWindowL();
	    }
	else
	    {
	    SetContainerWindowL( *aParent );
	    }
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	iSkinContext = CAknsBasicBackgroundControlContext::NewL( KAknsIIDQsnBgAreaMain , aRect, EFalse);
	SetRect( aRect );
	iPoint24 = CPoint24::NewL();
	iPoint24->CalculateL(1, 2, 3, 4, iEditResult);
	ActivateL();
	}
			
/**
* Return the number of controls in the container (override)
* @return count
*/
TInt CPoint24Container::CountComponentControls() const
	{
	return ( int ) ELastControl;
	}
				
/**
* Get the control with the given index (override)
* @param aIndex Control index [0...n) (limited by #CountComponentControls)
* @return Pointer to control
*/
CCoeControl* CPoint24Container::ComponentControl( TInt aIndex ) const
	{
	switch ( aIndex )
		{
		case EEdit1:
			return iEditResult;
		case ELabelNumber:
			return iLabelNumber;
		case EEditNumber:
			return iEditNumber;
		}
	// handle any user controls here...
	
	return NULL;
	}
				
/**
 *	Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */				
void CPoint24Container::SizeChanged()
	{
	CCoeControl::SizeChanged();
	iSkinContext->SetRect(Rect());
	LayoutControls();
	}
				
/**
 * Layout components as specified in the UI Designer
 */
void CPoint24Container::LayoutControls()
	{
	TRect rect(Rect());
	TSize sz(iLabelNumber->MinimumSize());
	iEditResult->SetExtent( TPoint( rect.iTl.iX + 4, rect.iTl.iY + 4 ), 
	                   TSize( rect.Width() - 16, rect.Height() - sz.iHeight - 12 ) );
	iLabelNumber->SetExtent( TPoint( rect.iTl.iX + 4, rect.iBr.iY - sz.iHeight - 4 ), sz );
	iEditNumber->SetExtent( TPoint( rect.iTl.iX + 4 + sz.iWidth + 4, rect.iBr.iY - sz.iHeight - 4 ), 
	                   TSize( rect.iBr.iX - (rect.iTl.iX + 4 + sz.iWidth + 4) - 4, sz.iHeight ) );
	}

/**
 *	Handle key events.
 */				
TKeyResponse CPoint24Container::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
    TBuf<1> space(_L(""));
    if (iEditNumber->TextLength() < 8 && iEditResult->TextLength() > 0)
        {
        iEditResult->SetTextL(&space);
        iEditResult->DrawDeferred();
        }
	iEditNumber->SetFocus(ETrue);
    iEditNumber->SetCursorPosL(iEditNumber->TextLength(), EFalse);
	if (aType == EEventKey)
	    {
	    // handle scroll event
    	if ( aKeyEvent.iCode == EKeyDownArrow || aKeyEvent.iCode == EKeyPageDown )
    	    {
    	    TKeyEvent key(aKeyEvent);
    	    key.iCode = EKeyPageDown;
    	    iEditResult->OfferKeyEventL(key, aType);
    	    return EKeyWasConsumed;
    	    }
    	else if ( aKeyEvent.iCode == EKeyUpArrow || aKeyEvent.iCode == EKeyPageUp )
    	    {
    	    TKeyEvent key(aKeyEvent);
    	    key.iCode = EKeyPageUp;
    	    iEditResult->OfferKeyEventL(key, aType);
    	    return EKeyWasConsumed;
    	    }
    	// handle number input event
       	if ( aKeyEvent.iCode > '0' && aKeyEvent.iCode <= '9' )
       	    {
       	    // number
       	    if (iEditNumber->TextLength() > 6)
       	        {
       	        iEditNumber->SetTextL(&space);
       	        iEditNumber->HandleTextChangedL();
       	        iEditNumber->SetCursorPosL(0, EFalse);
       	        }
    		if ( iEditNumber->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
    		    {
    		    TKeyEvent key(aKeyEvent);
    		    key.iCode = 0x20;
    		    iEditNumber->OfferKeyEventL( key, aType );
    		    
           	    if (iEditNumber->TextLength() > 6)
           	        {
           	        // 4 numbers are inputed
           	        TBuf<10> s;
           	        iEditNumber->GetText(s);
           	        TInt value1(s[0] - 0x30);
           	        TInt value2(s[2] - 0x30);
           	        TInt value3(s[4] - 0x30);
           	        TInt value4(s[6] - 0x30);
           	        iPoint24->CalculateL(value1, value2, value3, value4, iEditResult);
           	        iEditResult->HandleTextChangedL();
           	        }
    		    return EKeyWasConsumed;
    		    }
       	    }
       	else if (aKeyEvent.iCode == EKeyBackspace) 
       	    {
       	    // backspace
       	    iEditNumber->OfferKeyEventL( aKeyEvent, aType );
       	    iEditNumber->OfferKeyEventL( aKeyEvent, aType );
       	    return EKeyWasConsumed;
       	    }
   	    }
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
	}
				
/**
 *	Initialize each control upon creation.
 */				
void CPoint24Container::InitializeControlsL()
	{
	iEditResult = new ( ELeave ) CEikEdwin;
	iEditResult->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_POINT24_CONTAINER_EDIT_RESULT );
		iEditResult->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iLabelNumber = new ( ELeave ) CEikLabel;
	iLabelNumber->SetContainerWindowL( *this );
	iLabelNumber->SetFont(iEikonEnv->NormalFont());
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_POINT24_CONTAINER_LABEL_NUMBER );
		iLabelNumber->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iEditNumber = new ( ELeave ) CEikEdwin;
	iEditNumber->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_POINT24_CONTAINER_EDIT_NUMBER );
		iEditNumber->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
		{
		HBufC* text = StringLoader::LoadLC( R_POINT24_CONTAINER_EDIT_NUMBER_TEXT );
		iEditNumber->SetTextL( text );
	    iEditNumber->SetCursorPosL(iEditNumber->TextLength(), EFalse);
		CleanupStack::PopAndDestroy( text );
		}
	
	iEditNumber->SetFocus( ETrue );
	}

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CPoint24Container::HandleResourceChange( TInt aType )
	{
	CCoeControl::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( EPoint24ContainerViewId ) )->ClientRect() );
	}
				
/**
 *	Draw container contents.
 */				
void CPoint24Container::Draw( const TRect& aRect ) const
	{
	CWindowGc& gc = SystemGc();
	MAknsSkinInstance* skin = AknsUtils::SkinInstance();
	AknsDrawUtils::Background(skin, iSkinContext, this, gc, aRect);
	}
				

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -