直接调用RWsSession绘屏

来源:互联网 发布:大学生网络购物2016 编辑:程序博客网 时间:2024/06/04 19:16

.h文件
-------------------------------------------------

/*
 * WindowDrawer.h
 *
 *  Created on: 2010-1-29
 *      Author: Administrator
 */
#ifndef WINDOWDRAWER_H_
#define WINDOWDRAWER_H_

#include <gdi.h> // CFont
#include <w32std.h> //RWindowGroup, CWsScreenDevice, CWindowGc
#include <e32base.h>
_LIT(KtxHelloThere ,"Hi there !");
_LIT(KFontArial ,"Arial");
class CWindowDrawer : public CActive
 {
public:
 static CWindowDrawer* NewL(void);
 ~CWindowDrawer();
protected:
 void DoCancel();
 void RunL();
private:
 CWindowDrawer();
 void ConstructL(void);
 void Draw(void);

private:
 RWsSession iWsSession;
 CWindowGc* iWindowGc;
 CWsScreenDevice* iScreenDevice;
 RWindowGroup iWindowGroup;
 RWindow iWindow;
 CFont* iMyFont;
 };

#endif /* WINDOWDRAWER_H_ */

 

================================================================

.cpp文件

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

/*
 * WindowDrawer.cpp
 *
 *  Created on: 2010-1-29
 *      Author: Administrator
 */

#include <apgwgnam.h> //CApaWindowGroupName
#include "WindowDrawer.h"
#include <coedef.h> //for ECoeWinPriorityAlwaysAtFront

CWindowDrawer* CWindowDrawer::NewL(void)

 {
 CWindowDrawer* self = new (ELeave) CWindowDrawer();
 CleanupStack::PushL(self);
 self->ConstructL();
 CleanupStack::Pop(self);
 return self;
 }

CWindowDrawer::~CWindowDrawer()
 {
 Cancel();
 if (iMyFont)
  {
  iScreenDevice->ReleaseFont(iMyFont);
  iMyFont = NULL;
  }

 delete iWindowGc;
 iWindowGc = NULL;

 delete iScreenDevice;
 iScreenDevice = NULL;

 iWindow.Close();
 iWindowGroup.Close();
 iWsSession.Close();
 }

CWindowDrawer::CWindowDrawer() :
 CActive(EPriorityLow)
 {

 }

void CWindowDrawer::ConstructL(void)
 {
 CActiveScheduler::Add(this);

 User::LeaveIfError(iWsSession.Connect());

 iScreenDevice = new (ELeave) CWsScreenDevice(iWsSession);
 User::LeaveIfError(iScreenDevice->Construct());
 User::LeaveIfError(iScreenDevice->CreateContext(iWindowGc));

 TFontSpec MyeFontSpec(KFontArial, 12 * 15);
 MyeFontSpec.iTypeface.SetIsProportional(ETrue);
 User::LeaveIfError(iScreenDevice->GetNearestFontInTwips(iMyFont,
   MyeFontSpec));

 iWindowGroup = RWindowGroup(iWsSession);
 User::LeaveIfError(iWindowGroup.Construct((TUint32) &iWindowGroup, EFalse));
 //iWindowGroup.SetOrdinalPosition(0,ECoeWinPriorityNormal);
 iWindowGroup.SetOrdinalPosition(0, ECoeWinPriorityAlwaysAtFront);
 iWindowGroup.EnableReceiptOfFocus(EFalse);

 CApaWindowGroupName* winGroupName = CApaWindowGroupName::NewLC(iWsSession);
 winGroupName->SetHidden(ETrue);
 winGroupName->SetWindowGroupName(iWindowGroup);
 CleanupStack::PopAndDestroy();

 iWindow = RWindow(iWsSession);
 User::LeaveIfError(iWindow.Construct(iWindowGroup, (TUint32) &iWindow));

 TPixelsTwipsAndRotation SizeAndRotation;
 iScreenDevice->GetDefaultScreenSizeAndRotation(SizeAndRotation);

 iWindow.Activate();
 iWindow.SetExtent(TPoint(0, 0), SizeAndRotation.iPixelSize);
 iWindow.SetBackgroundColor(KRgbBlue);
 iWindow.SetOrdinalPosition(0,ECoeWinPriorityNormal);
 //iWindow.SetOrdinalPosition(0, ECoeWinPriorityAlwaysAtFront);
 iWindow.SetNonFading(ETrue);
 iWindow.SetVisible(ETrue);

 Draw();

 iWsSession.RedrawReady(&iStatus);
 SetActive();
 }

void CWindowDrawer::RunL()
 {
 if (iStatus != KErrCancel)
  {
  TWsRedrawEvent e;
  iWsSession.GetRedraw(e);
  // if Windows Server does not want a redraw the window handle is 0
  if (e.Handle() != 0)
   {
   // draw our only window
   Draw();
   }

  iWsSession.RedrawReady(&iStatus);
  SetActive();
  }
 }

void CWindowDrawer::Draw(void)
 {
 iWindowGc->Activate(iWindow);
 TRect DrwRect(TPoint(0, 0), iWindow.Size());

 iWindow.Invalidate(DrwRect);
 iWindow.BeginRedraw();
 iWindowGc->Clear(DrwRect);

 if (iMyFont)
  {
  iWindowGc->UseFont(iMyFont);
  TInt StartY((DrwRect.Height() - iMyFont->HeightInPixels()) / 2);
  iWindowGc->DrawText(KtxHelloThere, TRect(0, StartY, DrwRect.Width(),
    (StartY + iMyFont->HeightInPixels())),
    iMyFont->AscentInPixels(), CGraphicsContext::ECenter, 0);
  }

 iWindow.EndRedraw();
 iWindowGc->Deactivate();
 iWsSession.Flush();
 }

void CWindowDrawer::DoCancel()
 {
 iWsSession.RedrawReadyCancel();
 }

原创粉丝点击