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

📄 rtxcontainer.cpp

📁 显示彩色字体的程序
💻 CPP
字号:
                                              /*
* ============================================================================
*  Name     : CRTxContainer from RTxContainer.h
*  Part of  : RTx
*  Created  : 2003. 08. 15. by  (V) - Forum Nokia 
*  Implementation notes:
*     
*  Version  :
*  Copyright: Forum Nokia
* ============================================================================
*/

// INCLUDE FILES
#include "RTxContainer.h"

#include <txtrich.h>   //Richtext
#include <eikrted.h>  //RichText Editor
#include <avkon.mbg> // Image Ids 
#include <eikenv.h> // ReadResource() 
#include <rtx.rsg> // Demo Text Buffer
#include "mypicture.h"   // CMyPicture 
 
// ================= MEMBER FUNCTIONS =======================

// Demo shows the T9 (Front End Processor) ,Scroller,
// "Insert Picture", disable Copy-Paste feature
// in RichText Editor


// CONSTANTS
_LIT( KImageBitmapFile, "Z:\\system\\data\\avkon.mbm" );
//_LIT( KImageBitmapFile, "c:\\System\\Apps\\Bmpmanip\\newimage.bmp" );
//  Magic Numbers generated for the Demo text
#define KLeftImagePositionInTheText		51
#define KRightImagePositionInTheText	56
#define KEndofCenterFormat				15
#define KLeftNokia						54
#define KRightNokia						58
// 567 twips == 1cm
#define KKImageWidth					800//290
#define KImageHeight					200//120

// ---------------------------------------------------------
// CRTxContainer::Prepare()
// Set the Editor flags
// ---------------------------------------------------------
//

void CRTxContainer::Prepare()
	{

	// Lower Case 
	iRtEd->SetAknEditorCase(EAknEditorLowerCase);
	// Fixed case
	iRtEd->SetAknEditorFlags
		(
		EAknEditorFlagFixedCase| //Set up the Allowed Cases
		EAknEditorFlagEnableScrollBars 	// Set up the scroller
		);
	}

// ---------------------------------------------------------
//  CRTxContainer::PreparePicture()
//  Append Bitmap to iBitmap Array
// ---------------------------------------------------------
//
void CRTxContainer::PrepareBitmapL(TInt32 aId)
	{
	CFbsBitmap* bitmap;
	// Create and Load the Bitmap
	bitmap= new( ELeave )CFbsBitmap;
	CleanupStack::PushL(bitmap);
	User::LeaveIfError(bitmap->Load( KImageBitmapFile, aId)); 
	//we have to free the memory at the Destructor() -> store the bitmap memory address
	iBitmap->AppendL(bitmap);
	CleanupStack::Pop(); //bitmap - container free bitmap at the destructor
	// last item in iBitmap array is ready to Insert
	}

// ---------------------------------------------------------
//  CRTxContainer::InsertMyPictureL()
//  Call Richtext's InsertL() with the CPicture
// ---------------------------------------------------------
//
void CRTxContainer::InsertMyPictureL(TInt aPos,TInt32 aId)
	{
	PrepareBitmapL(aId);
	
	CMyPicture* picture;
	// Create a CPicture derived class which will draw our image, depending this Size
	picture = new( ELeave )CMyPicture(TSize(KKImageWidth,KImageHeight),*(iBitmap->At(iBitmap->Count()-1)/*process the last item of iBitmap*/));
	CleanupStack::PushL(picture);
	// Prepare the Picture header, which will be instered into the Richtext
	TPictureHeader header;
	header.iPicture =TSwizzle<CPicture>(picture);
	iRtEd->RichText()->InsertL( aPos,header); 
	CleanupStack::Pop(); // picture - Richtext take the ownership 
	}

// ---------------------------------------------------------
//  CRTxContainer::Demo()
//  Create Demo Content 
// ---------------------------------------------------------
//

