Click here to Skip to main content
13,451,557 members (45,875 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 25 May 2006

MDX WinForm with .NET Controls

, 25 May 2006
Rate this:
Please Sign up or sign in to vote.
This article shows how to use .NET controls in Windows Forms containing managed DirectX content


This article shows how to use .NET Windows Form controls with managed DirectX content. Most books/tutorials use a WinForm strictly with MDX content. But people are more interested in using WinForm controls together with MDX. This question is frequently asked and not well answered. Here, I present a small project using .NET 2.0 and Visual Studio 2005.


The most important part of this project is the DirectX device creation. Normally it's:

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

In order to make .NET controls such as menubar, etc. to co-exist with a DirectX control, I set up a panel to be used by DirectX.

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

To demonstrate that this works, I have provided a simple managed DirectX project with this project that can be downloaded. Here is the complete source code. Copy/paste and compile with .NET 2.0 managed 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
    /// <span class="code-SummaryComment"><summary></span>
    /// Summary description for Form1.
    /// <span class="code-SummaryComment"></summary></span>
    public class Form1 : System.Windows.Forms.Form
        private Device device = null;
        /// <span class="code-SummaryComment"><summary></span>
        /// Required designer variable.
        /// <span class="code-SummaryComment"></summary></span>
        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

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

        /// <span class="code-SummaryComment"><summary></span>
        /// We will initialize our graphics device here
        /// <span class="code-SummaryComment"></summary></span>
        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, 

        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)
                System.Drawing.Color.CornflowerBlue, 1.0f, 0);


            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.VertexFormat = CustomVertex.PositionNormalColored.Format;
            device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);



        /// <span class="code-SummaryComment"><summary></span>
        /// Clean up any resources being used.
        /// <span class="code-SummaryComment"></summary></span>
        protected override void Dispose( bool disposing )
            if( disposing )
                if (components != null) 
            base.Dispose( disposing );

        #region Windows Form Designer generated code
        /// <span class="code-SummaryComment"><summary></span>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// <span class="code-SummaryComment"></summary></span>
        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();
            // 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.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
                               new System.Windows.Forms.ToolStripItem[] {
            this.mDXFormWithMenuToolStripMenuItem.Name = 
            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.Name = "Form1";
            this.Text = "Form1";

        /// <span class="code-SummaryComment"><summary></span>
        /// The main entry point for the application.
        /// <span class="code-SummaryComment"></summary></span>
        static void Main() 
            using (Form1 frm = new Form1())
                // Show our form and initialize our graphics engine

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)


  • 25th May, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Fei Liu
Web Developer
United States United States
I currently work and live in NJ, US. I am interested in C/C++/CLR/C# technologies. My blog is

You may also be interested in...


Comments and Discussions

QuestionWhat is MDX? Pin
Jun Du26-May-06 4:22
memberJun Du26-May-06 4:22 
AnswerRe: What is MDX? Pin
Nishant Sivakumar26-May-06 4:38
staffNishant Sivakumar26-May-06 4:38 
GeneralRe: What is MDX? Pin
Nishant Sivakumar26-May-06 5:52
staffNishant Sivakumar26-May-06 5:52 
GeneralRe: What is MDX? Pin
Fei Liu26-May-06 6:31
memberFei Liu26-May-06 6:31 
GeneralRe: What is MDX? Pin
Jun Du27-May-06 10:51
memberJun Du27-May-06 10:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180318.3 | Last Updated 25 May 2006
Article Copyright 2006 by Fei Liu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid