Archived

This topic is now archived and is closed to further replies.

Handling ALT+TAB in .NET

This topic is 5744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've tried everything. I can't manage to handle ALT+TAB correctly. Here's what happens: 1. I start the application. A separate thread begins which does the rendering loop. 2. I press ALT+TAB, I am back in Windows and the application is paused.(this part works) 3. I click the application's taskbar icon to restore it. The screen becomes black, resolution changes, and it seems to work.. then, Windows appears again, and nothing is rendered. The resolution is NOT switched back to the Windows resolution and the application is still running. 4. I close the application. The resolution switches back, and I'm back in VS.NET wondering why it doesn't work. Here's the code.
		private void ThreadMain()
		{
			int end, start;
			int tps = 0;
			float CamY = 11.0f;
			D3D8.Initialize(this.Handle, 1024, 768, true, true);
			XObject mesh = new XObject(D3D8.d3dDevice);
			mesh.Load("object.x", "texture", "texture\\refl_dc.tga");
			mesh.RotX = mesh.RotY = mesh.RotZ = 0;
			mesh.X = mesh.Y = mesh.Z = 0;
			Scene scn = new Scene(D3D8.d3dDevice);
			start = System.Environment.TickCount;
			end = System.Environment.TickCount - start;
			while(end < 60000)
			{
				//begin scene
				D3D8.PreScene();
				D3D8.d3dDevice.SetRenderState(CONST_D3DRENDERSTATETYPE.D3DRS_LIGHTING, (int)1);
				D3D8.d3dDevice.SetRenderState(CONST_D3DRENDERSTATETYPE.D3DRS_ZENABLE, (int)1);
				D3D8.d3dDevice.SetRenderState(CONST_D3DRENDERSTATETYPE.D3DRS_SPECULARENABLE, (int)1);
				D3D8.d3dDevice.SetRenderState(CONST_D3DRENDERSTATETYPE.D3DRS_ALPHABLENDENABLE, (int)1);
				D3D8.d3dDevice.SetRenderState(CONST_D3DRENDERSTATETYPE.D3DRS_CULLMODE, (int)CONST_D3DCULL.D3DCULL_CW);
				D3D8.d3dDevice.SetRenderState(CONST_D3DRENDERSTATETYPE.D3DRS_NORMALIZENORMALS, (int)1);
				mesh.RotY += ((float)Math.PI/128.0f);
				mesh.Z = -3.0f;
				CamY += 1.0f/32.0f;
				//setup the scene
				scn.SetProjection((float)Math.PI / 2, 100.0f);
				scn.SetCamera(4.0f, CamY, 25.0f, 0.0f, 0.0f, 0.0f);
				scn.SetLight(0, -5.0f, 18.0f, 36.0f, 0.9f, 0.9f, 0.9f, 0.8f, 0.001f, 0.001f, 0.0001f);
				mesh.Render();
				int r=0;
				//try to present the scene and if it doesn't work, react appropriately
				while((r=D3D8.PostScene(this.Handle)) != 0)
				{
					System.Threading.Thread.Sleep(2000);
					//if we need to reset the device:
					if(r == 1)
					{
						try
						{
							if(mesh != null)
							{
								mesh.Dispose();
								mesh = null;
							}
							if(scn != null)
							{
								scn.RemoveLight(0);
								scn.Dispose();
								scn = null;
							}
						}
						catch(Exception)
						{
						}
						try
						{
							System.Threading.Thread.Sleep(300);
							D3D8.Dispose();
							D3D8.d3d = null;
							D3D8.dx8 = null;
							System.Threading.Thread.Sleep(400);
							D3D8.dx8 = new DirectX8Class();
							try
							{
								D3D8.d3d = D3D8.dx8.Direct3DCreate();
							}
							catch(Exception)
							{
								goto tryagain;
							}
							D3D8.Initialize(this.Handle, 1024, 768, true, true);
							System.Threading.Thread.Sleep(300);
						}
						catch(Exception)
						{
							goto tryagain;
						}
						try
						{
							scn = new Scene(D3D8.d3dDevice);
							mesh = new XObject(D3D8.d3dDevice);
							mesh.Load("object.x", "texture", "texture\\refl_dc.tga");
							mesh.RotX = mesh.RotY = mesh.RotZ = 0;
							mesh.X = mesh.Y = mesh.Z = 0;
						}
						catch(Exception)
						{
						}
						tryagain:;
					}
					//if the device is lost
					if(r == 2)
					{
						System.Threading.Thread.Sleep(1000);
					}
				}
				end = System.Environment.TickCount - start;
				tps+=37122;
			}
			mesh = null;
			scn = null;
			//get rid of the device
			D3D8.Dispose();
			MessageBox.Show(this, "KTps: " + tps/end);
		}
 
Edited by - dacris on March 24, 2002 2:39:50 PM

Share this post


Link to post
Share on other sites
BTW, I''m using my own wrapper here.

Here''s the code for the PostScene() function:


public static int PostScene(System.IntPtr wndHandle)
{
try
{
d3dDevice.EndScene();
}
catch(Exception)
{
}
try
{
int r = d3dDevice.TestCooperativeLevel();
if(r != 0)
{
if(r == (int)CONST_D3DERR.D3DERR_DEVICENOTRESET)
{
return 1;//device needs reset
}
return 2;//device lost
}
}
catch(System.Exception)
{
return 2;
}
d3dDevice.Present((System.IntPtr)0, (System.IntPtr)0, wndHandle.ToInt32(), (System.IntPtr)0);
return 0;
}

Share this post


Link to post
Share on other sites