すごく簡単です。主な変更点は次の通りです。
using System; using System.Drawing; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace Project2 { /// <summary> /// md3d2 の概要の説明です。 /// </summary> public class md3d2:Form { private System.Windows.Forms.PictureBox pictureBox1; public md3d2():base() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.SuspendLayout(); this.pictureBox1.Location = new System.Drawing.Point(8, 8); this.pictureBox1.Name = "pictureBox2"; this.pictureBox1.Size = new System.Drawing.Size(240, 184); this.pictureBox1.TabIndex = 1; this.pictureBox1.TabStop = false; this.Controls.Add(this.pictureBox1); this.ResumeLayout(false); this.MinimumSize=new Size(80,60); this.ClientSize = new Size(300,300); this.Text ="Direct3D-My"; } private Device device_; private PresentParameters presentParam_; /// <summary> /// 立方体メッシュ /// </summary> private Mesh boxMesh_; /// <summary> /// Direct3Dの初期化を行います。 /// </summary> /// <returns>初期化が成功したかどうか</returns> public bool DXInitialize() { try { presentParam_ = new PresentParameters(); presentParam_.Windowed =true; presentParam_.SwapEffect = SwapEffect.Discard; device_ = new Device(0,DeviceType.Hardware,pictureBox1 ,CreateFlags.HardwareVertexProcessing,presentParam_); //メッシュを作成 creatMesh(); return true; } catch { return false; } } /// <summary> /// メッシュを作成する /// </summary> private void creatMesh() { boxMesh_ = Mesh.Box(device_,2,2,2); } public void Render() { if(device_==null)return; if(this.WindowState ==FormWindowState.Minimized)return; //回転と移動を行う device_.Transform.World = Matrix.RotationY(Environment.TickCount/600f); //カメラの設定を行う device_.Transform.View = Matrix.LookAtLH( new Vector3( 5.0f, 5.0f, 5.0f ), new Vector3( 0.0f, 0.0f, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) ); device_.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)this.pictureBox1.Width / (float)this.pictureBox1.Height, 3.0f, 15.0f); //device_.RenderState.Lighting =false; //ライトの設定 device_.Lights[0].Direction =Vector3.Normalize(new Vector3(-1,-2,-3)); device_.Lights[0].Type = LightType.Directional; device_.Lights[0].Diffuse = Color.White; device_.Lights[0].Ambient = Color.FromArgb(255,40,40,40); device_.Lights[0].Enabled =true; device_.Lights[0].Update(); //マテリアルの設定 Material mat =new Material(); mat.AmbientColor = new ColorValue(1.0f,1.0f,0.1f); mat.DiffuseColor = new ColorValue(1.0f,1.0f,0.1f); device_.Material = mat; device_.Clear(ClearFlags.Target,Color.Blue,1.0f,0); device_.BeginScene(); boxMesh_.DrawSubset(0); device_.EndScene(); try { //更新 device_.Present(); } catch(DeviceLostException) { resetDevice(); } } /// <summary> /// デバイスのリセットを行う /// </summary> private void resetDevice() { int result; if(!device_.CheckCooperativeLevel(out result)) { if(result ==(int)ResultCode.DeviceLost) { //ちょっと待つ System.Threading.Thread.Sleep(10); } else if(result ==(int)ResultCode.DeviceNotReset) { device_.Reset(presentParam_); } } } } /// <summary> /// エントリクラス /// </summary> class Program { public static void Main() { using(md3d2 dxform =new md3d2()) { if(!dxform.DXInitialize()) { MessageBox.Show("Diret3Dの初期化に失敗しました。" ,"初期化の失敗"); return; } dxform.Show(); while(dxform.Created) { dxform.Render(); Application.DoEvents(); } } } } }
これだけです。
device_ = new Device(0,DeviceType.Hardware,pictureBox1
,CreateFlags.HardwareVertexProcessing,presentParam_);
適当に作ったコントロールを渡してあげてください。