Forum
Contact





DirectX using C#
DirectX using C++
DirectX using Visual Basic



Latest Forum posts

 Texture on a 3d Object
  Posted by: ToastbrotX
  When: 10/09/2010 at 10:23:08

 Unable to Build
  Posted by: Rich_Zap
  When: 09/09/2010 at 15:39:33

 HLSL calculating normals
  Posted by: Rich_Zap
  When: 09/09/2010 at 15:34:56

 Texture on a 3d Object
  Posted by: ToastbrotX
  When: 09/09/2010 at 11:14:19

 DRIVER ERROR XBOX 360
  Posted by: Anonymous
  When: 09/09/2010 at 01:12:47

 Reflection problem in corners ...
  Posted by: Anonymous
  When: 08/09/2010 at 21:03:35

 Texture on a 3d Object
  Posted by: radulph
  When: 08/09/2010 at 17:34:01

 InvalidDataException???
  Posted by: Anonymous
  When: 08/09/2010 at 09:10:36

 Unable to Build
  Posted by: Anonymous
  When: 08/09/2010 at 07:49:36

 Suggestion to change a few lines
  Posted by: Insomnica
  When: 06/09/2010 at 14:37:05

 Collision with series 1
  Posted by: radulph
  When: 05/09/2010 at 13:33:14

 HLSL calculating normals
  Posted by: miroslavign
  When: 04/09/2010 at 17:26:26

 Collision with series 1
  Posted by: ToastbrotX
  When: 04/09/2010 at 16:52:02

 HLSL calculating normals
  Posted by: Rich_Zap
  When: 04/09/2010 at 15:00:20

 Collision with series 1
  Posted by: ToastbrotX
  When: 04/09/2010 at 12:28:41

 HLSL calculating normals
  Posted by: miroslavign
  When: 04/09/2010 at 08:46:31

 Walk along a wall
  Posted by: Anonymous
  When: 03/09/2010 at 10:28:02

 model problems
  Posted by: muffinman
  When: 03/09/2010 at 06:47:32

 Vertices problem
  Posted by: Anonymous
  When: 03/09/2010 at 05:48:35

 OcTree Question
  Posted by: Zorzomezz
  When: 03/09/2010 at 04:07:03




Topic: with PictureBox



  
Goto parent category
  
Create a new user account


   with PictureBox
 Poster : skyline
 Posts: 6
 Country : Turkey
 City: izmir

  
Posted by skyline on 06/09/2006 at 03:12:41
hi,

i've finished all the tutorial and now i need to use DirectX with my picture box, and i couldn't draw the triangle to the pic box. i used this.picbox instead of this in some places to make the pic box relational to directX but nothing happened..

what should i do?
thanks..
 Poster : skyline
 Posts: 6
 Country : Turkey
 City: izmir

  
Posted by skyline on 06/09/2006 at 03:16:10
now i noticed that the triangle can be seen for a very short time and then the pic box becomes empty again!!
 Poster : skyline
 Posts: 6
 Country : Turkey
 City: izmir

  
Posted by skyline on 07/09/2006 at 06:37:14
does anyone know how to fix it ? pls help..
 Poster : riemer
 Posts: 1388
 Country : Belgium
 City: Antwerp

  
Posted by riemer on 07/09/2006 at 08:04:53
Haven't used the picture box with directx yet..
 Poster : BlueG
 Posts: 21
 Country : United States
 City:

  
Posted by BlueG on 03/12/2006 at 15:10:10
I was able to do this with code like the following in the Paint event handler:


if( m_Device == null ) InitializeDevice();
m_Device.Clear( ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0 );
m_Device.Present();

if( m_DoInvalidate )
{
    Invalidate( false );
    Update();
    m_DoInvalidate = false;
}


You'll notice from this that I was handling device creation somewhat differently (were I serious about this example, I'd also check to see if the device had been lost).

The key to using a PictureBox, however, is the m_DoInvalidate flag. That's just a boolean class member that I added to my form class. Calling Invalidate and Update forces the form to redraw itself a second time. The reason the PictureBox keeps being redrawn according to its background color is because its being invalidated and then redrawn unexpectedly. Probably at some point after the form has already been drawn.

I used to have a more elegant solution to this problem (one that didn't require redrawing the form twice), but I lost it during my great computer crash of 2006 (I'm still recovering). If I remember it, I'll try to also remember to post back here again. It was similar in concept, but I think I had to tinker with the way these events are processed.
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 03/12/2006 at 15:37:50
Or, on second thought, I won't be using picture boxes anymore... I just found a much better way for my purposes. Might work for skyline too.

Instead of using a PictureBox as the target for my rendering, I created my own control derived from UserControl. To do this, simply go to project, then add new item, and select User Control. The following is the code from my user control:


