• Advertisement

Archived

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

ALT+TAB in C#.NET Full Screen - Working example needed

This topic is 5064 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 have searched on many boards, including this one for a simple example of successfully working code for handling FULL SCREEN MODE ALT+TAB in C#.NET to no avail. Can anyone help me and post a working example Many thanks

Share this post


Link to post
Share on other sites
Advertisement
(Double Post...)
But anyway are you trying to restore the device after you minimize the app and then come back? If So here''s what i do.

basically have a minimized boolean and when the form is minimized set it to true, and on the paint handler chenck if minimized = true and if so do device.reset(device.presntparams)

if you do this you have to have a handler on the device''s Resize event with the fillowing code { e.Cancel = True }...

hope that helps...

i would post code, but i am using vb until either i get C# or until .Net 2005 comes out...

Share this post


Link to post
Share on other sites
Oh, and this is the modified code with the resizing handler I was missing


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Testa
{
public class Game : System.Windows.Forms.Form
{
static void Main ()
{
Game app = new Game();
app.InitializeGraphics();
app.Show();
while (app.Created)
{
app.Render();
Application.DoEvents();
}
}

private Device device;
private VertexBuffer vertices;

// Has the device been lost and not reset?
private bool deviceLost;

// We'll need these to Reset successfully, so hold them here
private PresentParameters pres = new PresentParameters();

protected void CancelResize(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
Trace.WriteLine("CancelResize");
}

protected bool InitializeGraphics()
{
Format current = Manager.Adapters[0].CurrentDisplayMode.Format;

// Set up our presentation parameters as usual
pres.Windowed = false;
pres.BackBufferFormat = current;
pres.BackBufferCount = 1;
pres.BackBufferWidth = 640;
pres.BackBufferHeight = 480;
//pres.PresentationInterval = PresentInterval.Immediate;//PresentInterval.One;

pres.SwapEffect = SwapEffect.Discard;

device = new Device(0, DeviceType.Hardware, this ,
CreateFlags.SoftwareVertexProcessing, pres);

// Hook the DeviceReset event so OnDeviceReset will get called every
// time we call device.Reset()
device.DeviceReset += new EventHandler( this .OnDeviceReset);

// Similarly, OnDeviceLost will get called every time we call
// device.Reset(). The difference is that DeviceLost gets called
// earlier, giving us a chance to do the cleanup that needs to
// occur before we can call Reset() successfully
device.DeviceLost += new EventHandler( this .OnDeviceLost);
device.DeviceResizing += new System.ComponentModel.CancelEventHandler(this.CancelResize);
// Do the initial setup of our graphics objects
SetupDevice();
return true ;
}



protected void OnDeviceReset( object sender, EventArgs e)
{
// We use the same setup code to reset as we do for initial creation
SetupDevice();
}

protected void OnDeviceLost( object sender, EventArgs e)
{
// Clean up the VertexBuffer
vertices.Dispose();
}
protected void SetupDevice()
{
// Set up the device's RenderStates
device.RenderState.Lighting = false ;
device.RenderState.CullMode = Cull.None;

// And create the VertexBuffer
vertices = CreateVertexBuffer(device);
}

protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == System.Windows.Forms.Keys.Escape)
this.Close();

base.OnKeyUp(e);
}


protected VertexBuffer CreateVertexBuffer(Device device)
{
VertexBuffer buf = new VertexBuffer(
typeof (CustomVertex.PositionColored), // What type of vertices
3, // How many
device, // The device
0, // Default usage
CustomVertex.PositionColored.Format, // Vertex format
Pool.Default); // Default pooling

CustomVertex.PositionColored[] verts =
(CustomVertex.PositionColored[]) buf.Lock(0, 0);

int i = 0;
verts[i++] = new CustomVertex.PositionColored(
0, 1, 0, Color.Red.ToArgb());

verts[i++] = new CustomVertex.PositionColored(
-0.5F, 0, 0, Color.Green.ToArgb());

verts[i++] = new CustomVertex.PositionColored(
0.5F, 0, 0, Color.Blue.ToArgb());

buf.Unlock();

return buf;
}





protected void SetupMatrices()
{
float angle = Environment.TickCount / 500.0F;
device.Transform.World = Matrix.RotationY(angle);
device.Transform.View = Matrix.LookAtLH( new Vector3(0, 0.5F, -3),
new Vector3(0, 0.5F, 0), new Vector3(0, 1, 0));

device.Transform.Projection =
Matrix.PerspectiveFovLH(( float )Math.PI/4.0F, 1.0F, 1.0F, 5.0F);

}

protected void Render()
{
if ((deviceLost) || (System.Windows.Forms.Form.ActiveForm != this))
{
// Try to get the device back

AttemptRecovery();
}

// If we couldn't get the device back, don't try to render
if (deviceLost)
{
return ;
}

// Clear the back buffer
device.Clear(ClearFlags.Target, Color.Bisque, 1.0F, 0);

// Ready Direct3D to begin drawing
device.BeginScene();

// Set the Matrices
SetupMatrices();

// We're going to draw colored vertices in 3D
device.VertexFormat =
CustomVertex.PositionColored.Format;

// Draw the scene - 3D Rendering calls go here
device.SetStreamSource(0, vertices, 0);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

// Indicate to Direct3D that we're done drawing
device.EndScene();

try
{
// Copy the back buffer to the display
device.Present();
}

catch (DeviceLostException)
{
// Indicate that the device has been lost
deviceLost = true ;

// Spew a message into the output window of the debugger
Debug.WriteLine("Device was lost");


}


}





protected void AttemptRecovery()
{
try
{
device.TestCooperativeLevel();
}

catch (DeviceLostException)
{
}


catch (DeviceNotResetException)
{
try
{
device.Reset(device.PresentationParameters);
deviceLost = false ;
// Spew a message into the output window of the debugger

Debug.WriteLine("Device successfully reset");

}


catch (DeviceLostException)


{


// If it's still lost or lost again, just do


// nothing


}


}


}

private void InitializeComponent()
{
//
// Game
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Game";
this.Load += new System.EventHandler(this.Game_Load);

}




}


}


[edited by - robpearmain on April 13, 2004 6:48:08 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by robpearmain
Awesome, awesome, awesome

I can''t believe I never found that link


I had just posted it the other day, so not surprising at all. I''m glad it helped. I was very annoyed that in all the info out there no one had put together a nice simple example of fullscreen & lost devices. So I figured I''d better do it.

Stay Casual,

Ken
Drunken Hyena

Share this post


Link to post
Share on other sites

  • Advertisement