Sign in to follow this  

Render DirectX in c# form picturebox?

This topic is 3043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, So this might be an entirely noob question but I am actually just starting out with the whole c# and visual thing as opposed to c++ console stuff. Im trying to make an application tool with a GUI that arranges sprites from sprite sheets, anyway im getting totally off topic as im stuck at the beginning. so I create a new visual c# project and read some stuff about the basics of setting up managed directx device. In my program im going to want the created sprite to animate in a picturebox or something on the form. How?! This is all I have so far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Assignment
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Assignment
{
    public partial class Form1 : Form
    {
        private Device m_device;
        public Form1()
        {
            InitializeComponent();
            InitializeGraphics();
        }

        //initialise DirectX graphics
        public bool InitializeGraphics()
        {
            PresentParameters pp = new PresentParameters();
            pp.SwapEffect = SwapEffect.Discard;
            pp.Windowed = true;
            pp.DeviceWindowHandle = pictureBox1.Handle;
            try
            {
                m_device = new Device(0, DeviceType.Hardware, pictureBox1.Handle, CreateFlags.SoftwareVertexProcessing, pp);
                return true;
            }
            catch (DirectXException)
            {
                return false;
            }
        }

        //
        private void Render()
        {
            m_device.Clear(ClearFlags.Target, Color.Navy, 1.0f, 0);
            m_device.BeginScene();

            // Render frame here

            m_device.EndScene();
            m_device.Present();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void pictureBox1_paint(object sender, EventArgs e)
        {
            Render();
        }

    }
}

I know the Render function doesn't actually do anything right now but I figure if it clears the picture box to blue its working right? Any and all very detailed help as im new to this would be much appreciated

Share this post


Link to post
Share on other sites

This topic is 3043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this