Sign in to follow this  
m3f_neadekwat

[SlimDx] WPF + Direct2D

Recommended Posts

I am trying to connect WPF and Direct2D using Direct3D10 through DXGI. I am not very good at directX and WPF, so I have a problem connecting them without using any c++ code.
Trying to connect using next trivial code:
[code]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Forms.Integration;


using SlimDX;

using D2D = SlimDX.Direct2D;
using D3D = SlimDX.Direct3D10;
//using D3D1 = SlimDX.Direct3D10_1;
using DXGI = SlimDX.DXGI;

namespace Test
{
class SlimDxControl : ContentControl
{
static D2D.Factory g_D2DFactory;
static DXGI.Factory g_DXGIFactory;

static SlimDxControl()
{
g_D2DFactory = new D2D.Factory(D2D.FactoryType.SingleThreaded);
g_DXGIFactory = new DXGI.Factory();
}


D3D.Device m_D3DDevice;
D2D.RenderTarget m_D2DRenderTarget;

public SlimDxControl()
{
Initialize();
}

void Initialize()
{
//Create swap chain desriptions
DXGI.SwapChainDescription schdesc = new DXGI.SwapChainDescription();

schdesc.IsWindowed = true;
schdesc.BufferCount = 2;
schdesc.Usage = DXGI.Usage.BackBuffer;

DXGI.ModeDescription mdesc = new DXGI.ModeDescription(200, 200, new Rational(20, 0), DXGI.Format.R8G8B8A8_UNorm);

//mdesc.Scaling = DXGI.DisplayModeScaling.Centered;
//mdesc.ScanlineOrdering = DXGI.DisplayModeScanlineOrdering.Progressive;

schdesc.ModeDescription = mdesc;
schdesc.SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0);

//Create windows forms host
WindowsFormsHost wfhost = new WindowsFormsHost();

this.Content = wfhost;

wfhost.Child = new System.Windows.Forms.Panel();
wfhost.Child.Height = 200;
wfhost.Child.Width = 200;
wfhost.Child.Dock = System.Windows.Forms.DockStyle.Fill;

//Connect to forms host
//g_DXGIFactory.SetWindowAssociation(wfhost.Child.Handle, DXGI.WindowAssociationFlags.None);

schdesc.OutputHandle = wfhost.Child.Handle;

DXGI.SwapChain swapChain;

D3D.Device.CreateWithSwapChain(g_DXGIFactory.GetAdapter(0), D3D.DriverType.Hardware, D3D.DeviceCreationFlags.SingleThreaded, schdesc, out m_D3DDevice, out swapChain);

DXGI.Surface surf = DXGI.Surface.FromSwapChain(swapChain, 0);

m_D2DRenderTarget = D2D.RenderTarget.FromDXGI(g_D2DFactory, surf, new D2D.RenderTargetProperties() //Here exception
{
Type = D2D.RenderTargetType.Hardware,
HorizontalDpi = g_D2DFactory.DesktopDpi.Width,
VerticalDpi = g_D2DFactory.DesktopDpi.Height,
});
}
}
}
[/code]


How to avoid an exception that occurs whlie creating render target?
What is wrong?
Or are there other ways to connect WPF and Direct2D wihout any writing in c++?

Thanks in advance.

Share this post


Link to post
Share on other sites

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