首页 新闻 会员 周边 捐助

自子窗口中使用Managed DirectX

0
悬赏园豆:20 [待解决问题]

我创建了一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();
        }
    }
}

oilhuang的主页 oilhuang | 初学一级 | 园豆:180
提问于:2011-09-22 15:16
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册