public partial class GfxBox : UserControl
{
    public GfxBox()
    {
        InitializeComponent();
        SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque | ControlStyles.UserPaint, true );
        UpdateStyles();
    }
}


I'm then able to freely use it as the render target, without all the hoopla. The OnPaint method of my form now looks like this:


if( m_Device == null ) InitializeDevice();
m_Device.Clear( ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0 );
m_Device.Present();


Much nicer. ;)

Note that before my control showed up in the toolbox so that I could add this control to my form using the designer, I had to compile the project. I thought there was a way to add it directly from Solution Explorer instead of the toolbox, but I seem to have forgotten it >.<
 Poster : BlueG
 Posts: 21
 Country : United States
 City:

  
Posted by BlueG on 03/12/2006 at 15:38:59
Whoops... forgot my name on that one =p
 Poster : BlueG
 Posts: 21
 Country : United States
 City:

  
Posted by BlueG on 03/12/2006 at 19:55:48
In the spirit of these tutorials, here is code using a UserControl instead of a PictureBox all in one file:


using System;
using System.Drawing;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DXTriangle
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault( false );
            Application.Run( new MainForm() );
        }
    }

    public sealed class MainForm : Form
    {
        private GfxTarget _gfx;
        private ScrollBar _scrollbar;

        private Device m_Device;
        private CustomVertex.PositionColored[] vertices;

        public MainForm()
        {
            InitializeControls();
            InitializeModel();
        }

        private void InitializeControls()
        {
            this.Text = "DirectX Triangle App";
            this.Width = 668;
            this.Height = 572;

            _gfx = new GfxTarget();
            _gfx.BorderStyle = BorderStyle.Fixed3D;
            _gfx.Location = new Point( 8, 8 );
            _gfx.Size = new Size( 644, 484 );

            _scrollbar = new HScrollBar();
            _scrollbar.Location = new Point( 8, 500 );
            _scrollbar.Width = 644;
            _scrollbar.LargeChange = 30;
            _scrollbar.Minimum = 0;
            _scrollbar.Maximum = 389; // max value returned will be 360
            _scrollbar.Value = 180;
            _scrollbar.ValueChanged += new EventHandler( OnScroll );

            this.Controls.Add( _gfx );
            this.Controls.Add( _scrollbar );
        }

        private void InitializeModel()
        {
            vertices = new CustomVertex.PositionColored[3];

            vertices[0].Position = new Vector3( -10.0f, -2.0f, 0.0f );
            vertices[1].Position = new Vector3( 0.0f, 8.0f, 0.0f );
            vertices[2].Position = new Vector3( 10.0f, -2.0f, 0.0f );

            vertices[0].Color = Color.Red.ToArgb();
            vertices[1].Color = Color.Blue.ToArgb();
            vertices[2].Color = Color.Green.ToArgb();
        }

        private void InitializeDevice()
        {
            PresentParameters parameters = new PresentParameters();
            parameters.Windowed = true;
            parameters.SwapEffect = SwapEffect.Discard;

            m_Device = new Device( 0, DeviceType.Hardware, _gfx, CreateFlags.SoftwareVertexProcessing, parameters );

            m_Device.RenderState.CullMode = Cull.None;
            m_Device.RenderState.Lighting = false;
            m_Device.Transform.Projection = Matrix.PerspectiveFovLH( (float)( Math.PI / 4.0 ), (float)( 4.0 / 3.0 ), 1.0f, 1000.0f );
            m_Device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f, 10.0f, -20.0f ), new Vector3( 0.0f, 0.0f, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) );
            m_Device.VertexFormat = CustomVertex.PositionColored.Format;
        }

        private void OnScroll( object sender, EventArgs e )
        {
            Invalidate();
        }

        protected override void OnPaint( PaintEventArgs e )
        {
            base.OnPaint( e );

            double angle = _scrollbar.Value - 180;

            if( m_Device == null ) InitializeDevice();
            m_Device.Clear( ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0 );
            m_Device.BeginScene();
            m_Device.Transform.World = Matrix.RotationY( (float)( Math.PI * angle / 180.0 ) );
            m_Device.DrawUserPrimitives( PrimitiveType.TriangleList, 1, vertices );
            m_Device.EndScene();
            m_Device.Present();
        }
    }

    public sealed class GfxTarget : UserControl
    {
        public GfxTarget()
        {
            SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque | ControlStyles.UserPaint, true );
            UpdateStyles();
        }
    }
}


I also loosely based the triangle rendering code off of these tutorials. Feels good to be programming again (see "the Great Computer Crash" =p). Hope this will help someone.
 Poster : riemer
 Posts: 1388
 Country : Belgium
 City: Antwerp

  
