Code:
// ================= Start of Window.h =======================
//
#if !defined(__MY_WINDOW_H__)
#define __MY_WINDOW_H__
class CWindow;
/////////////////////////////////////////////////////////////////////////
////////////////////// Declaration of CWsClient /////////////////////////
/////////////////////////////////////////////////////////////////////////
// Base class for all windows
class CWsClient : public CActive
{
protected:
//construct
CWsClient(const TRect& aRect);
public:
static CWsClient* NewL(const TRect& aRect);
void ConstructL();
// destruct
~CWsClient();
public:
// terminate cleanly
void Exit();
// active object protocol
void IssueRequest(); // request an event
void DoCancel(); // cancel the request
virtual void RunL(); // handle completed request
private:
CWsScreenDevice* iScreen;
CWindowGc* iGc;
CWindow *iWindow;
RWsSession iWs;
RWindowGroup iGroup;
const TRect& iRect;
friend class CWindow; // needs to get at session
};
//////////////////////////////////////////////////////////////////////////////
///////////////////////// CWindow declaration ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
class CWindow : public CBase
{
public:
CWindow(CWsClient* aClient);
void ConstructL (const TRect& aRect);
~CWindow();
public:
// access
RWindow& Window(); // our own window
// drawing
void Draw(const TRect& aRect);
private:
CWindowGc* SystemGc(); // system graphics context
private:
RWindow iWindow; // window server window
TRect iRect; // rectangle re owning window
private:
CWsClient* iClient; // client including session and group
};
#endif // __MY_WINDOW_H__
// ================= End of Window.h =======================
// ================= Start of Window.cpp =======================
// Window.cpp
//
#include <w32std.h>
#include <coedef.h>
#include "Window.h"
///////////////////////////////////////////////////////////////////////////////
////////////////////////// CWindow implementation /////////////////////////////
///////////////////////////////////////////////////////////////////////////////
CWindow::CWindow(CWsClient* aClient)
: iClient(aClient)
{
}
void CWindow::ConstructL (const TRect& aRect)
{
// Use the window group for parent window
RWindowTreeNode* parent= &(iClient->iGroup);
iWindow=RWindow(iClient->iWs); // use app's session to window server
User::LeaveIfError(iWindow.Construct(*parent,(TUint32)this));
iRect = aRect;
iWindow.SetExtent(iRect.iTl, iRect.Size()); // set extent relative to group coords
iWindow.Activate(); // window is now active
}
CWindow::~CWindow()
{
iWindow.Close(); // close our window
}
RWindow& CWindow::Window()
{
return iWindow;
}
CWindowGc* CWindow::SystemGc()
{
return iClient->iGc;
}
/****************************************************************************\
| Function: CWindow::Draw
| Purpose: Redraws the contents of CSmallWindow within a given
| rectangle. CSmallWindow displays a square border around
| the edges of the window, and two diagonal lines between the
| corners.
| Input: aRect Rectangle that needs redrawing
| Output: None
\****************************************************************************/
void CWindow::Draw(const TRect& aRect)
{
// Drawing to a window is done using functions supplied by
// the graphics context (CWindowGC), not the window.
CWindowGc* gc = SystemGc(); // get a gc
gc->SetClippingRect(aRect); // clip outside this rect
gc->Clear(aRect); // clear
TSize size=iWindow.Size();
TInt width=size.iWidth;
TInt height=size.iHeight;
// Draw a square border
gc->DrawLine(TPoint(0,0),TPoint(0,height-1));
gc->DrawLine (TPoint (0, height-1), TPoint (width-1, height-1));
gc->DrawLine(TPoint(width-1,height-1),TPoint(width-1,0));
gc->DrawLine (TPoint (width-1, 0), TPoint (0, 0));
// Draw a line between the corners of the window
gc->DrawLine(TPoint(0,0),TPoint(width, height));
gc->DrawLine (TPoint (0, height), TPoint (width, 0));
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// CWsClient implementation ////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
CWsClient* CWsClient::NewL(const TRect& aRect)
{
// make new client
CWsClient* client=new (ELeave) CWsClient(aRect);
CleanupStack::PushL(client); // push, just in case
client->ConstructL(); // construct and run
CleanupStack::Pop();
return client;
}
CWsClient::CWsClient(const TRect& aRect)
: CActive(CActive::EPriorityHigh),
iRect(aRect)
{
}
void CWsClient::ConstructL()
{
// add ourselves to active scheduler
CActiveScheduler::Add(this);
// get a session going
User::LeaveIfError(iWs.Connect());
// construct our one and only window group
iGroup=RWindowGroup(iWs);
User::LeaveIfError(iGroup.Construct(2,ETrue)); // meaningless handle; enable focus
// construct screen device and graphics context
iScreen=new (ELeave) CWsScreenDevice(iWs); // make device for this session
User::LeaveIfError(iScreen->Construct()); // and complete its construction
User::LeaveIfError(iScreen->CreateContext(iGc));// create graphics context
iWindow = new (ELeave) CWindow (this);
iWindow->ConstructL(iRect);
// 窗口始终在最上层
iGroup.SetOrdinalPosition(0, ECoeWinPriorityAlwaysAtFront);
// 禁止接受焦点
iGroup.EnableReceiptOfFocus(EFalse);
// Set the window is non-fading
iGroup.SetNonFading(ETrue);
// 将窗口提到前面
TApaTask task(iWs);
task.SetWgId(iGroup.Identifier());
task.BringToForeground();
// request first event and start scheduler
IssueRequest();
}
CWsClient::~CWsClient()
{
// neutralize us as an active object
Deque(); // cancels and removes from scheduler
// get rid of everything we allocated
delete iGc;
delete iScreen;
delete iWindow;
// destroy window group
iGroup.Close();
// finish with window server
iWs.Close();
}
void CWsClient::IssueRequest()
{
iWs.RedrawReady(&iStatus); // request redraw
SetActive(); // so we're now active
}
void CWsClient::DoCancel()
{
iWs.RedrawReadyCancel(); // cancel redraw request
}
/****************************************************************************\
| Function: CWsClient::RunL()
| Called by active scheduler when an even occurs
| Purpose: do Redraw
\****************************************************************************/
void CWsClient::RunL()
{
// find out what needs to be done
TWsRedrawEvent redrawEvent;
iWs.GetRedraw(redrawEvent); // get event
CWindow* window=(CWindow*)(redrawEvent.Handle()); // get window
if (window)
{
TRect rect=redrawEvent.Rect(); // and rectangle that needs redrawing
// now do drawing
iGc->Activate(window->Window());
window->Window().BeginRedraw(rect);
window->Draw(rect);
window->Window().EndRedraw();
iGc->Deactivate();
}
// maintain outstanding request
IssueRequest(); // maintain outstanding request
}
// ================= End of Window.cpp =======================
上面的代码只是一个最基础的框架,你可以自己添更多的东西。比如显示一些文字。不过要显示文字就要先设定字体,具体操作如下:
先要建立一个CWsScreenDevice:
Code:
iScreen = new (ELeave) CWsScreenDevice(iWs);
然后可以用GetNearestFontInTwips通过字体名字获得CFont:
Code:
LIT(FONT_CH16, "CombinedChinesePlain16");
TFontSpec myFontSpec(FONT_CH16, 200);
iScreen->GetNearestFontInTwips(iFont, myFontSpec);