SharpDx绘制形式而无需使用Renderloop将其无限实现



嗨,就像标题所说的那样,我试图为一个项目做一个图像查看器(在Windows Image Viewer的类型中有点(。我看到了很多代码将它们的Renderform展示到Renderloop中,但是我不喜欢此解决方案,因为我不想在Renderloop中无限地刷新图像。我只想在需要重新绘制时(以示例为例。(问题是,问题是,现在我尝试使用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的情况下绘制表单。我只想刷新图像,而不是不断刷新它。

实际上可以在不使用无限环路的情况下将图像显示在窗口形式中。添加缩放或调整大小之类的动作在您的表单中并执行操作(这样重新绘制了您的图像(。

当然,如果您正在制作视频游戏,那不是最好的解决方案,因为您想要不断的图形渲染。但是,对于图像查看器而言,只能将所有内容绘制到Windows表单上并在ActionEvents上进行更新。

最新更新