×
Namespaces

Variants
Actions

使用CPicture在文本(RichTextEditor)中插入图像

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

兼容于
平台:
Symbian

文章
User:Davey 2 在 08 Dec 2007 创建
最后由 hamishwillee 在 08 May 2013 编辑
适用版本:
   Series 60 2nd FP3, Series 60 3rd 
 Pic.PNG

有时候在文字中插入一些图片可以帮助我们更好的表达我们的意图,就像上图一样。Series 60提供了RichTextEditor(CEikRichTextEditor)来实现这一功能。下面简述一下在RichTextEditor中插入图片的流程: 首先,在RichTextEditor中插入的图片都是CPicture类型,我们需要定义一个继承自CPicture的类作为插入的图片类。一个典型的图片类CMyPicture如下定义:


 #ifndef CMYPICTURE_H
  #define CMYPICTURE_H
  // INCLUDES
  #include <gdi.h  // FORWARD DECLARATIONS
  class TSize;
  class CFbsBitmap;
  // CLASS DECLARATION
  /**
  *  CMyPicture the class which draw the image.
  */
  class CMyPicture :public CPicture
    {
    public:  // Constructors and NO destructor (bitmap not owned)
        /**
        * C++ default constructor.
        * @param aSize Size of the picture in twips.
	* @param aBitmap Bitmap
        */
        CMyPicture( TSize aSize, CFbsBitmap& aBitmap );
	public: // From CPicture
		/**
		* Prohibit linebreaks.
		*/
		TBool LineBreakPossible( TUint aClass,
							 TBool aBeforePicture,
							 TBool aHaveSpaces ) const;
		/**
        * Draw the picture
        */
		 void Draw( CGraphicsContext& aGc,
			    const TPoint& aTopLeft,
			    const TRect& aClipRect,
			    MGraphicsDeviceMap* aMap ) const;

	/**
        * There's no need for it in this , but must be implemented.
        */
	void ExternalizeL( RWriteStream& aStream ) const;

        /**
        * Sets the picture's size in twips.
	* @param aSize Size.
        */
	void SetOriginalSizeInTwips( TSize aSize );

        /**
        * Returns the picture's size in twips.
		* @param aSize Size.
        */
		 void GetOriginalSizeInTwips( TSize& aSize ) const;
	protected:	// Data
		TSize iSizeInTwips; // Size of the bitmap data
		CFbsBitmap* iBitmap; // reference to the Bitmap data
    };

  #endif


实际绘制的工作是在Draw()函数中完成的:

  void CMyPicture::Draw( CGraphicsContext& aGc,
				  const TPoint& aTopLeft,
				  const TRect& aClipRect,
				  MGraphicsDeviceMap* aMap ) const
	{
	TRect bitmapRect=aMap->TwipsToPixels(TRect(TPoint(),iSizeInTwips));
	bitmapRect.Move(aTopLeft);
	aGc.Reset();
	aGc.SetClippingRect(aClipRect);
	aGc.DrawBitmap(bitmapRect, iBitmap);
	}


完整的CMyPicture类实现如下:

  // INCLUDE FILES
  #include "MyPicture.h"
  #include <fbs.h>
  // ---------------------------------------------------------
  // Constructor
  // ---------------------------------------------------------
  //
  CMyPicture::CMyPicture( TSize aSize, CFbsBitmap& aBitmap )
	: iSizeInTwips(aSize), iBitmap(&aBitmap)
    {
    }
  // ---------------------------------------------------------
  // LineBreakPossible()
  // ---------------------------------------------------------
  //
  TBool CMyPicture::LineBreakPossible( TUint /*aClass*/,							       TBool /*aBeforePicture*/,						       TBool /*aHaveSpaces*/ ) const
	{
	return EFalse;
	}

  // ---------------------------------------------------------
  // Draw()
  // ---------------------------------------------------------
  //
  void CMyPicture::Draw( CGraphicsContext& aGc,
                         const TPoint& aTopLeft,
                         const TRect& aClipRect,
			 MGraphicsDeviceMap* aMap ) const
	{
	TRect bitmapRect=aMap->TwipsToPixels(TRect(TPoint(),iSizeInTwips));
	bitmapRect.Move(aTopLeft);
	aGc.Reset();
	aGc.SetClippingRect(aClipRect);
	aGc.DrawBitmap(bitmapRect, iBitmap);
	}

  // ---------------------------------------------------------
  // ExternalizeL()
  // ---------------------------------------------------------
  //
  void CMyPicture::ExternalizeL( RWriteStream& /*aStream*/ ) const
	{
    // No implementation required
	}

  // ---------------------------------------------------------
  // SetOriginalSizeInTwips()
  // ---------------------------------------------------------
  //
  void CMyPicture::SetOriginalSizeInTwips( TSize aSize )
	{
	iSizeInTwips = aSize;
	}

  // ---------------------------------------------------------
  // GetOriginalSizeInTwips()
  // ---------------------------------------------------------
  //
  void CMyPicture::GetOriginalSizeInTwips( TSize& aSize ) const
	{
	aSize = iSizeInTwips;
	}

  //EOF

这样我们就定义了一个插入的图片类,在RichTextEditor中我们可以使用一个类似如下的InsertMyPictureL插入函数进行插入图片了。

 void CRTEContainer::InsertMyPictureL(TInt aPos)
	{
	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 
	}

完整的示例代码可以在Forum Nokia获取[1]

使用以上的方案插入的图片背景始终是白色的,如果要插入带背景的图片请参考这里在文本(CEikRichTextEditor)中插入带背景的图片

--------------------------------------------

best regards

davey_2

This page was last modified on 8 May 2013, at 03:01.
83 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.

×