XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
My XNA Book
       Go to section on this site

Additional info

Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


Initializing the Device

In this chapter, we are going to create the device. In short, a device is a direct link to your graphical adapter. It is an object that gives you direct access to the piece of hardware inside your computer.

To start, you have to declare the device as a variable within your WinForm-class. Just add this line as the first line in your class :

 private Device device;

Now, we are going to create a method InitializeDevice() to activate the device. Here's the code, I'll explain it afterwards:

 public void InitializeDevice()
     PresentParameters presentParams = new PresentParameters();
     presentParams.Windowed = true;
     presentParams.SwapEffect = SwapEffect.Discard;
     device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

The first line creates the Presentation Parameters, which we will need to tell the device how to behave. We will tell the device that we don't want a fullscreen application, and to discard the Swapeffect, so you write to the device immediately, and not to an extra back buffer that will be presented (= swapped) at runtime. Then, the device is created. The 0 selects the first graphical adapter in your PC. We want to render the graphics using the hardware. If you don't have a hardware card, you can use DeviceType.Reference, which supports all possible features, but is a lot slower. Next we bind 'this' window to the device and we for now we want all 'vertex processing' to happen on the CPU. More on vertices in the next chapter. Finally we pass our presentParams, and our device has been created!

Of course, we have to call this method, so add this line to your Main method :


When you try to compile your program now, you’ll probably get some errors like ‘The type or namespace name 'DirectX' does not exist in the namespace’. Make sure you add references to Microsoft.DirectX and Microsoft.Direct3D by clicking Project -> Add References to correct these errors. If you have multiple versions installed of these references, make sure you select references of the same version. If these 2 references aren’t included in your list, check if you have installed the full (free) Microsoft DirectX SDK. If they’re still missing, check out the first paragraphs of ‘Linking to the Device’ in the C++ part of this tutorial.

Running this code will still give you an empty form, but in the background the device has been initialized! Next we are going to overwrite the OnPaint method, so we can control what to draw on the screen. Add the following code below the InitializeDevice method:

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

This method will be called every time something is drawn to the screen. The Clear method will fill the window with a solid color, darkslateblue in our case. The ClearFlags indicate what we actually want to clear, in our case the target window. To actually update our display, we have to Present the updates to the device. Running this code will give you a blueish window. For even more stunning results, read on!

DirectX Tutorial 2 - Linking to the Device

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !

Click here to go to the forum on this chapter!

Or click on one of the topics on this chapter to go there:
  • Problem in Linking effect.fx File
          I attached in my project by option of adding an ex...
          Thought this might help beginners (like me, heh): ...
  • InitializeDevice() problem
          in source code InitializeDevice() is underlining w...
  • BadImageFormatException
          I really want to thank you for this tutorial its r...
  • Red X
          When I ran the application all I saw was a red X a...
  • Specify part of a form for the device?
          Howdy, I would find it useful to be able to...
  • stem.MissingFieldException
          On step 2 of your tutorial I get the following err...
  • Color command not recognized
          in the line device.Clear(ClearFlags.Tar...
  • error@onpaint method
          I'm getting this error message when I insert the ...
  • low FPS without Firefox
          Hi, I have a strange problem. While is Firefox ru...
  • prob with 2nd step of dx tutorial
          hi, i have an exception when i run the second st...
  • Form or any of Forms?
          Can the DX device be linked to any of System.Windo...
  • DirectX and Menu Bar
          Does anyone know how I can create a menu bar with ...
  • Device Creation Problem
          Hi, Your approach is just fantastic. Clearly ai...
  • Dispose() function?
          Hi! Thanx for this very fine tutorial! So fa...
  • Error exception
          What is error exception : Microsoft.DirectX.Direct...
  • Direct3D.dll problem?!?
          I did the 2nd step of the DirectX C# Tutorial. an...
  • Getting an error
          Hi , im not good at programming but trying anyway ...
  • device = null
          Hi, first of all congrats to your absolute fantas...
  • This error appears : when using Windowed
          The line below causes an error: Microsoft.DirectX...
  • Device Not Found
          I got the following Error The type or namespace...

    Here is the complete code:

     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 DirectX_Tutorial
         public class WinForm : System.Windows.Forms.Form
             private Device device;
             private System.ComponentModel.Container components = null;
             public WinForm()
             public void InitializeDevice()
                 PresentParameters presentParams = new PresentParameters();
                 presentParams.Windowed = true;
                 presentParams.SwapEffect = SwapEffect.Discard;
                 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
             protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
                 device.Clear(ClearFlags.Target, Color.DarkSlateBlue , 1.0f, 0);
             protected override void Dispose (bool disposing)
                 if (disposing)
                     if (components != null)
             private void InitializeComponent()
                 this.components = new System.ComponentModel.Container();
                 this.Size = new System.Drawing.Size(500,500);
                 this.Text = "DirectX Tutotial";
             static void Main()
                 using (WinForm our_dx_form = new WinForm())


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !

    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2011 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award

    2007 - 2011 MVP Award
    DirectX - XNA


    XNA 2.0 Recipes Book (8)
    XNA 3.0 Recipes Book (8)
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Tutorials (160)
    XNA 4.0 using C# (89)
    DirectX using C# (54)
    Series 1:Terrain (14)
    Opening a window
    Linking to the Device
    Drawing a triangle
    Rotation - Translation
    Terrain creation
    Terrain from file
    Importing bmp files
    Colored vertices
    DirectX Light basics
    Mesh creation
    Mesh lighting
    Series 2: Flightsim (19)
    Series 3: HLSL (19)
    Short Tuts (2)
    DirectX using C++ (15)
    DirectX using VB (2)
    -- Expand all --

    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!