Posted by riemer on 04/12/2006 at 02:54:18
This is really excellent! Thanks for the addition, really useful information here.

I've only been using DX for full-window apps, but this must enable using DX for other apps as well. I'll give it a try this evening.
 Poster : BlueG
 Posts: 21
 Country : United States
 City:

  
Posted by BlueG on 04/12/2006 at 21:05:07
Being able to render to a control window is incredibly useful, especially for making tools, which is how I've used it before.
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 05/12/2006 at 02:46:28
Yes it is! This has already been asked for quite a couple of times in this forum. So thanks again for posting this solution.
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 12/12/2006 at 01:58:55
Does it work also if the verteces are not in ...Colored format?
In my case PositionTransformedColored vertices were drawn in Panel with no problem (assuming all control Styles are set as indicated), however trying to apply materials and lights caused a problem.
Using UserControl instead of Panel did not change anything (I can't define border style however :)
 Poster : riemer
 Posts: 1388
 Country : Belgium
 City: Antwerp

  
Posted by riemer on 12/12/2006 at 04:34:53
I haven't tried yet what you're trying, but are you sure you program works in a simple DirectX window? Because a LOT can go wrong when switching from Colored vertices to something with normals...
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 12/12/2006 at 15:46:47
Yes, at first it works, if transferred one to one into main Form. Actually it's vise versa - I just took tutorial6 from DX SDK and tryed to put into the panel.
At second I'm geting the image, but it's soooo strange.
I see separate fragments of a mesh, they are correctly colored and correcly positioned. But they are separate and they change with time, if I rotate the mesh.
Can it be that z-buffer is different for the Frame and other controls?
 Poster : BlueG
 Posts: 21
 Country : United States
 City:

  
Posted by BlueG on 12/12/2006 at 19:31:02
The z-buffer is a property of the graphics device and its back buffers. It knows nothing of the z order of form elements.

I guess I'll have to try the tutorial putting it in a picture box or user control.
 Poster : BlueG
 Posts: 21
 Country : United States
 City:

  
Posted by BlueG on 13/12/2006 at 01:38:58
I was able to run Tutorial 6 from the DirectX SDK in a panel created from the GfxTarget class without a hitch. I haven't tried a Panel or PictureBox from System.Windows.Forms and probably won't.

After creating a new project from Tutorial 6 and getting the application icon to load so as to be able to run it in the full window successfully, I made the following changes:

1) I pasted the GfxClass from my previous post here into the source code (I actually copied it from the post here to make sure it was the same).

2) I added the following member to the Meshes class:

GfxTarget gfxtarget = null;


3) I changed the following line in the Meshes class constructor:

this.ClientSize = new System.Drawing.Size(416,316);


4) I added the following code to the Meshes class constructor:

gfxtarget = new GfxTarget();
gfxtarget.Left = 8;
gfxtarget.Top = 8;
gfxtarget.Width = 400;
gfxtarget.Height = 300;
this.Controls.Add( gfxtarget );


5) I changed the following line in the InitializeGraphics method:

device = new Device(0, DeviceType.Hardware, gfxtarget, CreateFlags.SoftwareVertexProcessing, presentParams);


And that's it. The change I had to make to get the icon to work was as follows (in the Meshes class constructor):

this.Icon = new Icon("..\\..\\directx.ico");


So, as you can see, that had nothing to do with it.

P.S. I'll hang on to the complete source for a few days in case its needed, but I didn't want to post it here since I was unsure if that was both acceptable and legal.
 Poster : stebo0728
 Posts: 1
 Country : USA
 City: Covington

  
Posted by stebo0728 on 24/08/2007 at 12:23:47
I am designing a sound recording program, and needed to have a way to represent the wave as it is being recorded. I saw some tutorials showing how to display it as a constantly updated image, but that seemed a little under par for me, so i had the idea of using DirectX, and I tried this same idea, making a UserControl. It worked great except I had to figure a few things out, I had to define the AutoDepthStencilFormat, which I set to D16, and I had to set EnableAutoDepthStencil to true, both of these properties being part of the PresentParameters. Also I had to define the graphics device with the UserControl's handle rather than the UserControl itself. Now I have the control working. I just started so I am only to the device.Clear(), device.Present() stage, but here is my usercontrol code so far


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

namespace DirectXTest
{
    public partial class WaveFormDisplay : UserControl
    {
        public Microsoft.DirectX.Direct3D.Device GraphicsDevice;

        public WaveFormDisplay()
        {
            InitializeComponent();
            SetupSoundCapture();
            SetupGraphicsDevice();
            SetupCamera();
            SetupInput();
        }

        private void SetupSoundCapture()
        {

        }

