Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Using directX in a control

Using directX in a control

Using directX in a control


I'm new to directX and want to prgram directX in managed code.
I want to show a 3D-Scene in a control, e.g. a piturebox.
I tried an example code which was running in a window. I changed the devicetarget to the control.
Now is the problem the following:
If I use the method picturebox1.Invalidate() the graphic is flickering.
If I don't use the method picturebox1.Invalidate() the device is created and the scene is drawn, but immediately cleared. I think that the device is deleted because the picturebox is drawn as a standard control miliseconds after starting the application.

Can anyone help me?
cu Kostarsus

Here is the code I used:


public Form1(bool isDebug, bool isFast)

    this.pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint);
    this.SetStyle(ControlStyles.ContainerControl |

protected override void OnPaint(System.Windows.Forms.PaintEventArgs a)


private void pictureBox1_Paint(object sender,
        System.Windows.Forms.PaintEventArgs a)
    CustomVertex.TransformedColored[] verts;
    m_device.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0);
    m_device.VertexFormat= CustomVertex.TransformedColored.Format;

private CustomVertex.TransformedColored[] DefineTriangle()

    CustomVertex.TransformedColored[] verts = new
    verts[0].SetPosition(new Vector4(this.pictureBox1.Width/2.0f, 50.0f,
    verts[1].SetPosition(new Vector4(this.pictureBox1.Width -
        (this.pictureBox1.Width / 5.0f), this.pictureBox1.Height -
        (this.pictureBox1.Height / 5.0f), 0.5f, 1.0f));
    verts[2].SetPosition(new Vector4(this.pictureBox1.Width / 5.0f,
        this.pictureBox1.Height -(this.pictureBox1.Height / 5.0f), 0.5f,
    return verts;

private void InitializeGraphics()

    PresentParameters myPresentParameters = new PresentParameters();
    m_device=new Device(0,DeviceType.Hardware,this.pictureBox1,CreateFlags.SoftwareVertexProc

RE: Using directX in a control

I just got C# so I don't know it very well, and have only used D3D with C++, but I'll try to help.  A similar problem occurs when using D3D with MFC.  There is an OnEraseBkgnd message handler that you have to override and prevent from calling the default OnEraseBkgnd.  This way Windows won't use Win32 GDI to draw over your D3D stuff.  Try it and let me know how it works out.

RE: Using directX in a control

Thank you for your respond.
I'd found a solution by my own, yesterday night
Here is the solution:

1. I found in the internet a reference,that the picturebox is buggy for this. The author of the article used a panel than. So I used a panel, too. This was the first step of solution.
2. You don't need to override the paintevent of the control, because the paintevent of the control is only entered if the control is hidden by an other window. Better you use a normal method and call this method from the PaintEvent of the form. You must invalidate the form than.
3. If you have more than one control in the form you should use the method     Application.DoEvents() in the PaintEvent of the form, otherwise the other controls e.g. buttons are only rendered if you click the buttons once.
4. Because the device is running in a control, you don't need the SetStyle-Method.

In this way you can use PositionColored vertexes and use transformations to animate the szene in a panel.

cu Kostarsus

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close