Jump to content
  • Advertisement
Sign in to follow this  

[.net] Using Managed DirectX in a Forms.Panel in C#, Direct3D.NotAvailableException

This topic is 2485 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

My first post. I hope the formatting is right, but forgive me if I screw it up. I am trying to get Managed DirectX working in C#. I have it working while drawing to the main form, but when I try to move it into a panel I get the following exception when I try to minimize the form. An unhandled exception of type 'Microsoft.DirectX.Direct3D.NotAvailableException' occurred in microsoft.directx.direct3d.dll Additional information: Error in the application. My debugger (MS Visual Studio.NET debugger) just kicks me out to the end of the program, and when I try to put a try/catch block around my draw function, it doesn't catch it. I put a stop in my resize handler, but it doesn't get triggered before the program exceptions out. My code is setup thusly: Important member variables:
private Device device;
private DXPanel panel1; //I explain this class later

Initialization:
public MainForm()
{
	InitializeComponent();

	PresentParameters presentParams = new PresentParameters();
	presentParams.Windowed = true;
	presentParams.SwapEffect = SwapEffect.Flip;
	presentParams.DeviceWindow = this.panel1;
	device = new Device(0, DeviceType.Hardware, panel1, CreateFlags.SoftwareVertexProcessing, presentParams);
}

Paint Event Handler:
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
	device.Clear(ClearFlags.Target, Color.DarkSlateBlue , 1.0f, 0);
	device.Present();
}

Also, in order to stop it from flickering when resizing, i inherited my own panel class from Forms.Panel. I'm not sure if this is the right way to do this at all:
public class DXPanel : Panel
{
	public DXPanel() : base()
	{ 
		SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
	}
}

Of course, I've left out some of the irrelevant code (Designer Generated). Can anyone help me with this? I am new to DirectX, as all the graphics I've ever done has been in OpenGL. I am also relatively new to Windows programming in general.

Share this post


Link to post
Share on other sites
Advertisement
Try creating your own panel control inherited from the normal panel. Then on the new DXPanel Controls constructor create the DX Device.

Share this post


Link to post
Share on other sites
Nope, still does the same thing. I already had the derived class as posted above and I changed it to this:

public class DXPanel : Panel
{
private Device device;

public DXPanel() : base()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);

PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Flip;
presentParams.DeviceWindow = this;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}

public Device Device
{
get
{
return device;
}
}
}



Am I setting up the PresentParameters correctly? I have no idea what's going wrong and debugging hasn't been getting anywhere.

There doesn't seem to be an event for the minimization of a Windows.Form, am I just missing it? This would be useful because I could set a breakpoint in a handler for it and try to catch the program before it hits the exception.

What happens to the component Controls of a Form when the form is minimized? It seems like Direct3D is somehow losing it's hold (very technical term, I know) on the Panel when it's minimized. They aren't disposed or anything like that, are they?

Share this post


Link to post
Share on other sites
I fixed it!

Apparently, since i had the panel set to be anchored to all size of the forms, when I minimized the form the size of the panel was set to 0, 0, which Direct3D doesn't like. So, I fixed it by simply overriding the OnResize() method of the form and changing the anchoring to System.Windows.Forms.Anchorstyles.None ala:

protected System.Windows.Forms.FormWindowState TheStateOfTheWindow;
private const System.Windows.Forms.AnchorStyles ALL_ANCHOR = System.Windows.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Left
| System.Windows.Forms.AnchorStyles.Right;
protected override void OnResize(EventArgs e)
{
if (TheStateOfTheWindow != this.WindowState)
{
this.panel1.Anchor = (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)?
System.Windows.Forms.AnchorStyles.None : ALL_ANCHOR;
TheStateOfTheWindow = this.WindowState;
}
base.OnResize (e);
}


Kind of a hack, but it works.

Share this post


Link to post
Share on other sites
the code works fine, but I get stucked after one frame is rendered. Once the panel gets drawn the whole application freezes out. Can you help me please?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!