我实际上是试着将SharpDX Window放在Winforms窗口中,如下面的视频:http://www.youtube.com/watch?v=g-JupOxwB-k
在SharpDX中,这种方法不起作用。任何人都可以告诉我如何轻松地做到这一点?
不要认为它是一个sharpDX窗口进入winforms窗口。
而是将其视为如何将SharpDX输出到Windows句柄(sorta)
关键是SharpDX.DXGI.SwapChain。创建时,您需要一个SwapChainDescription
我喜欢创造我的喜欢
SwapChainDescription scd = new SwapChainDescription()
{
//set other fields
OutputHandle = yourform.Handle,
//set other fields
};
因此,当您调用SwapChain.Present()时,它将呈现给表单。
这是使用直接SharpDX而不是工具包的基本方法
编辑04222019链接不工作
如果要使用工具包的GraphicsDevice,则必须设置Presenter属性。 constructor here。几乎与在演示参数中设置窗口句柄的方式相同。 DeviceWindowHandle
此工具包还有RenderForm,可以与Game类配合使用
04222019
编辑(DirectX示例)
这是一个使用直接SharpDX(No Toolkit)的例子。有关完整示例,请参阅github示例HERE
如上所述,渲染到Windows窗体窗口所需要做的就是将Handle传递给SwapChain
视觉工作室2012
namespace YourNameSpaceHere
{
using Device = SharpDX.Direct3D11.Device;
using Buffer = SharpDX.Direct3D11.Buffer;
...the rest of the application
}
Form
类:这里我们创建设备,交换链,渲染目标,并渲染目标视图我们声明的Form
类的变量
public partial class Form1 : Form //default vs2012 declaration
{
Device d; //Direct311
SwapChain sc; //DXGI
Texture2D target; //Direct3D11
RenderTargetView targetveiw;//DIrect3D11
...the rest of the form
}
主要代码示例:
using System;
using System.ComponentModel;//needed to overide OnClosing
//I removed useless usings
using System.Windows.Forms;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX;
namespace WindowsFormsApplication2
{
using Device = SharpDX.Direct3D11.Device;
using Buffer = SharpDX.Direct3D11.Buffer;
public partial class Form1 : Form
{
Device d;
SwapChain sc;
Texture2D target;
RenderTargetView targetveiw;
public Form1()
{
InitializeComponent();
SwapChainDescription scd = new SwapChainDescription()
{
BufferCount = 1, //how many buffers are used for writing. it's recommended to have at least 2 buffers but this is an example
Flags = SwapChainFlags.None,
IsWindowed = true, //it's windowed
ModeDescription = new ModeDescription(
this.ClientSize.Width, //windows veiwable width
this.ClientSize.Height, //windows veiwable height
new Rational(60,1), //refresh rate
Format.R8G8B8A8_UNorm), //pixel format, you should resreach this for your specific implementation
OutputHandle = this.Handle, //the magic
SampleDescription = new SampleDescription(1, 0), //the first number is how many samples to take, anything above one is multisampling.
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
Device.CreateWithSwapChain(
SharpDX.Direct3D.DriverType.Hardware,//hardware if you have a graphics card otherwise you can use software
DeviceCreationFlags.Debug, //helps debuging don't use this for release verion
scd, //the swapchain description made above
out d, out sc //our directx objects
);
target = Texture2D.FromSwapChain<Texture2D>(sc, 0);
targetveiw = new RenderTargetView(d, target);
d.ImmediateContext.OutputMerger.SetRenderTargets(targetveiw);
}
protected override void OnClosing(CancelEventArgs e)
{
//dipose of all objects
d.Dispose();
sc.Dispose();
target.Dispose();
targetveiw.Dispose();
base.OnClosing(e);
}
protected override void OnPaint(PaintEventArgs e)
{
//I am rendering here for this example
//normally I use a seperate thread to call Draw() and Present() in a loop
d.ImmediateContext.ClearRenderTargetView(targetveiw, Color.CornflowerBlue);//Color to make it look like default XNA project output.
sc.Present(0, PresentFlags.None);
base.OnPaint(e);
}
}
}
这是为了让您开始在托管环境中使用ShaprDX使用DirectX,特别是在Windows上使用C#。你需要更多的东西来获得真实的东西。这意味着使用SharpDX在Winforms窗口上渲染的网关。我不解释像顶点/索引缓冲区或渲染纹理/精灵这样的东西。因为它超出了问题的范围。