65.9K
CodeProject 正在变化。 阅读更多。
Home

MDX WinForm 与 .NET 控件

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.44/5 (10投票s)

2006年5月25日

CPOL
viewsIcon

62940

downloadIcon

323

本文展示了如何在包含托管 DirectX 内容的 Windows Forms 中使用 .NET 控件。

引言

本文展示了如何在托管 DirectX 内容的 Windows Forms 中使用 .NET Windows Form 控件。大多数书籍/教程都只使用完全由 MDX 内容组成的 WinForm。但人们更感兴趣的是将 WinForm 控件与 MDX 一起使用。这个问题经常被提出,但答案并不完善。在这里,我提供了一个使用 .NET 2.0 和 Visual Studio 2005 的小型项目。

技术

这个项目最重要的部分是 DirectX 设备的创建。通常情况下是

device = new Device(0, DeviceType.Hardware, this, 
         CreateFlags.SoftwareVertexProcessing, presentParams);
'this' is the alias of 'Form' used to represent this winForm.

为了使 .NET 控件(例如菜单栏等)与 DirectX 控件共存,我设置了一个面板供 DirectX 使用。

private System.Windows.Forms.Panel panel1;
// Initialize panel1
device = new Device(0, DeviceType.Hardware, panel1, 
         CreateFlags.SoftwareVertexProcessing, presentParams);

为了演示这一点,我提供了一个简单的托管 DirectX 项目,可以与此项目一起下载。以下是完整的源代码。使用 .NET 2.0 托管 DirectX 复制/粘贴并编译。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Chapter1Code
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private Device device = null;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        private Panel panel1;
        private MenuStrip menuStrip1;
        private ToolStripMenuItem mDXFormWithMenuToolStripMenuItem;
        private ToolStripMenuItem exitToolStripMenuItem;
        private float angle = 0.0f;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            this.Size = new Size(800, 600);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | 
                          ControlStyles.Opaque, true);
        }

        /// <summary>
        /// We will initialize our graphics device here
        /// </summary>
        public void InitializeGraphics()
        {
            // Set our presentation parameters
            PresentParameters presentParams = new PresentParameters();

            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;

            // Create our device
            device = new Device(0, DeviceType.Hardware, panel1, 
                     CreateFlags.SoftwareVertexProcessing, 
                     presentParams);            
        }

        private void SetupCamera()
        {            
            device.RenderState.CullMode = Cull.None;
            device.Transform.World = Matrix.RotationAxis(new Vector3(angle / 
                                     ((float)Math.PI * 2.0f), 
                                     angle / ((float)Math.PI * 4.0f), 
                                     angle / ((float)Math.PI * 6.0f)), 
                                     angle / (float)Math.PI);
            angle += 0.1f;

            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 = true;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {            
            device.Clear(ClearFlags.Target, 
                System.Drawing.Color.CornflowerBlue, 1.0f, 0);

            SetupCamera();

            CustomVertex.PositionNormalColored[] verts = 
                  new CustomVertex.PositionNormalColored[3];
            verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
            verts[0].Normal = new Vector3(0.0f, 0.0f, -1.0f);
            verts[0].Color = System.Drawing.Color.White.ToArgb();
            verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
            verts[1].Normal = new Vector3(0.0f, 0.0f, -1.0f);
            verts[1].Color = System.Drawing.Color.White.ToArgb();
            verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
            verts[2].Normal = new Vector3(0.0f, 0.0f, -1.0f);
            verts[2].Color = System.Drawing.Color.White.ToArgb();

            device.Lights[0].Type = LightType.Point;
            device.Lights[0].Position = new Vector3();
            device.Lights[0].Diffuse = System.Drawing.Color.White;
            device.Lights[0].Attenuation0 = 0.2f;
            device.Lights[0].Range = 10000.0f;
            
            device.Lights[0].Enabled = true;

            device.BeginScene();
            device.VertexFormat = CustomVertex.PositionNormalColored.Format;
            device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
            device.EndScene();

            device.Present();

            this.Invalidate();
        }

        /// <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 Windows Form 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()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.mDXFormWithMenuToolStripMenuItem = 
                 new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = 
                 new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 24);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(307, 275);
            this.panel1.TabIndex = 0;
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mDXFormWithMenuToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(307, 24);
            this.menuStrip1.TabIndex = 1;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // mDXFormWithMenuToolStripMenuItem
            // 
            this.mDXFormWithMenuToolStripMenuItem.DropDownItems.AddRange(
                               new System.Windows.Forms.ToolStripItem[] {
            this.exitToolStripMenuItem});
            this.mDXFormWithMenuToolStripMenuItem.Name = 
                 "mDXFormWithMenuToolStripMenuItem";
            this.mDXFormWithMenuToolStripMenuItem.Size = 
                 new System.Drawing.Size(117, 20);
            this.mDXFormWithMenuToolStripMenuItem.Text = "MDX form with Menu";
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.exitToolStripMenuItem.Text = "Exit";
            this.exitToolStripMenuItem.Click += 
                 new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(307, 299);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.menuStrip1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main() 
        {
            using (Form1 frm = new Form1())
            {
                // Show our form and initialize our graphics engine
                frm.Show();
                frm.InitializeGraphics();
                Application.Run(frm);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}

历史

  • 2006 年 5 月 25 日:首次发布
© . All rights reserved.