Tuesday, 24 April 2007

MDX-101: A simple window

To get a simple DirectX window running inside a panel on a form using Managed DirectX do the following:

1) Create a new WinForms project
2) On the main form, drop a panel (panel1) onto the form.
3) Change the Main() function in Program.cs to look as follows:

static void Main()
{
using (Form1 frm = new Form1())
{
frm.Show();

while (frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
}

4) Put the following code into Form1.cs:

private Device device;

public Form1()
{
InitializeComponent();

InitDirectX();
}

void InitDirectX()
{
PresentParameters pp = new PresentParameters();
pp.SwapEffect = SwapEffect.Discard;
pp.Windowed = true;

device = new Device(0, DeviceType.Hardware, panel1,
CreateFlags.SoftwareVertexProcessing, pp);
}

internal void Render()
{
device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
device.BeginScene();

device.EndScene();
device.Present();
}


Don't forget to reference Microsoft.DirectX.

That's it.

No comments: