游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2263|回复: 1

在子窗口中使用Managed DirectX

[复制链接]

1

主题

1

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2011-9-22 15:30:00 | 显示全部楼层 |阅读模式
我创建了一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();
        }
    }
}

0

主题

2

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2011-9-26 10:18:00 | 显示全部楼层

Re: 在子窗口中使用Managed DirectX

在new Device的参数里把this改成你要的子窗口的引用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-9 10:47

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表