How can I avoid task-switching in my DirectX Applications?
If you are using a fullscreen application using DirectDraw and you have task-switched (Pressed Alt-Tab) your window will lose its focus. It generates a WM_KILLFOCUS message. You can use the following steps to regain control.- In the WM_KILLFOCUS handler, kill all of your DirectX Objects. Set a flag to indicate that the current window’s focus is lost.
- The Windows Operating System will always generate a WM_SETFOCUS message to the window currently under focus. So when you task switch back, your window will receive this message. You can then set the focus to your window handler, reinitialize the DirectX objects and turn off the flag.
case WM_KILLFOCUS:
if( g_bIsGameObjectInited )
{
DirectXApp->m_bIsFocusLost = TRUE;
DirectXApp->DestroyApp();
}
case WM_SETFOCUS:
if( g_bIsGameObjectInited )
if(DirectXApp->m_bIsFocusLost)
{
DirectXApp->m_bIsFocusLost = FALSE;
SetFocus(DirectXApp->m_hWnd);
DirectXApp->InitApp();
}
break;Check Menu.zip for full source code
If the application is a Direct3D one, then it will lose the device when you task-switch. Even then the functions will seem to have SUCCEEDED () - but nothing will actually come out on the screen. The only exception to that is IDirect3DDevice9::Present () - which will fail with the code D3DERR_DEVICELOST. To track whether the device is lost or not, either checks the return value of IDirect3DDevice9: Present () for D3DERR_DEVICELOST, or call IDirect3DDevice9::TestCooperativeLevel (). The second is preferred; as if you use the first, you will waste an entire cycle drawing the world that won't be seen and will find it out only after calling IDirect3DDevice9::Present function. Instead calling TestCooperativeLevel () at the beginning of each frame will return one of three things: D3D_OK (The device is not lost), D3DERR_DEVICENOTRESET (Device is lost but you can get it back now, so no need of resetting). D3DERR_DEVICELOST (Device is lost, but you can't get it back yet - pause your game and wait until it's ready) Reset the device and try again.
FAQ Menu
Sponsored links
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Villanova University Six Sigma & IT Certificate Programs
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
3 Months Free - ASP.NET Web Hosting
3 Months Free & No Setup Fees on ASP.NET 3.5/2.0 Hosting on Windows 2008/2003 Servers ? Click Here!
3 Months Free & No Setup Fees on ASP.NET 3.5/2.0 Hosting on Windows 2008/2003 Servers ? Click Here!
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Add complete SSH and SFTP support to your .NET framework application
