【发布时间】:2021-10-29 17:33:30
【问题描述】:
我对 DirectX 很陌生,我一直在尝试学习本教程,但它并没有真正解释这些对象是什么,我希望将来能够将它与 WinForms 或 WPF 一起使用,但我不知道如何设置用于 2d 绘图的设备。有人介意告诉我你是这样做的吗?我尝试了这个,但是当设置变量 d2dTarget 时,我得到了一个 sharpdx 异常。
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ComponentModel;
using SharpDX;
using SharpDX.DXGI;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.Direct2D1;
namespace SharpDX_Tester
{
public partial class Form1 : Form
{
SharpDX.Direct3D11.Device device;
SharpDX.Direct3D11.DeviceContext1 d3dContext;
SharpDX.Direct2D1.DeviceContext d2dContext;
RenderTargetView targetView;
SwapChain swapChain;
Texture2D target;
Bitmap1 d2dTarget;
public Form1()
{
InitializeComponent();
SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport);
device = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();
d3dContext = device.ImmediateContext.QueryInterface<SharpDX.Direct3D11.DeviceContext1>();
SharpDX.DXGI.Device2 dxgiDevice2 = device.QueryInterface<SharpDX.DXGI.Device2>();
SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice2.Adapter;
SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>();
SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2);
d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);
var scd = new SwapChainDescription()
{
BufferCount = 1,
Flags = SwapChainFlags.None,
IsWindowed = true, //WINDOWS
ModeDescription = new ModeDescription(panel1.ClientSize.Width, panel1.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
OutputHandle = panel1.Handle/*<-Impotant*/,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
SharpDX.Direct3D11.Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.Debug, scd, out device, out swapChain);
BitmapProperties1 properties = new BitmapProperties1(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied));
Surface backBuffer = swapChain.GetBackBuffer<Surface>(0);
d2dTarget = new Bitmap1(d2dContext, backBuffer, properties);
/* IGNORE THIS
target = Texture2D.FromSwapChain<Texture2D>(sc, 0);
targetView = new RenderTargetView(device, target);
device.ImmediateContext.OutputMerger.SetRenderTargets(targetView);
*/
}
}
```
【问题讨论】:
-
对不起,我忘了发布这个我试图遵循的教程在这里english.r2d2rigo.es/2012/07/04/…
标签: c# winforms directx sharpdx