Click here to Skip to main content
15,396,537 members
Articles / Multimedia / DirectX
Posted 26 Mar 2009

Tagged as


11 bookmarked

Windows Forms, Managed DirectX and C++. All In One

Rate me:
Please Sign up or sign in to vote.
4.56/5 (5 votes)
26 Mar 2009GPL31 min read
This project shows with an example how to combine Managed C++, Managed DirectX and Windows Forms.


This article presents a very clean and simple method to render in 3D using Managed DirectX, Windows Forms and C++ (Managed C++ in this case). The application is a simple 3D Point viewer where the points are obtained from a DataGridView that allows user editing and multiline Copy/Paste.

The November 2008 DirectX SDK download includes complete support for MC++ development: all DirectX API has been fully translated to MC++. This has strong implications for those who use C++ language and want .NET functionality and DirectX functionality.


This article assumes the reader has some knowledge of using Microsoft Visual Studio, a Managed C++ background and some basic concepts about Windows Forms. 

Using the Code

The project is created with Visual Studio 2008 and you will also need the DirectX SDK November 2008 installed.  

Please, remember to check if the references Microsoft.DirectX and Microsoft.DirectX.Direct3D are included in the project (see Project-> Properties-> Common Properties -> FrameWork and References).

The next code is a cut extracted from the Render routine in the example program. The data source for the 3D points rendering is an array of points called points (of type array<CustomVertex::PositionColored, 1>). Before rendering the points, it is necessary to setup the viewing parameters as follows:

/// <summary>
/// Render the 3D points vector.
/// </summary>   
//Clear the backbuffer and color
device->Clear(ClearFlags::Target | ClearFlags::ZBuffer, 
		System::Drawing::Color::Black, 1.0f, 0);
//Begin the scene

// View definitions		
Vector3 CameraPos(Bmaxx, Bmaxy, Bmaxz);
Vector3 Center((Bmaxx+Bminx)/2, (Bmaxy+Bminy)/2,(Bmaxz+Bminz)/2);
CameraPos = CameraPos + (CameraPos-Center);

// View
device->Transform->World = 
	Matrix::RotationZ(Environment::TickCount / 2500.0f); // a rotating world
device->Transform->Projection = 
	Matrix::OrthoLH(8.0f, 8.0f, 0.1f, 1000.0f); // field of view
device->Transform->View = 
	Matrix::LookAtLH(CameraPos,Center,  Vector3(0.0f, 0.0f, 1.0f)); // point of view

device->RenderState->Lighting = false;
device->RenderState->ZBufferEnable = true;

// Point size
device->SetRenderState(Direct3D::RenderStates::PointSize, 2.0f);
// Rendering of scene objects
device->VertexFormat = CustomVertex::PositionColored::Format;
device->DrawUserPrimitives(PrimitiveType::PointList, points->Length, points);

//End the scene

The data of the array points is extracted from a DataGridView using this simple procedure: 

/// <summary>
/// Update points vector from dataGridView1.
/// </summary>
try // using try to control data conversion errors
    // actualize points
    for (i = 0; i < points->Length; i++)
        points[i].X = (float)System::Convert::ToDouble(dataGridView1[0,i]->Value);
        points[i].Y = (float)System::Convert::ToDouble(dataGridView1[1,i]->Value);
        points[i].Z = (float)System::Convert::ToDouble(dataGridView1[2,i]->Value);
        points[i].Color = System::Drawing::Color::Red.ToArgb();
catch ( Exception^ exp ) 
    // Cell data format error
    MessageBox::Show( "Cell data format error: Row("+ i +")," + 
	exp->Message, "Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation );

Points of Interest

  • Mixing C++, DirectX and Windows Forms


  • Version 1.0 24-03-2009


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Spain Spain
No Biography provided

Comments and Discussions

GeneralGreat job Pin
joseph74it13-Jun-09 9:15
Memberjoseph74it13-Jun-09 9:15 
GeneralNice Clean Interface Pin
Jeffrey Walton26-Mar-09 12:22
MemberJeffrey Walton26-Mar-09 12:22 

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.