Symbian 显示位图的例子

 学习了一个Symbian 显示位图的例子 这个例子的关键是资源文件的配制 其他的也不是很麻烦:

图片的放置位置是:

创建一个和group同级的picture文件夹 在文件夹里面建立一个文件夹bmp里面有三个bmp格式的文件image1.bmp image2.bmp   image2_mask.bmp

一:在mmp文件的末尾加上打包图片的文件命令:

START BITMAP testsvg_bmp.mbm
HEADER
 TARGETPATH /resource/apps     //打包目标位置
 SOURCEPATH  ../picture/bmp   //图片的放置位置
 SOURCE c24  image1.bmp       //图片名称
 SOURCE c24  image2.bmp
 SOURCE 1    image2_mask.bmp
END

二:在自己的xxxview.h里面定义图片

 CFbsBitmap* iBackgroundImage;
 CFbsBitmap* iImage2;
 CFbsBitmap* iImage2mask;

三:在自己的xxxview.cpp里面添加初始化和画图信息

a:头文件和定义:    

     #include <aknutils.h>//swb
     #include <graphic_bmp.mbg> //swb Bitmap enumeration
     //#include <akniconutils.h>  // AknIconUtils类的定义
     //#include <graphic_svg.mbg> // Bitmap enumeration

      _LIT(KSvgPath, "//resource//apps//graphic_svg.mif");
      _LIT(KBitmapPath, "//resource//apps//graphic_bmp.mbm");

b:ConstructL()里面初始化:

    TFileName bitmapFile (KBitmapPath);
    User::LeaveIfError (CompleteWithAppPath (bitmapFile));
    iBackgroundImage = iEikonEnv->CreateBitmapL             ( bitmapFile,EMbmGraphic_bmpImage1 );
    iImage2 = iEikonEnv->CreateBitmapL( bitmapFile,EMbmGraphic_bmpImage2 );
    iImage2mask = iEikonEnv->CreateBitmapL( bitmapFile,EMbmGraphic_bmpImage2_mask );

c:析构函数里释放:

    delete iBackgroundImage;
    delete iImage2;
    delete iImage2mask;

d:draw()函数里画

      //Draw()函数中实现
      //在设备绘图上下文类CWindowGc中绘制图形,CWindowGc是图形上下文       CGraphicesContext类的派生类
      //清空窗口上的内容
      gc.SetPenStyle( CGraphicsContext::ENullPen );
      gc.SetBrushColor( KRgbGray );
      gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
      gc.DrawRect( Rect() );
      gc.SetBrushStyle( CGraphicsContext::ENullBrush );
 
      //绘制客户区背景
      gc.BitBlt( Rect().iTl,iBackgroundImage );
      //gc.DrawBitmap(Rect(),iBackgroundImage);  //通过拉伸的方式放缩要目标矩形中
        
         //在客户区的第一行绘制位图图标
         //绘制非透明位图,选择不含有“Masked”单词的函数就可以了
         TPoint ptPos=Rect().iTl;
         gc.BitBlt(ptPos,iImage2);  //绘制时保持原始大小
        
         //绘制透明位图,需要有相应的掩码图,只有使用两者结合才能显示成功
         TSize size=iImage2->SizeInPixels();
         TRect sourceRect( TPoint( 0,0 ),size);
         ptPos.iX+=size.iWidth;
         gc.BitBltMasked(ptPos,iImage2,sourceRect,iImage2mask,ETrue);
        
         //绘制放缩的透明位图
         ptPos.iX+=size.iWidth;
         size.iWidth*=2;
         size.iHeight*=2;
         TRect destRect(ptPos,size);
         gc.DrawBitmapMasked (destRect,iImage2,sourceRect,iImage2mask,ETrue);
e:mmp文件里面添加lib:

     LIBRARY           eikcoctl.lib
     LIBRARY           bafl.lib
     LIBRARY           charconv.lib
     LIBRARY     fbscli.lib 
     LIBRARY     aknicon.lib bitgdi.lib mediaclientimage.lib

 里面涉及到的类有俩个 还涉及到mmp文件:

分别如下:

