Anyone rendering in a .net user control?

Started by
2 comments, last by Kate18 19 years, 9 months ago
Hi, I am trying to make direct3d render in a control I made. The control is just a picturebox filled to the whole control. In the controls constructor I initialize d3d and render to the picture box. The trouble I am having is I cant get it to render constantly. I have my render code inside the controls OnPaint event. I have tried heaps of stuff but the results i get vary from, rendering on startup then dissappearing, crashing when minimizing, strange screen on resizing ect I just cant figure it out. Does anyone know how to render to a user control, like, what events should i override ect Any help with this would be great, Thanks :)
Advertisement
The OnRender method is only meant to handle GDI painting operations, and will only be called when your window is invalidated. This is not what you want to use with DirectX. The usual course of action is to override the OnIdle method and call your D3D rendering from within that.

For an example user control that does just the bare basics, the source below will work. Take note this handles no options or errors, but it works for me :).
using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms;using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;namespace Impress{	/// <summary>	/// Summary description for Canvas3D.	/// </summary>	public class Canvas3D : System.Windows.Forms.UserControl	{		/// <summary> 		/// Required designer variable.		/// </summary>		private System.ComponentModel.Container components = null;		private Device device = null;		private PresentParameters presentParams = null;		private bool windowed = true;		public Canvas3D()		{			// This call is required by the Windows.Forms Form Designer.			InitializeComponent();			Application.Idle += new EventHandler(OnIdle);		}		public bool Initialize()		{			try			{				this.BuildPresentParams();				CreateFlags flags = CreateFlags.HardwareVertexProcessing;				this.device = new Device(0, DeviceType.Hardware, this, flags, this.presentParams);			}			catch (Exception e)			{				return false;			}			return true;		}		private void BuildPresentParams()		{			this.presentParams = new PresentParameters();			this.presentParams.Windowed = this.windowed;			this.presentParams.BackBufferCount = 1;			this.presentParams.SwapEffect = SwapEffect.Discard;			this.presentParams.PresentFlag = PresentFlag.None;			if (this.windowed)			{				this.presentParams.BackBufferWidth = this.ClientRectangle.Width;				this.presentParams.BackBufferHeight = this.ClientRectangle.Height;				this.presentParams.PresentationInterval = PresentInterval.Immediate;				this.presentParams.DeviceWindow = this;			}		}			private void Render()		{			this.device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);			this.device.BeginScene();			this.device.EndScene();			this.device.Present();		}		/// <summary> 		/// Clean up any resources being used.		/// </summary>		protected override void Dispose( bool disposing )		{			if( disposing )			{				if(components != null)				{					components.Dispose();				}			}			base.Dispose( disposing );		}		#region Component Designer generated code		/// <summary> 		/// Required method for Designer support - do not modify 		/// the contents of this method with the code editor.		/// </summary>		private void InitializeComponent()		{			components = new System.ComponentModel.Container();		}		#endregion		private void OnIdle(object sender, EventArgs e)		{			if (this.device != null)			{				this.Render();			}		}	}}
Well I just wrote code for my own controls. I'm not sure what exactly you are trying to though, my focus was more towards an in game GUI. For my windows i just did a TransformedTextured square with some widgets i also programmed for hiding/moving etc. If you want to get away from all of the default looks the presets give you I would write your own stuff.
--------------------------------------------------------------------------Michael Schuld
Ok Premandrake, I'm impressed :)

I'm still having a few problems though. Before rendering in idle I was rendering in OnPaint and just calling this.Invalidate() at the end. Anyway, this idle method has made it smoother but my screen is still all fuzzy. I have noticed if I make the control small on my form it renders fine but when i make it bigger or set dock style to full it goes fuzzy. I'm starting to think it may be my graphic card and not the program.
I was hoping someone could test it on their machine. Its 14kb rar file. I could email it. That would be great. :)

Oh, its a modeller, not a game :)

Thankyou :)

This topic is closed to new replies.

Advertisement