Advertisement Jump to content
Sign in to follow this  
Neil Kerkin

[.net] Managed DirectX Control

This topic is 4928 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 am trying to create a general managed DirectX control using C# Express 2005 similar to this OpenGL COM control. This is what I have so far (sourced mainly from Tom Millers excellent Managed DirectX book [wink]). This works great when placed on a form, however if more then one control is added, only one control displays properly. I imagine because of how the device member variable is created for each object. I have read that multiple viewports can be created using the swap-chain but I am unsure how to implement the swap chain in this fashion. My guess would have been to create a static device and backbuffer and then create a surface member for each instance and set the render target in the OnPaint method. But I couldn't get this to work. Also, I am envisioning that each viewport will have a different set of vertices to render and I am wondering how costly this context switch will be using managed code. Anyway any input would be greatly appreciated.
using System;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace CTWindows
	public partial class CT3DGraph : UserControl
		private Device device = null;
		private PresentParameters presentParams = null;

		private CustomVertex.PositionColored[] verts;

		public CT3DGraph()
			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);

		#region Initialise
		public void InitialiseGraphics()
			// Set our presentation parameters
			presentParams = new PresentParameters();
			presentParams.Windowed = true;
			presentParams.SwapEffect = SwapEffect.Discard;
			presentParams.DeviceWindowHandle = this.Handle;

			//create our device
			device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

			//initialise data

		private void initialiseObjects()
			verts = new CustomVertex.PositionColored[3];
			verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
			verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
			verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
			verts[1].Color = System.Drawing.Color.Black.ToArgb();
			verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
			verts[2].Color = System.Drawing.Color.Purple.ToArgb();

		#region Render 
		protected override void OnPaint(PaintEventArgs e)
			device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);



		private void SetupCamera()
			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4,
				 this.Width / this.Height, 1.0f, 100.0f);
			device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0f), new Vector3(),
				 new Vector3(0, 1, 0));

			device.RenderState.Lighting = false;
			device.Transform.World = Matrix.RotationZ((System.Environment.TickCount / 450.0f) / (float)Math.PI);

		private void drawObjects()
			device.VertexFormat = CustomVertex.PositionColored.Format;
			device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);


Share this post

Link to post
Share on other sites
Thanks Mykre, The Inner Realm control seems to be based around one control per form (though I could be wrong)

static int Main() {

CmlForm form = new CmlForm();
return 0;

I've avoided the MSDN Forums you mentioned thus far as there does not seem to be much activity on those boards.

Share this post

Link to post
Share on other sites
OK so I've checked out the MDX Library which seems to be a lot closer to what I want to do, however even with this control (modified to work with the latest DX SDK) you seem to only be able to render to one target at a time. The community on this site seems dead also.

Surely someone, somewhere has created a control of this nature using Manaed DirectX.

I'll simplify my question. Doesn't anyone know whether what I am trying to do is actually possible? ie create a generic DirectX control.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!