示例#1
0
 private void SwapChainPanel_SizeChanged(object sender, SizeChangedEventArgs e)
 {
     if (isDXInitialized)
     {
         Size2 newSize = RenderSizeToPixelSize(e.NewSize);
         if (newSize.Width > swapChain.Description1.Width || newSize.Height > swapChain.Description1.Height)
         {
             Utilities.Dispose(ref this.backBufferView);
             Utilities.Dispose(ref this.backBufferTexture);
             swapChain.ResizeBuffers(swapChain.Description.BufferCount,
                                     (int)e.NewSize.Width,
                                     (int)e.NewSize.Height,
                                     swapChain.Description1.Format,
                                     swapChain.Description1.Flags);
             this.backBufferTexture = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(this.swapChain, 0);
             this.backBufferView    = new D3D11.RenderTargetView(this.device, this.backBufferTexture);
         }
         swapChain.SourceSize = newSize;
         tw = new TextWirter(device, swapChain, Color.Red, DisplayInformation.GetForCurrentView().LogicalDpi);
         tw.SetTextSize(36);
         #region D2D изменение размера окна
         //   BitmapProperties1 properties = new BitmapProperties1(
         //new PixelFormat(
         //    SharpDX.DXGI.Format.B8G8R8A8_UNorm,
         //    SharpDX.Direct2D1.AlphaMode.Premultiplied),
         //DisplayInformation.GetForCurrentView().LogicalDpi,
         //DisplayInformation.GetForCurrentView().LogicalDpi,
         //BitmapOptions.Target | BitmapOptions.CannotDraw);
         //   DXGI.Surface backBuffer = swapChain.GetBackBuffer<DXGI.Surface>(0);
         //   Utilities.Dispose(ref d2dTarget);
         //   d2dTarget = new Bitmap1(d2dContext, backBuffer, properties);
         //   SharpDX.DirectWrite.Factory fontFactory = new SharpDX.DirectWrite.Factory();
         //   Utilities.Dispose(ref textLayout1);
         //   Utilities.Dispose(ref textLayout2);
         //   Utilities.Dispose(ref textFormat);
         //   textFormat = new TextFormat(fontFactory, "Segoe UI", 24.0f);
         //   textLayout1 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
         //   textLayout2 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with no snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
         // d2dContext.DotsPerInch = new Size2F(DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi);
         #endregion
     }
 }
示例#2
0
        private void SwapChainPanel_Loaded(object sender, RoutedEventArgs e)
        {
            var v = ApplicationView.GetForCurrentView();

            v.FullScreenSystemOverlayMode = FullScreenSystemOverlayMode.Minimal;
            v.TryEnterFullScreenMode();
#if DEBUG
            var debugLevel = D3D11.DeviceCreationFlags.Debug | D3D11.DeviceCreationFlags.BgraSupport;
#else
            var debugLevel = D3D11.DeviceCreationFlags.None | D3D11.DeviceCreationFlags.BgraSupport;
#endif
            using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, debugLevel))
            {
                this.device = defaultDevice.QueryInterface <D3D11.Device2>();
            }
            this.deviceContext = this.device.ImmediateContext2;
            float pixelScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f;

            DXGI.SwapChainDescription1 swapChainDescription = new DXGI.SwapChainDescription1()
            {
                AlphaMode         = DXGI.AlphaMode.Premultiplied,
                BufferCount       = 2,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                Height            = (int)(this.SwapChainPanel.RenderSize.Height * pixelScale),
                Width             = (int)(this.SwapChainPanel.RenderSize.Width * pixelScale),
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Scaling           = DXGI.Scaling.Stretch,
                Stereo            = false,
                SwapEffect        = DXGI.SwapEffect.FlipSequential,
                Usage             = DXGI.Usage.BackBuffer | DXGI.Usage.RenderTargetOutput,
            };
            using (DXGI.Device3 dxgiDevice3 = this.device.QueryInterface <DXGI.Device3>())
            {
                using (DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <DXGI.Factory3>())
                {
                    using (DXGI.SwapChain1 swapChain1 = new DXGI.SwapChain1(dxgiFactory3, this.device, ref swapChainDescription))
                    {
                        this.swapChain = swapChain1.QueryInterface <DXGI.SwapChain2>();
                    }
                }
            }
            using (DXGI.ISwapChainPanelNative nativeObject = ComObject.As <DXGI.ISwapChainPanelNative>(this.SwapChainPanel))
            {
                nativeObject.SwapChain = this.swapChain;
            }
            this.backBufferTexture = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(this.swapChain, 0);
            this.backBufferView    = new D3D11.RenderTargetView(this.device, this.backBufferTexture);
            isDXInitialized        = true;
            tw = new TextWirter(device, swapChain, Color.Black, DisplayInformation.GetForCurrentView().LogicalDpi);
//            #region D2D

//#if DEBUG
//            var debug = SharpDX.Direct2D1.DebugLevel.Error;
//#else
//            var debug = SharpDX.Direct2D1.DebugLevel.None;
//#endif

//            d2dFactory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debug);
//            using (var dxgiDevice = device.QueryInterface<SharpDX.DXGI.Device>())
//            {
//                d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice);
//            }
//            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);

//            BitmapProperties1 properties = new BitmapProperties1(
//                new PixelFormat(
//                    SharpDX.DXGI.Format.B8G8R8A8_UNorm,
//                    SharpDX.Direct2D1.AlphaMode.Premultiplied),
//                DisplayInformation.GetForCurrentView().LogicalDpi,
//                DisplayInformation.GetForCurrentView().LogicalDpi,
//                BitmapOptions.Target | BitmapOptions.CannotDraw);
//            DXGI.Surface backBuffer = swapChain.GetBackBuffer<DXGI.Surface>(0);
//            d2dTarget = new Bitmap1(d2dContext, backBuffer, properties);
//            SharpDX.DirectWrite.Factory fontFactory = new SharpDX.DirectWrite.Factory();
//            textFormat = new TextFormat(fontFactory, "Segoe UI", 24.0f);
//            textLayout1 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
//            textLayout2 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with no snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
//            layoutY = 0.0f;
//            backgroundBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, Color.White);
//            textBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, Color.Black);
//            #endregion

            CompositionTarget.Rendering    += CompositionTarget_Rendering;
            Application.Current.Suspending += Application_Suspending;
        }