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

📄 gridcontainer.cpp

📁 一个比较完整的有关移动开发的例子 vc++6.0下编译通过
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CGridContainer from CCoeControl, MCoeControlObserver
*  Part of  : Grid
*  Copyright (c) 2003 Nokia. All rights reserved.
* ============================================================================
*/

// INCLUDE FILES

#define MEM_FREE(p) if (NULL!=p){delete p; p = NULL;}
#include <FairyLand.rsg>
#include "FairyLand.hrh"
#include "GridContainer.h"
#include "FairyLandappui.h"
#include "Memoview.h"
#include "SMSFunctionView.h"
#include "BookListView.h"

#include <akngrid.h>	//for Grid
#include <e32base.h>		//for CArrayPtr
#include <AknIconArray.h>	//for CAknIconArray
#include <AknLists.h>		//for AknListBoxLayouts
#include <aknnotewrappers.h> 

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

// C++ default constructor can NOT contain any code, that
// might leave.
//
CGridContainer::CGridContainer()
{
	m_pGrid = NULL;
}

// EPOC default constructor can leave.
void CGridContainer::ConstructL(const TRect& aRect,CGridView* aView)
{
    CreateWindowL();
	m_pView=aView;
	//---begin-for-Grid----------------------
	m_pGrid = new( ELeave ) CAknGrid;	//创建一个Grid控制器实例/对象
	m_pGrid->SetContainerWindowL( *this );	//设置Grid所在的容器:把这个Grid放到容器中
	m_pGrid->ConstructL( this, EAknListBoxSelectionGrid );	//第二阶段构造
	m_pGrid->SetEmptyGridTextL(_L("empty"));	//设置Grid默认文本:当Grid的Text未设置时,显示默认字符串
	
	// set layout.
	m_pGrid->SetRect(aRect.Size());	//设置Grid的矩形显示区域
	
	// set initial params for the grid.
	
	m_pGridType = EAknExGridSelectionGrid;	//设置Grid的类型
	
	//Sets the orientation of the grid,either vertical or horizontal.
	iVerticalOrientation	= EFalse;	//排列方向:垂直方向为假,则是水平方向
	iTopToBottom			= ETrue;	//排列方式:从上倒下为真
	iLeftToRight			= ETrue;	//排列顺序:从左到右为真
	//the size of the primary dimension of the grid. 
	iNumOfColumns			= 3;	//列数
	iNumOfRows				= 3;	//行数
	iNumOfItems				= 9;	//项的总个数
	//the ordering of the data.
	iContentType			= EContainGraphic;	//内容类型:包含图片
	
	iVerticalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;
	iHorizontalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;
	iSizeOfItems.iWidth = ( aRect.iBr.iX - aRect.iTl.iX ) / iNumOfColumns;
	iSizeOfItems.iHeight = ( aRect.iBr.iY - aRect.iTl.iY ) / iNumOfRows;
	
	//Sets the orientation of the grid, either vertical or horizontal, the ordering of the data and the size of the primary dimension of the grid.
	m_pGrid->SetLayoutL( iVerticalOrientation, iLeftToRight, iTopToBottom, 
		iNumOfColumns, iNumOfRows, iSizeOfItems );
	
	//设置水平移动方向:当焦点水平移动到最右边时,是否自动移动到最左边
	m_pGrid->SetPrimaryScrollingType( iHorizontalScrollingType );
	
	//设置垂直移动方向:当焦点垂直移动到最下方时,是否自动移动到最上边
	m_pGrid->SetSecondaryScrollingType( iVerticalScrollingType );	
	
	//load data 
	LoadGraphicsL();
	AddDataL();
	iInstantsCreated = ETrue;
	
	// activate the grid
	m_pGrid->SetCurrentDataIndex(0);	//设置当前光标焦点位置
	m_pGrid->MakeVisible( ETrue );	//设置可见性
	m_pGrid->SetFocus( ETrue );	//设置焦点
	m_pGrid->ActivateL();	//激活Grid
	m_pGrid->DrawNow();	//画Grid
	//---end-for-Grid-------------------------
	
    SetRect(aRect);
    ActivateL();
}

// Destructor
CGridContainer::~CGridContainer()
{
	MEM_FREE(m_pGrid);
}

// ---------------------------------------------------------
// CGridContainer::FocusTo(TInt aCommand)
// Change foccused control.
// (other items were commented in a header).
// ---------------------------------------------------------
//

void CGridContainer::FocusTo(TInt aCommand)
{
	
}

// ---------------------------------------------------------
// CGridContainer::SizeChanged()
// Called by framework when the view size is changed
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CGridContainer::SizeChanged()
{
}

// ---------------------------------------------------------
// CGridContainer::CountComponentControls() const
// (other items were commented in a header).
// ---------------------------------------------------------
//
TInt CGridContainer::CountComponentControls() const
{
	return 1; // return nbr of controls inside this container
}

