Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using directX in a control

Status
Not open for further replies.

Kostarsus

Programmer
Nov 30, 2000
86
DE
Hello,

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:
Code:
public Form1(bool isDebug, bool isFast)

{
    m_bIsDebug=isDebug;
    m_bIsFast=isFast;
    InitializeComponent();
    this.pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint);
    this.SetStyle(ControlStyles.ContainerControl |
        ControlStyles.Opaque,true);
}

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

{
    pictureBox1.Invalidate();
}

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);
    verts=DefineTriangle();
    m_device.BeginScene();
    m_device.VertexFormat= CustomVertex.TransformedColored.Format;
    m_device.DrawUserPrimitives(PrimitiveType.TriangleList,1,verts);
    m_device.EndScene();
    m_device.Present();
    this.pictureBox1.Invalidate();
}

private CustomVertex.TransformedColored[] DefineTriangle()

{
    CustomVertex.TransformedColored[] verts = new
        CustomVertex.TransformedColored[3];
    verts[0].SetPosition(new Vector4(this.pictureBox1.Width/2.0f, 50.0f,
        0.5f,1.0f));
    verts[0].Color=System.Drawing.Color.Aquamarine.ToArgb();
    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[1].Color=System.Drawing.Color.White.ToArgb();
    verts[2].SetPosition(new Vector4(this.pictureBox1.Width / 5.0f,
        this.pictureBox1.Height -(this.pictureBox1.Height / 5.0f), 0.5f,
1.0f));
    verts[2].Color=System.Drawing.Color.Green.ToArgb();
    return verts;
}

private void InitializeGraphics()

{
    PresentParameters myPresentParameters = new PresentParameters();
    myPresentParameters.Windowed=true;
    myPresentParameters.SwapEffect=SwapEffect.Discard;
    m_device=new Device(0,DeviceType.Hardware,this.pictureBox1,CreateFlags.SoftwareVertexProc
        essing,myPresentParameters);
}

 
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top