SharpDX 在不使用 RenderLoop 无限实现的情况下绘制表单
SharpDX drawing a form without using RenderLoop to actualize it infinitely
您好,正如标题所说,我正在尝试为一个项目制作图像查看器(有点像 Windows 图像查看器)。我看到很多代码将他们的 RenderForm 显示到 RenderLoop 中,但我不喜欢这个解决方案,因为我不想在 RenderLoop 中无限刷新图像。我只想在需要重绘时调用 Draw 方法(例如缩放。)问题是,现在我尝试使用 renderLoop.Show() 但它不会停留在屏幕上并向右关闭执行完所有代码后...
using SharpDX;
using SharpDX.Windows;
using SharpDX.D3DCompiler;
using SharpDX.Direct2D1;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Mathematics;
using System.Drawing;
using System.Windows.Forms;
using Device = SharpDX.Direct3D11.Device;
using Color = SharpDX.Color;
namespace SharpDXWic
{
public class SharpDXDisplay : IDisposable
{
private const int WIDTH = 1500;
private const int HEIGHT = 800;
private Device device;
private SwapChain swapChain;
private RenderForm renderForm;
private RenderTargetView targetView;
public SharpDXDisplay(string display_title)
{
renderForm = new RenderForm(display_title);
renderForm.Width = WIDTH;
renderForm.Height = HEIGHT;
Texture2D target;
SwapChainDescription scd = new SwapChainDescription()
{
BufferCount = 1,
Flags = SwapChainFlags.None,
IsWindowed = true,
ModeDescription = new ModeDescription(WIDTH,HEIGHT, new Rational(60, 1),Format.R8G8B8A8_UNorm),
OutputHandle = renderForm.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
Device.CreateWithSwapChain( SharpDX.Direct3D.DriverType.Hardware,DeviceCreationFlags.Debug, scd, out device, out swapChain);
target = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
targetView = new RenderTargetView(device, target);
device.ImmediateContext.OutputMerger.SetRenderTargets(targetView);
renderForm.Show();
device.ImmediateContext.ClearRenderTargetView(targetView, Color.CornflowerBlue);
swapChain.Present(0, PresentFlags.None);
}
private void OnClosing()
{
Dispose();
}
public void Dispose()
{
device.Dispose();
swapChain.Dispose();
renderForm.Dispose();
targetView.Dispose();
}
}
}
我的目标是在不进入 RenderLoop 的情况下 Draw() 一个表单。我只想刷新图片 on-demand 而不是不断刷新。
实际上可以在不使用无限循环的情况下将图像显示为 Windows 形式。向表单添加缩放或调整大小等操作并执行操作(例如重绘图像)效果很好。
当然,如果您正在制作视频游戏,那不是最佳解决方案,因为您需要不断渲染图形。然而,对于图像查看器来说,将所有内容绘制到 windows 表单一次并在 ActionEvents 上更新它是完全没问题的。
您好,正如标题所说,我正在尝试为一个项目制作图像查看器(有点像 Windows 图像查看器)。我看到很多代码将他们的 RenderForm 显示到 RenderLoop 中,但我不喜欢这个解决方案,因为我不想在 RenderLoop 中无限刷新图像。我只想在需要重绘时调用 Draw 方法(例如缩放。)问题是,现在我尝试使用 renderLoop.Show() 但它不会停留在屏幕上并向右关闭执行完所有代码后...
using SharpDX;
using SharpDX.Windows;
using SharpDX.D3DCompiler;
using SharpDX.Direct2D1;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Mathematics;
using System.Drawing;
using System.Windows.Forms;
using Device = SharpDX.Direct3D11.Device;
using Color = SharpDX.Color;
namespace SharpDXWic
{
public class SharpDXDisplay : IDisposable
{
private const int WIDTH = 1500;
private const int HEIGHT = 800;
private Device device;
private SwapChain swapChain;
private RenderForm renderForm;
private RenderTargetView targetView;
public SharpDXDisplay(string display_title)
{
renderForm = new RenderForm(display_title);
renderForm.Width = WIDTH;
renderForm.Height = HEIGHT;
Texture2D target;
SwapChainDescription scd = new SwapChainDescription()
{
BufferCount = 1,
Flags = SwapChainFlags.None,
IsWindowed = true,
ModeDescription = new ModeDescription(WIDTH,HEIGHT, new Rational(60, 1),Format.R8G8B8A8_UNorm),
OutputHandle = renderForm.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
Device.CreateWithSwapChain( SharpDX.Direct3D.DriverType.Hardware,DeviceCreationFlags.Debug, scd, out device, out swapChain);
target = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
targetView = new RenderTargetView(device, target);
device.ImmediateContext.OutputMerger.SetRenderTargets(targetView);
renderForm.Show();
device.ImmediateContext.ClearRenderTargetView(targetView, Color.CornflowerBlue);
swapChain.Present(0, PresentFlags.None);
}
private void OnClosing()
{
Dispose();
}
public void Dispose()
{
device.Dispose();
swapChain.Dispose();
renderForm.Dispose();
targetView.Dispose();
}
}
}
我的目标是在不进入 RenderLoop 的情况下 Draw() 一个表单。我只想刷新图片 on-demand 而不是不断刷新。
实际上可以在不使用无限循环的情况下将图像显示为 Windows 形式。向表单添加缩放或调整大小等操作并执行操作(例如重绘图像)效果很好。
当然,如果您正在制作视频游戏,那不是最佳解决方案,因为您需要不断渲染图形。然而,对于图像查看器来说,将所有内容绘制到 windows 表单一次并在 ActionEvents 上更新它是完全没问题的。