void CRTxContainer::DemoL()//主要东西都在这里面呢
	{
	//将rtx.rss里面定义的文字给iReEd赋值
	TBuf<256> Demobuf;
	CEikonEnv::Static()->ReadResource(Demobuf, R_TYPE_II_DIALOG_TBUF_DEMO_TEXT);
	iRtEd->SetTextL(&Demobuf);
	
	// 设置图片显示位置,分别为左右两个图片
	InsertMyPictureL(KLeftImagePositionInTheText,EMbmAvkonQgn_indi_battery_strength);
	InsertMyPictureL(KRightImagePositionInTheText ,EMbmAvkonQgn_indi_signal_strength);
	
	//// To Compare the '\f' to EParagraphDelimiter change the comment status of the two following lines
	///	iRtEd->RichText()->InsertL( KEndofCenterFormat , CEditableText::EParagraphDelimiter);
	//在15个字符后设了个换行
	iRtEd->RichText()->InsertL( KEndofCenterFormat , _L("\f"));

	// 把图片放置在下一行
	iRtEd->RichText()->InsertL( KLeftImagePositionInTheText , CEditableText::EParagraphDelimiter);

	// 居中
	iRtEd->SetSelectionL(KRightImagePositionInTheText ,  KEndofCenterFormat);
	
	//设置字体
	CParaFormat     paraFormat;
	TParaFormatMask paraFormatMask;
	paraFormatMask.SetAttrib(EAttAlignment);    // set the mask
	paraFormat.iHorizontalAlignment = CParaFormat::ECenterAlign;
	iRtEd->ApplyParaFormatL(&paraFormat, paraFormatMask);

	//改变颜色
	iRtEd->SetSelectionL(KLeftImagePositionInTheText ,  KRightImagePositionInTheText);

	TCharFormat charFormat;
	TCharFormatMask charFormatMask;
	charFormat.iFontPresentation.iTextColor=KRgbMagenta;//颜色洋红
	charFormatMask.SetAttrib(EAttColor );
	iRtEd->ApplyCharFormatL(charFormat, charFormatMask);
	iRtEd->SetSelectionL(KLeftNokia,  KRightNokia);//改变颜色的范围
	iRtEd->ApplyCharFormatL(charFormat, charFormatMask);

	//给最后加个句号:)
	iRtEd->RichText()->InsertL(iRtEd->Text()->DocumentLength()/* at the end*/,
				_L("."));

	}
void CRTxContainer::SetupL()
	{
    // One way to remove the selection
	//iRtEd->ClearSelectionL();
	
	// Copy-Paste Support is allowed in Edwin but we can disable
	// to do that uncomment the following line
		//iRtEd->EnableCcpuSupportL(EFalse);
	iRtEd->EnableCcpuSupportL(ETrue);
	// Another way to remove the selection
	iRtEd->SetCursorPosL(KRightImagePositionInTheText,EFalse);
	}

#undef KLeftImagePositionInTheText 
#undef KRightImagePositionInTheText 
#undef KEndofCenterFormat			
#undef KLeftNokia					
#undef KRightNokia					
#undef KKImageWidth					
#undef KImageHeight					


// ---------------------------------------------------------
// CRTxContainer::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//

void CRTxContainer::ConstructL(const TRect& aRect)
    {
	CreateWindowL();

	iBitmap = new (ELeave) CArrayFixFlat<CFbsBitmap *>(2); // DemoL() show two image	

	iRtEd = new(ELeave) CEikRichTextEditor;
	
	Prepare();

	iRtEd->ConstructL(this,0,0,0);
	iRtEd->SetFocus(ETrue);
	SetRect(aRect);
	
	DemoL();			
	
	ActivateL();
	
	SetupL();
    }

// ---------------------------------------------------------
// CRTxContainer::~CRTxContainer()
// Destructor - Free the allocated resources
// ---------------------------------------------------------

CRTxContainer::~CRTxContainer()
    {
	delete iRtEd;
	// Delete each image, which have reference in the Array
	for(int i=0;i<iBitmap->Count();i++)
		{
		delete iBitmap->At(i);
		}
	// Free the Array
	delete iBitmap;
    }

// ---------------------------------------------------------
// CRTxContainer::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void CRTxContainer::SizeChanged()
    {
	iRtEd->SetExtent(TPoint(1,1),TSize(174,142 ) ); //1 pixel border
    }

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

// ---------------------------------------------------------
// CRTxContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CRTxContainer::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
			return iRtEd;
        default:
            return NULL;
        }
    }

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



// ---------------------------------------------------------
// OfferKeyEventL() 
// Distribute the key event to the Editor
// ---------------------------------------------------------

TKeyResponse CRTxContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
	{
	if (iRtEd->IsFocused())
		{
		return iRtEd->OfferKeyEventL(aKeyEvent, aType);
		}
	else
		{
		return EKeyWasNotConsumed;
		}
	}


// End of File  

⌨️ 快捷键说明

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