I´m trying to get managed directX to work in a small project.
What I want is to enable windowed mode and draw with DirectDraw.
The following code is resulting in an invisible window (at least for me, using Visual Studio 2010 Ultimate, with DX11 installed and references added by browsing to C:\Windows\assembly\GAC\Microsoft.DirectX*\ and adding the dlls)
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);
public Microsoft.DirectX.DirectDraw.Device dx_device = null;
public Surface dxs_primary;
public Surface dxs_backbuffer;
private Rectangle drawRect;
public Form1()
{
InitializeComponent();
this.Show();
}
public void InitDX()
{
try
{
dx_device = new Microsoft.DirectX.DirectDraw.Device();
dx_device.SetCooperativeLevel(this, Microsoft.DirectX.DirectDraw.CooperativeLevelFlags.Normal);
SurfaceDescription dx_surf_desc = new SurfaceDescription();
dx_surf_desc.SurfaceCaps.PrimarySurface = true;
dxs_primary = new Surface(dx_surf_desc, dx_device);
Clipper dx_clipper = new Clipper(dx_device);
dx_clipper.Window = panel1;
dxs_primary.Clipper = dx_clipper;
dx_surf_desc.Clear();
dx_surf_desc.SurfaceCaps.OffScreenPlain = true;
dx_surf_desc.Width = this.ClientSize.Width;
dx_surf_desc.Height = this.ClientSize.Height;
dxs_backbuffer = new Surface(dx_surf_desc, dx_device);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error initializing Managed DirectX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void Loop()
{
do
{
if (!this.Focused)
{
Application.DoEvents();
}
else
{
dxs_backbuffer.ColorFill(Color.BlueViolet);
dxs_primary.Flip(dxs_backbuffer, FlipFlags.Wait);
Application.DoEvents();
}
} while (this.Created);
dxs_backbuffer.Dispose();
dxs_primary.Dispose();
dx_device.Dispose();
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
InitDX();
Loop();
}
}
The only added control to the prorgam is a panel with default name.
I dont really see a reason for using Direct3D instead of DirectDraw but I do admit that this statement is due to my lack of knowledge in the subject. If D3D is better (for 2D graphics which is my primary target and windowed mode) then I should switch.