我创建了一MDI 程序,并创建了三个子窗口,其中子窗口使用Managed DirectX进行场景绘制,代码如下(Form 类Draw 是MDI 子窗口:
这段代码在单个的单文档SDI中执行没有问题,但在MDI中,当新建一个Draw子窗口时就会出现错误:
请教大家,如何在子窗口中,或定制控件中使用Managed directX。谢谢!
namespace MDIDemo
{
public partial class Draw : Form
{
private Device device = null;
public Draw()
{
InitializeComponent();
if (InitializeGraphics() == false)
{
Application.Exit();
}
}
protected bool InitializeGraphics()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
return true;
}
catch(DirectXException e)
{
MessageBox.Show(e.ToString());
return false;
}
}
protected override void OnPaint(PaintEventArgs e)
{
DrawScene();
}
protected virtual void DrawScene()
{
if (device == null)
return;
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);
device.BeginScene();
device.EndScene();
device.Present();
}
}
}