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:
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);
}