[MDX] Initializing in a picture box

Started by
10 comments, last by Programmer101 16 years, 11 months ago
How can you initialize the directx viewport in a picture box? Rather than the entire window?
Advertisement
Don't use a picture box, use a panel. All you have to do is pass the panel when creating the device instead of the form.

Device = new Direct3D.Device(
0,
DeviceType,
Control, // <- here
CreateFlags,
PresentParameters );
Okay. Thanks!
I get an error initializing the device. I have tried this:

new Device(0, DeviceType.Hardware, panel1, CreateFlags.SoftwareVertexProcessing, presentParams);

and this:

new Device(0, DeviceType.Hardware, this.panel1, CreateFlags.SoftwareVertexProcessing, presentParams);

The device just will not create. What is wrong?
try this.panel1.Handle
Ok. Thats better and it doesn't fail in initializing but i get this debug error:

An unhandled exception of type 'System.NullReferenceException' occurred in Demon Heart Construction Set.exe

Additional information: Object reference not set to an instance of an object.

Thanks for all of the help.
The error tells you your trying to use an object that is null. This means you have an object that has not been instanciated (ObjectClass object = new ObjectClass()).

You should be running your application in debug mode so when you run it, it will show you the line the error is occuring. Once you know the line you will see the object your trying to use has not been instanciated.
Okay. Its the panel that is giving the error.

I'm not completely sure what you mean but i tried this and it didn't fix it:

this.panel1 = new Panel();

it just gave the same error. I am doing this with the form design thing if that makes a difference.
If your using the form designer to draw the panel on your form, you don't need to manually instanciate it. It will do that for you.

I suspect the the form hasn't been drawn yet before your trying to access the panel. Try placing a this.Show() before you create the device.
Its still not working. This is the code I am using now:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;namespace Demon_Heart_Construction_Set{    public partial class Form1 : Form    {        private Panel panel1;        // Our global variables for this project		Device device = null; // Our rendering device		public Form1()		{			// Set the initial size of our form			this.ClientSize = new System.Drawing.Size(400,300);			// And it's caption			this.Text = "Demon Heart Construction Set";			// Load our icon from the resources of the .exe		}				public bool InitializeGraphics()		{			try			{				// Now let's setup our D3D stuff                this.Show();				PresentParameters presentParams = new PresentParameters();				presentParams.Windowed=true;				presentParams.SwapEffect = SwapEffect.Discard;				device = new Device(0, DeviceType.Hardware, this.panel1.Handle, CreateFlags.SoftwareVertexProcessing, presentParams);				return true;			}			catch (DirectXException)            {                return false;             }		}		private void Render()		{			if (device == null) 				return;			//Clear the backbuffer to a blue color 			device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);			//Begin the scene			device.BeginScene();						// Rendering of scene objects can happen here    			//End the scene			device.EndScene();			device.Present();		}		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)		{			this.Render(); // Render on painting		}		protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)		{			if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)				this.Close(); // Esc was pressed		}		/// <summary>		/// The main entry point for the application.		/// </summary>		static void Main() 		{            using (Form1 frm = new Form1())            {                if (!frm.InitializeGraphics()) // Initialize Direct3D                {                    MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.");                    return;                }                frm.Show();                // While the form is still valid, render and process messages                while(frm.Created)                {                    frm.Render();                    Application.DoEvents();                }            }		}        private void InitializeComponent()        {            this.panel1 = new System.Windows.Forms.Panel();            this.SuspendLayout();            //             // panel1            //             this.panel1.Location = new System.Drawing.Point(222, 12);            this.panel1.Name = "panel1";            this.panel1.Size = new System.Drawing.Size(400, 300);            this.panel1.TabIndex = 0;            //             // Form1            //             this.ClientSize = new System.Drawing.Size(655, 398);            this.Controls.Add(this.panel1);            this.Name = "Form1";            this.Load += new System.EventHandler(this.Form1_Load);            this.ResumeLayout(false);            this.Show();        }        private void Form1_Load(object sender, EventArgs e)        {        }    }}


Thanks for your help!

This topic is closed to new replies.

Advertisement