来源:NewLC.com
This article will show you how you can control the behaviour of your application so that it knows when it gains or looses the screen focus and how to control it.
Being notified of the focus change
The Series 60 framework will notify an application when it gains or looses the screen focus through a call to CAknAppUi::HandleForegroundEventL(TBool aForeground). The aForeground parameter equals ETrue when your app gains the screen focus, and EFalse when it looses it.
If you need to do some specific stuff, you should override this function (and call the base class implementation). Here is an example for an application that don't want to loose the focus:
Changing the focus
You can also request to change the focus of your application. The commands are TApaTask::SendToBackground() and TApaTask::BringToForeground().
Here is how you can use them (from the AppUi):
void CMyAppUi::BringToForeground()
{
// Construct en empty TApaTask object
// giving it a reference to the Window Server session
TApaTask task(iEikonEnv->WsSession( ));
// Initialise the object with the window group id of
// our application (so that it represent our app)
task.SetWgId(CEikonEnv::Static()->RootWin().Identifier());
// Request window server to bring our application
// to foreground
task.BringToForeground();
}
I never tested the following code, but you may be able to control the focus of other applications by using:
// Bring the application "theApp" to background
TApaTaskList tasklist(iCoeEnv->WsSession());
TApaTask task(tasklist.FindApp(_L("theApp")));
task.SendToBackground(); // or BringToForeground()
Don't forget to link against apgrfx.lib




