![]()
/*****************************************/
/*★ DirectX で Form を表示    前田 稔 ★*/
/*****************************************/
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace DeviceTutorial
{
    public class CreateDevice : Form
    {
        Device device = null;           // Direct Device の定義
        // CreateDevice の Constructor
        public CreateDevice()
        {
            // Window のサイズを設定
            this.ClientSize = new System.Drawing.Size(400,300);
            // caption の設定
            this.Text = "D3D Tutorial 01: CreateDevice";
        }
        public bool InitializeGraphics()
        {
            try
            {
                // Now let's setup our D3D stuff
                PresentParameters presentParams = new PresentParameters();
                presentParams.Windowed=true;
                presentParams.SwapEffect = SwapEffect.Discard;
                device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
                return true;
            }
            catch (DirectXException)
            { 
                return false; 
            }
        }
        // OnPaint() から呼ばれる描画処理
        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();
        }
        // Window の描画 OnPaint() をオーバーロード
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.Render();      // Render on painting
        }
        // OnKeyPress() をオーバーロード
        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
        }
        //☆ Main() メソッド
        static void Main()
        {
            // using で資源の解放を確実に行う
            using (CreateDevice frm = new CreateDevice())
            {
                if (!frm.InitializeGraphics())  // Initialize Direct3D
                {
                    MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.");
                    return;
                }
                frm.Show();
                // メッセージループ
                while(frm.Created)
                {
                    frm.Render();
                    Application.DoEvents();
                }
            }
        }
    }
}
 | 
| Microsoft.DirectX | 
| Microsoft.DirectX.Direct3D | 
| Microsoft.DirectX.Direct3DX | 
| System | 
| System.Drawing | 
| System.Windows.Forms | 


![]()
    static void Main()
    {
        // using で資源の解放を確実に行う
        using (CreateDevice frm = new CreateDevice())
        {
     | 
    if (!frm.InitializeGraphics())  // Initialize Direct3D
    {
        MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.");
        return;
    }
     | 
    frm.Show();
    // メッセージループ
    while(frm.Created)
    {
        frm.Render();
        Application.DoEvents();
    }
     | 
    public class CreateDevice : Form
    {
        Device device = null;           // Direct Device の定義
        // CreateDevice の Constructor
        public CreateDevice()
        {
            // Window のサイズを設定
            this.ClientSize = new System.Drawing.Size(400,300);
            // caption の設定
            this.Text = "DirectX CreateDevice";
        }
     | 
        public bool InitializeGraphics()
        {
            try
            {
                // Now let's setup our D3D stuff
                PresentParameters presentParams = new PresentParameters();
                presentParams.Windowed=true;
                presentParams.SwapEffect = SwapEffect.Discard;
                device = new Device(0, DeviceType.Hardware, this, 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();
    }
     | 
        // Window の描画 OnPaint() をオーバーロード
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.Render();      // Render on painting
        }
     | 
        // OnKeyPress() をオーバーロード
        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
        }
     | 
![]()
| ファイル(フォルダー) | 説明 | 
|---|---|
| DXForm.csproj | プロジェクトファイル | 
| DXForm.cs | フォームのソースファイル | 
![]()
| BadImageFormatException はハンドルされませんでした。 | 

![]()