1: mmp文件:

 TARGET TestSVG_0xE6C3FF66.exe TARGETTYPE exe UID 0x100039CE 0xE6C3FF66 SOURCEPATH ../src SOURCE TestSVG.cpp SOURCE TestSVGApplication.cpp SOURCE TestSVGAppView.cpp SOURCE TestSVGAppUi.cpp SOURCE TestSVGDocument.cpp SOURCEPATH ../data START RESOURCE TestSVG.rss HEADER TARGET TestSVG_0xE6C3FF66 TARGETPATH resource/apps END //RESOURCE START RESOURCE TestSVG_reg.rss TARGET TestSVG_0xE6C3FF66_reg TARGETPATH /private/10003a3f/apps END //RESOURCE USERINCLUDE ../inc USERINCLUDE ../help SYSTEMINCLUDE /epoc32/include LIBRARY euser.lib LIBRARY apparc.lib LIBRARY cone.lib LIBRARY eikcore.lib LIBRARY avkon.lib LIBRARY commonengine.lib LIBRARY efsrv.lib LIBRARY estor.lib LIBRARY aknnotify.lib LIBRARY hlplch.lib LIBRARY eikcoctl.lib LIBRARY bafl.lib LIBRARY charconv.lib LIBRARY fbscli.lib LIBRARY aknicon.lib bitgdi.lib mediaclientimage.lib LANG SC VENDORID 0 SECUREID 0xE6C3FF66 CAPABILITY ReadUserData #ifdef ENABLE_ABIV2_MODE DEBUGGABLE_UDEBONLY #endif //BMP打包的资源命令 START BITMAP testsvg_bmp.mbm HEADER TARGETPATH /resource/apps SOURCEPATH ../picture/bmp SOURCE c24 image1.bmp SOURCE c24 image2.bmp SOURCE 1 image2_mask.bmp END // End of File

 

1: TestSVGAppView.h

#ifndef __TESTSVGAPPVIEW_h__ #define __TESTSVGAPPVIEW_h__ // INCLUDES #include <coecntrl.h> // CLASS DECLARATION class CTestSVGAppView : public CCoeControl { public: // New methods /** * NewL. * Two-phased constructor. * Create a CTestSVGAppView object, which will draw itself to aRect. * @param aRect The rectangle this view will be drawn to. * @return a pointer to the created instance of CTestSVGAppView. */ static CTestSVGAppView* NewL(const TRect& aRect); /** * NewLC. * Two-phased constructor. * Create a CTestSVGAppView object, which will draw itself * to aRect. * @param aRect Rectangle this view will be drawn to. * @return A pointer to the created instance of CTestSVGAppView. */ static CTestSVGAppView* NewLC(const TRect& aRect); /** * ~CTestSVGAppView * Virtual Destructor. */ virtual ~CTestSVGAppView(); public: // Functions from base classes /** * From CCoeControl, Draw * Draw this CTestSVGAppView to the screen. * @param aRect the rectangle of this view that needs updating */ void Draw(const TRect& aRect) const; /** * From CoeControl, SizeChanged. * Called by framework when the view size is changed. */ virtual void SizeChanged(); /** * From CoeControl, HandlePointerEventL. * Called by framework when a pointer touch event occurs. * Note: although this method is compatible with earlier SDKs, * it will not be called in SDKs without Touch support. * @param aPointerEvent the information about this event */ virtual void HandlePointerEventL(const TPointerEvent& aPointerEvent); private: // Constructors /** * ConstructL * 2nd phase constructor. * Perform the second phase construction of a * CTestSVGAppView object. * @param aRect The rectangle this view will be drawn to. */ void ConstructL(const TRect& aRect); /** * CTestSVGAppView. * C++ default constructor. */ CTestSVGAppView(); private: CFbsBitmap* iBackgroundImage; CFbsBitmap* iImage2; CFbsBitmap* iImage2mask; // CFbsBitmap* iSoundSvg; //图片会把SVG图转换成位图,如果要实现矢量拉伸,需要重新加载 // CFbsBitmap* iSoundSvgMask; }; #endif // __TESTSVGAPPVIEW_h__ // End of File

 

1: TestSVGAppView.cpp