// ---------------------------------------------------------
// CGridContainer::ComponentControl(TInt aIndex) const
// (other items were commented in a header).
// ---------------------------------------------------------
//
CCoeControl* CGridContainer::ComponentControl(TInt aIndex) const
{
	return m_pGrid;
}

// ---------------------------------------------------------
// CGridContainer::Draw(const TRect& aRect) const
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CGridContainer::Draw(const TRect& aRect) const
{
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushColor(KRgbCyan);
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.DrawRect(aRect);
	
	// 	_LIT(KNAME,"Name:");
	// 	TBuf<32>buf(KNAME);
	// 	const CFont* nor =	CEikonEnv::Static()->TitleFont();
	// 	gc.UseFont(nor);
	// 
	// 	_LIT(KNAMEA,"Chenliang");
	// 	_LIT(KTEL, "13500000000");
	// 	_LIT(kEMAIL, "chenliang-6@163.com");
	// 	_LIT(KADD, "Beijing");
	// 	CAddressBook* m_pAddRess = CAddressBook::NewL(KNAMEA, KTEL, kEMAIL, KADD);
	// 
	// 	TPoint point(aRect.Width() / 2 - 50, aRect.Height() / 2);
	// //	gc.DrawText(buf, point);
	// 	point.iX += 35;
	// //	gc.DrawText(*(m_pAddRess->GetName()),point);
	// 	gc.DiscardFont();
	// 	m_pAddRess->Distroy();
	
}

// ---------------------------------------------------------
// CGridContainer::OfferKeyEventL(...)
// Notify key events to editors.
// (other items were commented in a header).
// ---------------------------------------------------------
//
TKeyResponse CGridContainer::OfferKeyEventL(
											const TKeyEvent& aKeyEvent, TEventCode aType)
{
				//return EKeyWasNotConsumed;
	if ((aKeyEvent.iCode == EKeyDevice3) && (aType == EEventKey))
	{
		CFairyLandAppUi* pApp=(CFairyLandAppUi*)CEikonEnv::Static()->AppUi();
		TInt m_nSelected=GetGrid();
		if (m_nSelected==0)
		{
			m_pView->ReadFileL();
			pApp->ActivateLocalViewL(KViewId4);
		}
		else if (m_nSelected==1)
		{
			pApp->ActivateLocalViewL(KViewId12);
		}
		else if (m_nSelected==2)
		{
			pApp->ActivateLocalViewL(KViewId10);
		}
		else
		{
			TBuf<32> GridWarn;
			CEikonEnv::Static()->ReadResource(GridWarn, R_QTN_MH_GRID_WARN);
			//显示读取的文件内容
			CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
			informationNote->ExecuteLD(GridWarn);					
		}
	}
	else if(NULL != m_pGrid)
	{
		return m_pGrid->OfferKeyEventL(aKeyEvent, aType);
	}
	else
	{
		return EKeyWasNotConsumed;
	}
}


