多行文本框的应用。可以修改字体型号,字体颜色,行间距

//---------------------------------------实例化--------------------------------------------

    iREdit1.SetRect(TPoint(50, 50), TSize(260, 200));
    iEdit1 = new (ELeave) CMyEdwin;
    iEdit1->SetContainerWindowL(*this);

    TResourceReader reader;
    iEikonEnv->CreateResourceReaderLC(reader, R_UTFJJWVX_CONTAINER_EDIT1);
    iEdit1->ConstructFromResourceL(reader);
    CleanupStack::PopAndDestroy(); // reader internal state
    iEdit1->SetFocus(ETrue);
    //--------修改字体型号、颜色--------
    //CFont* sFont = (CFont*) CEikonEnv::Static()->DenseFont();
    CFont* sFont = (CFont*) LatinBold16();

    TFontSpec sFontSpec = sFont->FontSpecInTwips();

    TCharFormat format;
    Mem::FillZ(&format, sizeof(TCharFormat));

    TCharFormatMask mask;
    Mem::FillZ(&mask, sizeof(TCharFormatMask));

    format.iFontSpec.iTypeface = sFontSpec.iTypeface;
    TInt nHeight = sFontSpec.iHeight;
    format.iFontSpec.iHeight = sFontSpec.iHeight;
    format.iFontPresentation.iTextColor = TLogicalRgb(TRgb(238, 84, 0)); //修改字体颜色
    mask.SetAttrib(EAttColor);
    mask.SetAttrib(EAttFontTypeface);
    mask.SetAttrib(EAttFontHeight);

    CCharFormatLayer * layer = CCharFormatLayer::NewL(format, mask);

    iEdit1->SelectAllL();
    iEdit1->SetCharFormatLayer(layer);
    //--------修改多行文本框的行间距--------
    TParaFormatMask mask2;
    mask2.SetAll();
    CParaFormat* iFormat = CParaFormat::NewL();
    iFormat->iLineSpacingInTwips = 20;
    CParaFormatLayer * iFormatLayer = CParaFormatLayer::NewL(iFormat, mask2);
    iEdit1->SetParaFormatLayer(iFormatLayer);
    //-------------------------------------

    iEdit1->SetRect(iREdit1);

//---------------------------------------头文件-------------------------------------------

#ifndef MYEDWIN_H
#define MYEDWIN_H

// INCLUDES
#include <EIKRTED.H>
   
// CLASS DECLARATION
class CMyEdwin : public CEikRichTextEditor
    {
protected:
    CLafEdwinCustomDrawBase* CreateCustomDrawL();
    };

#endif

//---------------------------------------定义文件-------------------------------------------

// INCLUDE FILES
#include <eikenv.h>
#include "MyEdwin.h"

// =================== CUSTOM DRAW =========================
class CMyCustomDraw : public CLafEdwinCustomDrawBase
    {
public:
    static CMyCustomDraw
    * NewL(const MLafEnv& aEnv, const CCoeControl& aControl);
    ~CMyCustomDraw();

public:
    // from MFormCustomDraw
    void DrawBackground(const TParam& aParam, const TRgb& aBackground,
            TRect& aDrawn) const;
    TRgb SystemColor(TUint aColorIndex, TRgb aDefaultColor) const;

public:
    void LineSpacingChanged();

private:
    CMyCustomDraw(const MLafEnv& aEnv, const CCoeControl& aControl);
    void ConstructL();

private:
    CFbsBitmap* iBitmap;
    };

CMyCustomDraw* CMyCustomDraw::NewL(const MLafEnv& aEnv,
        const CCoeControl& aControl)
    {
    CMyCustomDraw* self = new (ELeave) CMyCustomDraw(aEnv, aControl);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
    }

CMyCustomDraw::~CMyCustomDraw()
    {
    if (iBitmap)
        {
        delete iBitmap;
        iBitmap = NULL;
        }
    }

void CMyCustomDraw::DrawBackground(const TParam& aParam,
        const TRgb& aBackground, TRect& aDrawn) const
    {

    CLafEdwinCustomDrawBase::DrawBackground(aParam, aBackground, aDrawn);

    /*
     if ((iBitmap != NULL) && (iBitmap->Handle() != 0))
     {
     TRect rect = TRect(TPoint(0, 0), iBitmap->SizeInPixels());
     aParam.iGc.SetClippingRect(aParam.iDrawRect);
     aParam.iGc.DrawBitmap(rect, iBitmap);
     }
     */

    }

TRgb CMyCustomDraw::SystemColor(TUint aColorIndex, TRgb aDefaultColor) const
    {
    return CLafEdwinCustomDrawBase::SystemColor(aColorIndex, aDefaultColor);
    }

void CMyCustomDraw::LineSpacingChanged()
    {
    CLafEdwinCustomDrawBase::LineSpacingChanged();
    }

CMyCustomDraw::CMyCustomDraw(const MLafEnv& aEnv, const CCoeControl& aControl) :
    CLafEdwinCustomDrawBase(aEnv, aControl)
    {
    }

void CMyCustomDraw::ConstructL()
    {
    /*
     #ifdef _DEBUG
     _LIT(KMbmFile, "z://resource//apps//Utfjjwvx.mbm");
     #else
     _LIT(KMbmFile, "c://resource//apps//Utfjjwvx.mbm");
     #endif
     iBitmap = new (ELeave) CFbsBitmap;
     TInt err = iBitmap->Load(KMbmFile, );
     User::LeaveIfError(err);
     */
    iBitmap = NULL;
    }

// ================= MEMBER FUNCTIONS =======================
CLafEdwinCustomDrawBase* CMyEdwin::CreateCustomDrawL()
    {
    return CMyCustomDraw::NewL(iEikonEnv->LafEnv(), *this);
    }

猜你喜欢

转载自blog.csdn.net/dymx101/article/details/6562202