×
Namespaces

Variants
Actions

屏幕方向改变时如何在exe程序中(无CONE环境)获得通知?

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

代码示例
文章
翻译:
dougcn
最后由 hamishwillee 在 22 Dec 2011 编辑


为何需要这个方案?

  为了让你的应用程序能感知布局并支持由窗口服务器发起的方向切换,在UI应用中可从CCoeControl::HandleResourceChange()获得方向改变通知,而对于(无UI的)exe,我们需要按下面的方法实现。

  创建一个活动对象类,调用RWsSession的EventReady()方法以监听事件,并调用RWindowGroup::EnableScreenChangeEvents()使程序能接收到屏幕方向变化通知,然后在RunL中调用RWsSession的GetEvent方法获得具体通知类型。

#include <e32base.h> // CActive
#include <w32std.h> // RWsSession
 
class CListener : public CActive
{
public:
static CListener* NewL();
static CListener* NewLC();
~CListener();
 
public:
void RequestEvent();
 
public:
void DoCancel();
void RunL();
 
private:
CListener();
void ConstructL();
 
private:
RWsSession iWsSession;
RWindowGroup iWindowGroup;
};
#include <Listener.h>
 
CListener* CListener::NewLC()
{
CListener* me = new(ELeave) CListener;
CleanupStack::PushL(me);
me->ConstructL();
return me;
}
 
CListener* CListener::NewL()
{
CListener* me = NewLC();
CleanupStack::Pop(me);
return me;
}
 
CListener::CListener() : CActive(CActive::EPriorityStandard)
{
CActiveScheduler::Add(this);
}
 
void CListener::ConstructL()
{
TInt err = iWsSession.Connect();
User::LeaveIfError(err);
iWindowGroup = RWindowGroup(iWsSession);
err = iWindowGroup.Construct(reinterpret_cast<TUint32>(this), EFalse);
User::LeaveIfError(err);
iWindowGroup.SetOrdinalPosition(-1);
iWindowGroup.DefaultOwningWindow();
iWindowGroup.EnableReceiptOfFocus(EFalse);
iWindowGroup.EnableScreenChangeEvents(); // EEventScreenDeviceChanged
// iWindowGroup.EnableFocusChangeEvents(); // EEventFocusGroupChanged
}
 
void CListener::RequestEvent()
{
if(!IsActive())
{
iStatus = KRequestPending;
iWsSession.EventReady(&iStatus);
SetActive();
}
}
 
void CListener::RunL()
{
TWsEvent wsEvent;
iWsSession.GetEvent(wsEvent);
RequestEvent();
switch (wsEvent.Type())
{
case EEventScreenDeviceChanged:
{
//you will get notification here, do whatever you want to do here.
RDebug::Printf("Got the EEventScreenDeviceChanged event!");
CActiveScheduler::Stop();
break;
}
default:
break;
}
}
 
void CListener::DoCancel()
{
iWsSession.EventReadyCancel();
}
 
CListener::~CListener()
{
Cancel();
iWindowGroup.Close();
iWsSession.Close();
}

  点击以下链接下载完整的示例程序:

Aluisjgu(EEventScreenDeviceChanged).zip

This page was last modified on 22 December 2011, at 07:44.
37 page views in the last 30 days.
×