// ---------------------------------------------------------
// CGridContainer::HandleControlEventL(
//     CCoeControl* aControl,TCoeEvent aEventType)
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CGridContainer::HandleControlEventL(
										 CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
{
}

//for Grid

//Set Icon and Mask for the Grid.
void CGridContainer::LoadGraphicsL()
{
	CFairyLandAppUi* pApp = (CFairyLandAppUi*)CEikonEnv::Static()->AppUi();
	
    CArrayPtr< CGulIcon >* icons = new(ELeave) CAknIconArray(iNumOfItems);
    CleanupStack::PushL( icons );
	
    // Create icon bitmap and mask.
    if ( m_pGridType == EAknExGridSelectionGrid )
	{
		icons->AppendL(pApp->LoadGraphicsL(0));
		icons->AppendL(pApp->LoadGraphicsL(1));
		icons->AppendL(pApp->LoadGraphicsL(2));
		icons->AppendL(pApp->LoadGraphicsL(1));
		icons->AppendL(pApp->LoadGraphicsL(1));
		icons->AppendL(pApp->LoadGraphicsL(1));
		icons->AppendL(pApp->LoadGraphicsL(1));
		icons->AppendL(pApp->LoadGraphicsL(1));
		icons->AppendL(pApp->LoadGraphicsL(1));
		
	}
	
    // Set icon array.
    m_pGrid->ItemDrawer()->FormattedCellData()->SetIconArrayL( icons );
    CleanupStack::Pop(); // icons array
}

//获得当前九宫格的位置
TInt CGridContainer::GetGrid() const      
{
	return m_pGrid->CurrentItemIndex();
}

//Set Data for the Grid.
void CGridContainer::AddDataL()
{
    MDesCArray* array = m_pGrid->Model()->ItemTextArray();
    CDesCArray* cArray = ( CDesCArray* )array;
	
	TBuf<32> sTmpGrid1;
	CEikonEnv::Static()->ReadResource(sTmpGrid1, R_QTN_MH_MEMO);
	TBuf<32> sTmpGrid2;
	CEikonEnv::Static()->ReadResource(sTmpGrid2, R_QTN_MH_SHORT_MESSAGE);
	TBuf<32> sTmpGrid3;
	CEikonEnv::Static()->ReadResource(sTmpGrid3, R_QTN_MH_ADDRESS_LIST);
	TBuf<32> sTmpGrid4;
	CEikonEnv::Static()->ReadResource(sTmpGrid4, R_QTN_MH_FAVORITE);
	TBuf<32> sTmpGrid5;
	CEikonEnv::Static()->ReadResource(sTmpGrid5, R_QTN_MH_CUSTOMER_SERVICE);
	TBuf<32> sTmpGrid6;
	CEikonEnv::Static()->ReadResource(sTmpGrid6, R_QTN_MH_MOBILE_WEB);
	TBuf<32> sTmpGrid7;
	CEikonEnv::Static()->ReadResource(sTmpGrid7, R_QTN_MH_CONFIG);
	TBuf<32> sTmpGrid8;
	CEikonEnv::Static()->ReadResource(sTmpGrid8, R_QTN_MH_MOBILE_SERVICE);
	TBuf<32> sTmpGrid9;
	CEikonEnv::Static()->ReadResource(sTmpGrid9, R_QTN_MH_TOOL);
	
	TBuf<32> sBuf;
	//SMS
	sBuf.Copy(_L("0\t"));
	sBuf.Append(sTmpGrid1);
	cArray->AppendL(sBuf);
	//PHB
	sBuf.Copy(_L("1\t"));
	sBuf.Append(sTmpGrid2);
	cArray->AppendL(sBuf);
	//Account
	sBuf.Copy(_L("2\t"));
	sBuf.Append(sTmpGrid3);
	cArray->AppendL(sBuf);
	//SMS
	sBuf.Copy(_L("3\t"));
	sBuf.Append(sTmpGrid4);
	cArray->AppendL(sBuf);
	//PHB
	sBuf.Copy(_L("4\t"));
	sBuf.Append(sTmpGrid5);
	cArray->AppendL(sBuf);
	//Account
	sBuf.Copy(_L("5\t"));
	sBuf.Append(sTmpGrid6);
	cArray->AppendL(sBuf);
	//SMS
	sBuf.Copy(_L("6\t"));
	sBuf.Append(sTmpGrid7);
	cArray->AppendL(sBuf);
	//PHB
	sBuf.Copy(_L("7\t"));
	sBuf.Append(sTmpGrid8);
	cArray->AppendL(sBuf);
	//Account
	sBuf.Copy(_L("8\t"));
	sBuf.Append(sTmpGrid9);
	cArray->AppendL(sBuf);
	
	//const CFont* fontText = ApacPlain12();
    m_pGrid->HandleItemAdditionL();
    ApplySelGridGraphicStyleL();
}

void CGridContainer::ApplySelGridGraphicStyleL()
{
    if (!m_pGrid) 
	{
        return;
	}
	
	//Setup text foreground and background colours to default
    AknListBoxLayouts::SetupStandardGrid( *m_pGrid );
	
	//获取中文字体
	const CFont* fontText = ApacPlain12();
	//
	TInt nX = (m_pGrid->ColumnWidth() - 24) / 2;
	TInt nY = (m_pGrid->ItemHeight() - 24 - fontText->HeightInPixels()) / 2;
	
	//Setup a single graphics cell of list item.
    AknListBoxLayouts::SetupFormGfxCell( 
		*m_pGrid, 
		m_pGrid->ItemDrawer(),
		0, 
		nX, 
		nY, 
		0, 
		0,  
		24, 
		24, 
		TPoint( nX, nY ), 
		TPoint( 24+nX, 24+nY) );
	
	//Setup a single text cell of list item.
	// layout of text.
	//This has flicker for settings.
	// 	AknListBoxLayouts::SetupFormTextCell( 
	// 		*m_pGrid, 
	// 		m_pGrid->ItemDrawer(),
	// 		1, 
	// 		fontText, 
	// 		215, 
	// 		0, 
	// 		0, 
	// 		24+nY+fontText->HeightInPixels(),
	// 		m_pGrid->ColumnWidth(), 
	// 		CGraphicsContext::ECenter,
	// 		TPoint( 0, 24+nY), 
	// 		TPoint( m_pGrid->ColumnWidth(), m_pGrid->ItemHeight()) );
	
	//This is for removing flicker for settings.
	AknListBoxLayouts::SetupFormAntiFlickerTextCell( 
		*m_pGrid, 
		m_pGrid->ItemDrawer(),
		1, 
		fontText, 
		215, 
		0, 
		0, 
		24+nY+fontText->HeightInPixels(),
		m_pGrid->ColumnWidth(), 
		CGraphicsContext::ECenter,
		TPoint( 0, 24+nY), 
		TPoint( m_pGrid->ColumnWidth(), m_pGrid->ItemHeight()) );
	
    DrawNow();
}

// End of File  

⌨️ 快捷键说明

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