imuserlogincontainer.cpp

来自「国内著名嵌入式培训机构内部资料,内含一些实例代码,包括技术专题书籍」· C++ 代码 · 共 206 行

CPP
206
字号
/*
 ============================================================================
 Author	  : hou maoqing
 Version	 : 1.0
 Copyright   : Copyright (c) Hou maoqing 2008
 Mail	: houmqing@163.com
 ============================================================================
 */

#include "ImUserLoginContainer.h"
#include <ImClientExam.rsg>
#include <StringLoader.h>
#include <eiklabel.h>  
#include <barsread.h>  // for resource reader
#include <eikedwin.h>  // for CEikEdwin
#include <eikseced.h>       // for secret editor
#include <EIKENV.H>


// ================= MEMBER FUNCTIONS =======================

// ---------------------------------------------------------
// CImUserLoginContainer::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void CImUserLoginContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();

    iLabelUsercode = new (ELeave) CEikLabel;
    iLabelUsercode->SetContainerWindowL( *this );
    HBufC* pUsercode=StringLoader::LoadLC(R_LOGIN_USER_CODE);
    iLabelUsercode->SetTextL( *pUsercode );
    CleanupStack::PopAndDestroy(1);

    iLabelPassword = new (ELeave) CEikLabel;
    iLabelPassword->SetContainerWindowL( *this );
    HBufC* pPassword=StringLoader::LoadLC(R_LOGIN_PASSWORD);
    iLabelPassword->SetTextL( *pPassword );
    CleanupStack::PopAndDestroy(1);
    
    TResourceReader reader;
    iCoeEnv->CreateResourceReaderLC( reader, R_LOGIN_USER_CODE_EDWIN );
    iEdwin = new ( ELeave ) CEikEdwin;
    iEdwin->SetContainerWindowL( *this );
    iEdwin->ConstructFromResourceL( reader );
    CleanupStack::PopAndDestroy();  // Resource reader
    iEdwin->SetAknEditorCurrentInputMode(EAknEditorNumericInputMode);
    
    iCoeEnv->CreateResourceReaderLC( reader,R_LOGIN_USER_SECRET_EDITOR );
    iSecretEditor = new ( ELeave ) CEikSecretEditor;
    iSecretEditor->SetContainerWindowL( *this );
    iSecretEditor->ConstructFromResourceL( reader );
    CleanupStack::PopAndDestroy();  // Resource reader
    
    m_nFocusPos=0;
    MoveFocus();
    
    SetRect(aRect);
    ActivateL();
    }

// Destructor
CImUserLoginContainer::~CImUserLoginContainer()
    {
    delete iLabelUsercode;
    delete iLabelPassword;
    delete iEdwin;
    delete iSecretEditor;
    }

// ---------------------------------------------------------
// CImUserLoginContainer::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void CImUserLoginContainer::SizeChanged()
    {
    const CFont* pFont = CEikonEnv::Static()->DenseFont();
    int nLineHeight=pFont->FontMaxHeight()+6;
    
    TPoint ptPos(10,10);
    iLabelUsercode->SetExtent( ptPos, iLabelUsercode->MinimumSize() );
    
    ptPos.iY+=nLineHeight;
    iEdwin->SetExtent( ptPos, TSize(170,nLineHeight) );
    
    ptPos.iY+=nLineHeight+10;
    iLabelPassword->SetExtent( ptPos, iLabelPassword->MinimumSize() );
    
    ptPos.iY+=nLineHeight;
    iSecretEditor->SetExtent( ptPos, TSize(170,nLineHeight) );
    }

// ---------------------------------------------------------
// CImUserLoginContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CImUserLoginContainer::CountComponentControls() const
    {
    return 4; // return nbr of controls inside this container
    }

// ---------------------------------------------------------
// CImUserLoginContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CImUserLoginContainer::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
            return iLabelUsercode;
        case 1:
            return iLabelPassword;
        case 2:
        	return iEdwin;
        case 3:
            return iSecretEditor;
        default:
            return NULL;
        }
    }

// ---------------------------------------------------------
// CImUserLoginContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CImUserLoginContainer::Draw(const TRect& aRect) const
    {
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle( CGraphicsContext::ENullPen );
    gc.SetBrushColor( KRgbGray );
    gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
    gc.DrawRect( aRect );
    }

// ---------------------------------------------------------
// CImUserLoginContainer::HandleControlEventL(
//     CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CImUserLoginContainer::HandleControlEventL(
    CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
    {
    
    }

TKeyResponse CImUserLoginContainer::OfferKeyEventL(
		const TKeyEvent& aKeyEvent,TEventCode aType)
	{
	TKeyResponse r = EKeyWasNotConsumed;
	if (aType == EEventKey)
		{	    	
		if(aKeyEvent.iCode==EKeyUpArrow)
			{
			m_nFocusPos--;
			if(m_nFocusPos<0)
				m_nFocusPos=0;
			MoveFocus();
			r=EKeyWasConsumed;
			}
		else if(aKeyEvent.iCode==EKeyDownArrow)
			{
			m_nFocusPos++;
			if(m_nFocusPos>=2)
				m_nFocusPos=1;
			MoveFocus();
			r=EKeyWasConsumed;
			}
		}
	
	if(r==EKeyWasConsumed)
		return r;
	
	if(iEdwin->IsFocused())
		return iEdwin->OfferKeyEventL(aKeyEvent,aType);
	else if(iSecretEditor->IsFocused())
		return iSecretEditor->OfferKeyEventL(aKeyEvent,aType);
	
	return CCoeControl::OfferKeyEventL(aKeyEvent,aType);
	}

void CImUserLoginContainer::MoveFocus()
	{
	if(m_nFocusPos==0)
		{
		this->iEdwin->SetFocus(ETrue);
		this->iSecretEditor->SetFocus(EFalse);
		}
	else if(m_nFocusPos==1)
		{
		this->iEdwin->SetFocus(EFalse);
		this->iSecretEditor->SetFocus(ETrue);
		}
	}

void CImUserLoginContainer::GetUserAccAndPassword(TDes& desUserAcc,TDes& desPassword)
	{
	iEdwin->GetText(desUserAcc);
	iSecretEditor->GetText(desPassword);
	}

// End of File  

⌨️ 快捷键说明

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