        private void SetupGraphicsDevice()
        {
            PresentParameters presentParameters = new PresentParameters();
            presentParameters.Windowed = true;
            presentParameters.SwapEffect = SwapEffect.Discard;
            presentParameters.AutoDepthStencilFormat = DepthFormat.D16;
            presentParameters.EnableAutoDepthStencil = true;
            GraphicsDevice = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, presentParameters);
        }

        private void SetupInput()
        {
            
        }

        private void SetupCamera()
        {

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            GraphicsDevice.Clear(ClearFlags.Target, Color.Navy, 1.0f, 0);
            GraphicsDevice.BeginScene();

            GraphicsDevice.EndScene();
            GraphicsDevice.Present();
        }
    }
}


My question is, I see you have done all your directX code in your main form and defined to usercontrol as the target, wheras i have done all my directx code in the control itself, what is the difference, and is one way better than the other?

Thanks
 Poster : serendipity
 Posts: 27
 Country : Pakistan
 City: Rawalpindi

  
Posted by serendipity on 02/05/2008 at 02:41:24
This is really neat!
Thanks a lot
 Poster : riemer
 Posts: 1388
 Country : Belgium
 City: Antwerp

  
Posted by riemer on 02/05/2008 at 14:48:44
Thanks for posting this!

Some other code that will probably do just the same can be found here:

http://www.riemers.net/Forum/index.php?var=1075&var2=0
 Poster : serendipity
 Posts: 27
 Country : Pakistan
 City: Rawalpindi

  
Posted by serendipity on 12/05/2008 at 03:03:48
Hi, Riemers, BlueG, Help required

I'm working on an application in which i needed to create a directx Device someplace other than the main form. At first it was being done in a child form to the mainform. But then, following BlueG's approach I converted that child form to a UserControl and its working great!!

The problem is in using DirectInput. Inside the UserControl class, the statements which were working previously aren't working:



keyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);          
          
keyboard.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
            
keyboard.Acquire();    



The error i'm getting is:

Microsoft.DirectX.DirectInput.AcquiredException was unhandled
  Message="Error in the application."
  Source="Microsoft.DirectX.DirectInput"
  ErrorCode=-2147024726
  ErrorString="DIERR_ACQUIRED"

I think this error is being caused by the first argument of keyboard.SetCooperativeLevel, which is defined as:

'System.Window.Form.Control' and "this" indicates the UserControl class that i created, which is a custom control. But the directinput device is not accepting it.

What am i doing wrong here?

I'v googled this problem a lot but have found no mention of it so far.

Thanks ....
 Poster : Serendipity
 Posts: 27
 Country : Pakistan
 City: Rawalpindi

  
Posted by Serendipity on 12/05/2008 at 03:24:26
Update: I tried 'this.Handle' instead of 'this' but that din't work as well.

Also I would like to repeat stebo0728's question: what's the difference between the two approaches, if any, coz I'v also done everything inside the used control class instead of doing it in the main form and then passing the device and other variables to the UserControl. .... ?
 Poster : riemer
 Posts: 1388
 Country : Belgium
 City: Antwerp

  
Posted by riemer on 12/05/2008 at 05:41:23
This is how I acquire my keyboard and mouse in a non-dx-excluse winapp:

keyb = new Microsoft.DirectX.DirectInput.Device(DInput.SystemGuid.Keyboard);
            keyb.SetCooperativeLevel(this, DInput.CooperativeLevelFlags.Background | DInput.CooperativeLevelFlags.NonExclusive);
            keyb.Acquire();

            mouse =new DInput.Device(DInput.SystemGuid.Mouse);
            mouse.SetCooperativeLevel(this, DInput.CooperativeLevelFlags.Background | DInput.CooperativeLevelFlags.NonExclusive);
            mouse.Acquire();

Important: this code is called from within the Form1 class. 'this' must refer to an object of the Form class. So if you have a separete DX class, the code should look like this:

keyb.SetCooperativeLevel(mainForm, DInput.CooperativeLevelFlags.Background | DInput.CooperativeLevelFlags.NonExclusive);

  
Post a new reply
 





Google
 
Web www.riemers.net
If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !
- Website design & DirectX code : Riemer Grootjans -
©2006 Riemer Grootjans


News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 3.0 using C# (89)
DirectX using C# (54)
Series 1:Terrain (14)
Opening a window
Linking to the Device
Drawing a triangle
Camera
Rotation - Translation
Indices
Terrain creation
Terrain from file
DirectInput
Importing bmp files
Colored vertices
DirectX Light basics
Mesh creation
Mesh lighting
Series 2: Flightsim (19)
Series 3: HLSL (19)
Short Tuts (2)
Resizing problem
Checking Device caps
DirectX using C++ (15)
DirectX using VB (2)