实际上,我试着将SharpDX窗口中的WinForms窗口像下面的视频: http://www.youtube.com/watch?v=g-JupOxwB-kSharpDX:如何在Winforms窗口中放置SharpDX窗口?
在SharpDX这种方法不work.Can谁能告诉我如何轻松地做到这一点?
实际上,我试着将SharpDX窗口中的WinForms窗口像下面的视频: http://www.youtube.com/watch?v=g-JupOxwB-kSharpDX:如何在Winforms窗口中放置SharpDX窗口?
在SharpDX这种方法不work.Can谁能告诉我如何轻松地做到这一点?
不认为它把一个sharpDX窗口放到winform窗口中。
,而不是把它当做如何输出SharpDX到窗口句柄(八九不离十)
的关键是在SharpDX.DXGI.SwapChain。创建这个时候你会需要一个SwapChainDescription
我喜欢像
SwapChainDescription scd = new SwapChainDescription()
{
//set other fields
OutputHandle = yourform.Handle,
//set other fields
};
所以当你调用SwapChain.Present(),它会呈现到窗体创建矿。
这是直SharpDX做到这一点的基本途径,而不是工具包东西
如果你要使用的工具包的GraphicsDevice的,你将不得不设置演示属性。 constructor here。 几乎与您在演示文稿参数中设置窗口句柄的方式几乎相同。 DeviceWindowHandle
也该工具包具有起着很好的与游戏类
EDIT(的DirectX例)
这里是使用直SharpDX(无工具包)为例RenderForm。完整的例子,你应该指的是GitHub的例子HERE
如上所有你需要做的渲染一个WindowsForm窗口说是句柄传递到SwapChain
的Visual Studio 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
}
初始化设备和SwapChain:这对我来说是什么工作我的系统上。如果您遇到问题,则需要研究具体的实施和硬件。DirectX(以及扩展SharpDX)具有可以检测硬件支持的方法。
主要的代码示例:
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,特别是C#在Windows上。你需要更多的东西来获得一些真正的东西。这意味着使用SharpDX在Winforms窗口上呈现的网关。我不解释像顶点/索引缓冲区或渲染纹理/精灵之类的东西。因为这个问题超出了范围。
你能给我一个完整的示例吗?我不明白如何将此添加到winform以及如何通过winform控件 – Jakub 2014-10-27 17:55:59
@Jakub修改此问题确定没有问题。但是这个例子是最低限度的,因为添加任何事物,即使是三角形都不适合你的问题。但如果你能掌握这一点,那么你可以把C++/Directx的例子翻译成c#/ SharpDX实现! – RadioSpace 2014-10-28 00:25:54
如何调整SharpDX窗口的大小?使用示例SharpDX窗口的大小始终与Winform Size – Jakub 2015-07-07 16:34:04
您是使用SharpDX/toolkit还是直接使用SharpDX/DirectX? – RadioSpace 2014-10-28 01:24:18