You Are Here:

Community: Wiki

This page was last modified 13:37, 11 June 2009.

Drawing to the screen outside application framework

From Forum Nokia Wiki

The CWindowDrawer class illustrates how to construct a drawable screen outside the standard application framework. You usually use this kind of code when constructing a background server that normally needs to be hidden and only requires an UI in some parts of its operations.

As with this example the CWindowDrawer will not take focus which means that the current focused application still receives all key input. To disable this call the EnableReceiptOfFocus() method with ETrue.

For key catching you can also use CKeyCapturer

The active object implementation used in this example uses RWsSession's RedrawReady() method to receive redraw events. To get your own drawing code into it you only need to change the content of the Draw() method.

Also if you don't want to take the whole screen for your window you can change the values given in iWindow's SetExtent() method inside the ConstructL() method.

Library required:

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;
};

Related Links:

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditFurlTechnocratiMagnoliaTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fMMPE5ffileX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxMMPE20fileE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfnTypeZCommunityContentQ qdcZtypeQUqfnTypeZE52esourceQ qdcZtypeQUqfnTypeZWebpageQ qdcZtypeQUqfnTypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZE52esourceQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxfileX qfnZuserE5ftagQSxlibpathX qfnZuserE5ftagQSxmmpX qfnZuserE5ftagQSxresourceX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfnTypeZCommunityContentQ qrdfZtypeQUqfnTypeZE52esourceQ qrdfZtypeQUqfnTypeZWebpageQ qrdfZtypeQUqfnTypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx5E2E0000X