×
Namespaces

Variants
Actions

如何不用程序框架来绘制屏幕

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

文章
翻译:
hoolee
最后由 hamishwillee 在 22 Dec 2011 编辑
  • 详细描述

下面演示的CWindowDrawer类演示了如何在标准程序框架之外构造一个可绘制的屏幕。可以利用这种形式构造一个后台服务,这样可以平时隐藏,在某些操作需要时再出现。


下面实例中的CWindowDrawer不会攫取焦点,因为当前已获焦点的程序仍然可以相应任何按键输入。如果要取消则可以传递ETrue到EnableReceiptOfFocus()函数里。

如果你不想获取整个屏幕作为你的绘制区域,可以在ConstructL()函数中的iWindow的SetExtent()方法中设置。

需要的链接库

LIBRARY   gdi.lib  //CFont
LIBRARY ws32.lib //RWindowGroup ,CWsScreenDevice, CWindowGc
LIBRARY apgrfx.lib //CApaWindowGroupName

WindowDrawer.cpp

#include <apgwgnam.h> //CApaWindowGroupName
#include "WindowDrawer.h"
#include <coedef.h> //for ECoeWinPriorityAlwaysAtFront
 
CWindowDrawer* CWindowDrawer::NewL(void)
{
CWindowDrawer* self = CWindowDrawer::NewLC(void);
CleanupStack::Pop(self);
return self;
}
 
 
CWindowDrawer* CWindowDrawer::NewLC(void)
{
CWindowDrawer* self = new (ELeave) CWindowDrawer(void);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
// the active object priority should be relatively low
CWindowDrawer::CWindowDrawer(void):CActive(EPriorityLow)
{
}
 
CWindowDrawer::~CWindowDrawer()
{
Cancel();
 
if(iMyFont)
{
iScreenDevice->ReleaseFont(iMyFont);
iMyFont = NULL;
}
 
delete iWindowGc;
iWindowGc = NULL;
delete iScreenDevice;
iScreenDevice = NULL;
 
iWindow.Close();
 
iWindowGroup.Close();
iWsSession.Close();
}
 
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();
}


WindowDrawer.h

#include <gdi.h> // CFont
#include <w32std.h> //RWindowGroup, CWsScreenDevice, CWindowGc
 
_LIT(KtxHelloThere ,"Hi there !");
_LIT(KFontArial ,"Arial");
 
class CWindowDrawer : public CActive
{
public:
static CWindowDrawer* NewL(void);
static CWindowDrawer* NewLC(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;
};

相关链接:

This page was last modified on 22 December 2011, at 08:10.
66 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.

×