public BackgroundRenderingSample()
        {
            InitializeComponent();

            if (!IsRenderingInBackgroundThread) // In case we are running BackgroundDXEngineRenderer on the Main UI thread, mark that on the GUI
            {
                BGRenderingTitleTextBlock.TextDecorations.Add(TextDecorations.Strikethrough);
            }


            // We need to wait until this sample is loaded, otherwise we do not get the correct dpiScale values.
            this.Loaded += delegate(object sender, RoutedEventArgs args)
            {
                // Get dpi scale setting and store that for later
                BackgroundDXEngineRenderer.GetActualDpiScaleValues(this, out _dpiScaleX, out _dpiScaleY);

                // Create D3DHost control that will host the 3D rendered scene
                CreateD3DHost();

                // Create MouseCameraController that will allow user to rotate the camera
                CreateMouseCameraController();
            };


            // Subscribe to rendering to collect Main UI thread FPS statistics
            CompositionTarget.Rendering += UpdateMainUIThreadFPS;

            // We also count mouse move events to see when the UI thread is not responsing well
            this.PreviewMouseMove += delegate(object sender, MouseEventArgs args)
            {
                _uiThreadMouseMovesCount++;
            };


            // Handle cleanup
            this.Unloaded += delegate(object sender, RoutedEventArgs args)
            {
                if (_targetPositionCamera.IsRotating)
                {
                    _targetPositionCamera.StopRotation();
                }

                CompositionTarget.Rendering -= UpdateMainUIThreadFPS;

                DXViewportBorder.Child = null;

                if (_d3dHost != null)
                {
                    _d3dHost.Dispose();
                    _d3dHost = null;
                }

                if (_backgroundDXEngineRenderer != null)
                {
                    _backgroundDXEngineRenderer.Dispose();
                }
            };
        }
        private void CreateD3DHost()
        {
            // D3DHost is a control that creates a new window that is hosted inside this WPF application and returns the window's handle (hWnd).
            // The hWnd can be used to initialize DXEngine so it will render the 3D scene to the specified area.
            // NOTE: The D3DHost is used with DXViewport3D when the PresentationType == DirectXOverlay

            _d3dHost = new Ab3d.DirectX.Controls.D3DHost();
            _d3dHost.HandleCreated += _d3dHost_HandleCreated;
            _d3dHost.Painting      += _d3dHost_Painting;
            _d3dHost.SizeChanging  += _d3dHost_SizeChanging;

            // Add D3DHost to this WPF application
            DXViewportBorder.Child = _d3dHost;
        }