c# SharpDX -从控制到全屏

本文关键字:控制 SharpDX | 更新日期: 2023-09-27 18:08:07

我有一个问题,从渲染切换到窗口窗体控件渲染到全屏。所以我把这个问题分解成一个小的示例项目,其中只包括交换链和设备初始化以及全屏切换。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using SharpDX;
using SharpDX.DXGI;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using Device = SharpDX.Direct3D11.Device;
namespace ControlToFullscreenTest
{
    static class Program
    {
        static Device device;
        static SwapChain swapchain;
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 form = new Form1();
            Control control = form.pictureBox1; //Doesnt work
            //Control control = form;  //works
            control.MouseClick += new MouseEventHandler(click);
            SwapChainDescription sd = new SwapChainDescription()
            {
                BufferCount = 1,
                Flags = SwapChainFlags.AllowModeSwitch,
                IsWindowed = true, //false doesnt work too !
                ModeDescription = new ModeDescription(control.ClientSize.Width, control.ClientSize.Height, Rational.Empty, Format.R8G8B8A8_UNorm),
                OutputHandle = control.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput,
            };
            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, sd, out device, out swapchain);
            Factory DxgiFactory;
            SharpDX.DXGI.Device d = device.QueryInterface<SharpDX.DXGI.Device>();
            Adapter a = d.GetParent<Adapter>();
            DxgiFactory = a.GetParent<Factory>();
            DxgiFactory.MakeWindowAssociation(control.Handle, WindowAssociationFlags.None);
            form.Show();
            while(form.Focused)
            {
                swapchain.Present(0, PresentFlags.None);
                Application.DoEvents();
            }
        }
        static void click(object o, EventArgs args)
        {
            swapchain.ResizeBuffers(1, 1920, 1080, Format.Unknown, SwapChainFlags.AllowModeSwitch);
            swapchain.SetFullscreenState(true, null);
        }
    }
}

该开关在正常的windows窗体中工作良好,但在控件中不工作。

swapchain.SetFullscreenState(true, null);

扔HRESULT: [0x887A0001], Module: [SharpDX. 0001]。, ApiCode: [DXGI_ERROR_INVALID_CALL/InvalidCall]

MSDN中没有关于它的任何内容。甚至不可能在全屏下用控件初始化Swapchain。我知道我可以通过创建一个新窗口来修复这个问题,然后在全屏模式下渲染,但我想知道如果没有这个改变,它是否可能。

如果有人有一个想法,信息或我没有找到的链接,那就太好了。

c# SharpDX -从控制到全屏

将SwapChain设置为全屏需要一个非子控件,所以你的图片框不满足这个条件。

使用Debug设备创建标志,当您尝试设置为全屏时,以下错误消息将弹出在您的输出窗口:

DXGI ERROR: IDXGISwapChain::SetFullscreenState: Fullscreen is not允许针对子窗口的交换链。应用程序是建议创建第二个针对非子窗口的交换链。[杂项错误#82:]