Okay although I make no claim to tidy or well ordered code here you go...
First step is of course correctly setting up a D3D device, which I assume you've done.
Next you have to set up your swap chain. In the code below ViewPanel is inherited from Panel and has a swap chain associated with it. pSwapChain is a private member of ViewPanel.
Void ViewPanel::InitializeSwapChain(DirectX::Direct3D::Device * pDev)
{
// Create a new set of presentation parameters based on our main Direct3D device.
// Point the new presentation parameters at the panel, and then create a new swap chain.
PresentParameters * pp = new PresentParameters(pDev->PresentationParameters);
pp->DeviceWindow = this;
pp->DeviceWindowHandle = this->Handle;
try
{
pSwapChain = new SwapChain(pDev, pp);
}
catch(InvalidCallException * dxCatch)
{
Console::WriteLine(dxCatch->ToString());
}
} // End METHOD InitializeSwapChain
Now to test this I used the Paint event of the panel...
I had to put a test in to make sure we didn't try to use the swap chain if it was marked for garbage collection because it was fairly bad karma if we did (see original post).
Void ViewPanel::OnPaintBackground(Object * pObject, PaintEventArgs * pea)
{
DirectX::Direct3D::Surface * pSurf;
// If the swap chain is marked for disposal, due to a device reset or something, then don't
// do anything.
if(pSwapChain->Disposed)
{
return;
}
pSurf = pSwapChain->GetBackBuffer(0, BackBufferType::Mono);
pSurf->Device->SetRenderTarget(0, pSurf);
pSurf->Device->Clear(ClearFlags::Target, Drawing::Color::Black, 0.0f, 0);
pSurf->Device->EndScene();
pSwapChain->Present();
} // End EVENT OnPaintBackground
Now the only other thing is that you have to remember to handle the Reset event for your main D3D device.
When a device is reset you must re-initialise all of your swap chains again using the first method I posted. In this code I keep track of my swap chains by keeping an ArrayList of pointers to the active ViewPanels... although I'm sure there are better ways to do this.
Hope this helps...
JP