我实际上是试着将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
};
Run Code Online (Sandbox Code Playgroud)
因此,当您调用SwapChain.Present()时,它将呈现给表单.
这是使用直接SharpDX而不是工具包的基本方法
如果要使用工具包的GraphicsDevice,则必须设置Presenter属性.这里的构造函数.几乎与在演示参数中设置窗口句柄的方式相同.DeviceWindowHandle
此工具包还有RenderForm,可以与Game类配合使用
编辑(DirectX示例)
这是一个使用直接SharpDX(No Toolkit)的例子.有关完整示例,请参阅此处的github示例
如上所述,渲染到WindowsForm窗口所需要做的就是将Handle传递给SwapChain
视觉工作室2012

一些使用语句使事情变得更容易:
namespace YourNameSpaceHere
{
using Device = SharpDX.Direct3D11.Device;
using Buffer = SharpDX.Direct3D11.Buffer;
...the rest of the application
}
Run Code Online (Sandbox Code Playgroud)在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
}
Run Code Online (Sandbox Code Playgroud)初始化Device和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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是为了让您开始在托管环境中使用ShaprDX使用DirectX,特别是在Windows上使用C#.你需要更多的东西来获得真实的东西.这意味着使用SharpDX在Winforms窗口上渲染的网关.我不解释像顶点/索引缓冲区或渲染纹理/精灵这样的东西.因为它超出了问题的范围.
| 归档时间: |
|
| 查看次数: |
8292 次 |
| 最近记录: |