#include <coemain.h> #include "TestSVGAppView.h" #include <aknutils.h>//swb #include <graphic_bmp.mbg> //swb Bitmap enumeration #include <akniconutils.h> // AknIconUtils类的定义 #include <graphic_svg.mbg> // Bitmap enumeration _LIT(KSvgPath, "//resource//apps//graphic_svg.mif"); _LIT(KBitmapPath, "//resource//apps//graphic_bmp.mbm"); // ============================ MEMBER FUNCTIONS =============================== // ----------------------------------------------------------------------------- // CTestSVGAppView::NewL() // Two-phased constructor. // ----------------------------------------------------------------------------- // CTestSVGAppView* CTestSVGAppView::NewL(const TRect& aRect) { CTestSVGAppView* self = CTestSVGAppView::NewLC(aRect); CleanupStack::Pop(self); return self; } // ----------------------------------------------------------------------------- // CTestSVGAppView::NewLC() // Two-phased constructor. // ----------------------------------------------------------------------------- // CTestSVGAppView* CTestSVGAppView::NewLC(const TRect& aRect) { CTestSVGAppView* self = new (ELeave) CTestSVGAppView; CleanupStack::PushL(self); self->ConstructL(aRect); return self; } // ----------------------------------------------------------------------------- // CTestSVGAppView::ConstructL() // Symbian 2nd phase constructor can leave. // ----------------------------------------------------------------------------- // void CTestSVGAppView::ConstructL(const TRect& aRect) { // Create a window for this application view CreateWindowL(); // Set the windows size SetRect(aRect); _LIT(KBitmapPath, "//resource//apps//graphic_bmp.mbm"); /*********加载图片***********************/ TFileName bitmapFile (KBitmapPath); User::LeaveIfError (CompleteWithAppPath (bitmapFile)); iBackgroundImage = iEikonEnv->CreateBitmapL( bitmapFile,EMbmGraphic_bmpImage1 ); iImage2 = iEikonEnv->CreateBitmapL( bitmapFile,EMbmGraphic_bmpImage2 ); iImage2mask = iEikonEnv->CreateBitmapL( bitmapFile,EMbmGraphic_bmpImage2_mask ); //svg // TFileName svgFile (KSvgPath); // User::LeaveIfError (CompleteWithAppPath (svgFile)); // AknIconUtils::CreateIconL(iSoundSvg, iSoundSvgMask, svgFile, EMbmGraphic_svgSound,EMbmGraphic_svgSound_mask); // //iSoundSvg=AknIconUtils::CreateIconL(svgFile,EMbmGraphic_svgSound); // AknIconUtils::SetSize(iSoundSvg, TSize(50,50), EAspectRatioNotPreserved); // AknIconUtils::SetSize(iSoundSvgMask, TSize(50,50), EAspectRatioNotPreserved); //*/ /*******************************************/ // Activate the window, which makes it ready to be drawn ActivateL(); } // ----------------------------------------------------------------------------- // CTestSVGAppView::CTestSVGAppView() // C++ default constructor can NOT contain any code, that might leave. // ----------------------------------------------------------------------------- // CTestSVGAppView::CTestSVGAppView() { // No implementation required } // ----------------------------------------------------------------------------- // CTestSVGAppView::~CTestSVGAppView() // Destructor. // ----------------------------------------------------------------------------- // CTestSVGAppView::~CTestSVGAppView() { // No implementation required delete iBackgroundImage; delete iImage2; delete iImage2mask; // delete iSoundSvg; // delete iSoundSvgMask; } // ----------------------------------------------------------------------------- // CTestSVGAppView::Draw() // Draws the display. // ----------------------------------------------------------------------------- // void CTestSVGAppView::Draw(const TRect& /*aRect*/) const { // Get the standard graphics context CWindowGc& gc = SystemGc(); // Gets the control's extent TRect drawRect(Rect()); // Clears the screen gc.Clear(drawRect); //Draw()函数中实现 //在设备绘图上下文类CWindowGc中绘制图形,CWindowGc是图形上下文CGraphicesContext类的派生类 //清空窗口上的内容 gc.SetPenStyle( CGraphicsContext::ENullPen ); gc.SetBrushColor( KRgbGray ); gc.SetBrushStyle( CGraphicsContext::ESolidBrush ); gc.DrawRect( Rect() ); gc.SetBrushStyle( CGraphicsContext::ENullBrush ); //绘制客户区背景 gc.BitBlt( Rect().iTl,iBackgroundImage ); //gc.DrawBitmap(Rect(),iBackgroundImage); //通过拉伸的方式放缩要目标矩形中 //在客户区的第一行绘制位图图标 //绘制非透明位图,选择不含有“Masked”单词的函数就可以了 TPoint ptPos=Rect().iTl; gc.BitBlt(ptPos,iImage2); //绘制时保持原始大小 //绘制透明位图,需要有相应的掩码图,只有使用两者结合才能显示成功 TSize size=iImage2->SizeInPixels(); TRect sourceRect( TPoint( 0,0 ),size); ptPos.iX+=size.iWidth; gc.BitBltMasked(ptPos,iImage2,sourceRect,iImage2mask,ETrue); //绘制放缩的透明位图 ptPos.iX+=size.iWidth; size.iWidth*=2; size.iHeight*=2; TRect destRect(ptPos,size); gc.DrawBitmapMasked(destRect,iImage2,sourceRect,iImage2mask,ETrue); /* //在客户区的第2行绘制SVG图图标 //绘制非透明SVG图 ptPos=Rect().iTl; ptPos.iY+=100; gc.BitBlt(ptPos,iSoundSvg); //绘制透明SVG图 TSize size2=iSoundSvg->SizeInPixels(); TRect sourceRect2( TPoint( 0,0 ),size2); ptPos.iX+=size2.iWidth; gc.BitBltMasked(ptPos,iSoundSvg,sourceRect2,iSoundSvgMask,ETrue); */ } // ----------------------------------------------------------------------------- // CTestSVGAppView::SizeChanged() // Called by framework when the view size is changed. // ----------------------------------------------------------------------------- // void CTestSVGAppView::SizeChanged() { DrawNow(); } // ----------------------------------------------------------------------------- // CTestSVGAppView::HandlePointerEventL() // Called by framework to handle pointer touch events. // Note: although this method is compatible with earlier SDKs, // it will not be called in SDKs without Touch support. // ----------------------------------------------------------------------------- // void CTestSVGAppView::HandlePointerEventL(const TPointerEvent& aPointerEvent) { // Call base class HandlePointerEventL() CCoeControl::HandlePointerEventL(aPointerEvent); }

 

你可能感兴趣的:(Symbian 显示位图的例子)