Exemplo n.º 1
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            // Direct2D
            var renderTargetProperties = new RenderTargetProperties()
            {
                PixelFormat = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore)
            };
            var hwndRenderTargetProperties = new HwndRenderTargetProperties()
            {
                Hwnd = this.Handle,
                PixelSize = new Size2(bounds.Width, bounds.Height),
                PresentOptions = PresentOptions.Immediately,
            };
            renderTarget = new WindowRenderTarget(factory, renderTargetProperties, hwndRenderTargetProperties);

            var bitmapProperties = new BitmapProperties()
            {
                PixelFormat = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore)
            };
            bitmap = new SharpDX.Direct2D1.Bitmap(renderTarget, new Size2(bounds.Width, bounds.Height), bitmapProperties);

            textFormat = new TextFormat(directWriteFacrtory, "Arial", FontWeight.Normal, SharpDX.DirectWrite.FontStyle.Normal, 96.0f);
            textFormat.ParagraphAlignment = ParagraphAlignment.Center;
            textFormat.TextAlignment = TextAlignment.Center;

            solidColorBrush = new SolidColorBrush(renderTarget, Color4.White);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Recreates the render target, if necessary, by calling SetRenderTarget</summary>
 protected override void RecreateRenderTarget()
 {
     var curRT = (WindowRenderTarget)D2dRenderTarget;
     Matrix3x2F xform = Transform;
     var hwnProps = new HwndRenderTargetProperties();
     hwnProps.Hwnd = Hwnd;
     hwnProps.PixelSize = curRT.PixelSize;
     hwnProps.PresentOptions = PresentOptions.Immediately;
     RenderTarget rt = new WindowRenderTarget(D2dFactory.NativeFactory, D2dFactory.RenderTargetProperties, hwnProps);            
     SetRenderTarget(rt);
     Transform = xform;           
 }
Exemplo n.º 3
0
        private void CreateD2DResources()
        {
            Factory2D     = new Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();

            properties.Hwnd           = m_displayHandle;
            properties.PixelSize      = new SharpDX.Size2(1024, 768);
            properties.PresentOptions = PresentOptions.None;

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);

            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;
        }
Exemplo n.º 4
0
        private void wnd_overlay_Resize(object sender, EventArgs e)
        {
            var factory    = new Factory();
            var renderProp = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2(this.Width, this.Height),
                PresentOptions = PresentOptions.Immediately,
            };

            Device = new WindowRenderTarget(factory,
                                            new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)),
                                            renderProp);
            factory.Dispose();
        }
Exemplo n.º 5
0
        public Direct2DGraphics(UserControl control)
        {
            factory       = new SharpDX.Direct2D1.Factory();
            windowScaling = Direct2DTheme.MainWindowScaling;

            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();

            properties.Hwnd           = control.Handle;
            properties.PixelSize      = new SharpDX.Size2(control.ClientSize.Width, control.ClientSize.Height);
            properties.PresentOptions = PresentOptions.None;

            renderTarget = new WindowRenderTarget(factory, new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), properties);

            Initialize();
        }
        public virtual void InitDevice()
        {
            FactoryManager.Init();
            this.TopMost = true;

            renderProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2(this.Width, this.Height),
                PresentOptions = PresentOptions.None
            };
            device = new WindowRenderTarget(FactoryManager.Factory, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), renderProperties);
            device.TextAntialiasMode = TextAntialiasMode.Cleartype;

            this.OnInitDevice();
        }
Exemplo n.º 7
0
        private void InitDX()
        {
            _d2dFactory = new SharpDX.Direct2D1.Factory(FactoryType.MultiThreaded);
            SharpDX.Direct2D1.PixelFormat p = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied);

            HwndRenderTargetProperties h = new HwndRenderTargetProperties();

            h.Hwnd           = _picContainer.Handle;
            h.PixelSize      = new Size2(_picContainer.Width, _picContainer.Height);
            h.PresentOptions = SharpDX.Direct2D1.PresentOptions.None;

            RenderTargetProperties r = new RenderTargetProperties(RenderTargetType.Hardware, p, 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);

            _renderTarget = new WindowRenderTarget(_d2dFactory, r, h);
            _imgFactory   = new ImagingFactory();
        }
        protected override void Initialize(DemoConfiguration demoConfiguration)
        {
            Factory2D = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
            properties.Hwnd = DisplayHandle;
            properties.PixelSize = new System.Drawing.Size(demoConfiguration.Width, demoConfiguration.Height);
            properties.PresentOptions = PresentOptions.None;

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);

            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;

            SceneColorBrush = new SolidColorBrush(RenderTarget2D, new Color4(1, 1, 1, 1));
        }
Exemplo n.º 9
0
        private void Form1_Load(object sender, EventArgs e)
        {
            SetLayeredWindowAttributes(Handle, (uint)0, 255, 0x00000002);

            DoubleBuffered = true;
            Width          = 64;
            Height         = 64;
            Location       = new System.Drawing.Point(0, 0);
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.DoubleBuffer |
                     ControlStyles.UserPaint |
                     ControlStyles.Opaque |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.SupportsTransparentBackColor, true);
            TopMost = true;
            Visible = true;

            DXFactory     = new Factory();
            DXFontFactory = new FontFactory();

            while (!IsRunning)
            {
                IsRunning = GetProcessAndHandles();
                Thread.Sleep(1);
            }

            DeviceRenderProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2(1920, 1080),
                PresentOptions = PresentOptions.None
            };

            //Init DirectX
            RenderDevice = new WindowRenderTarget(DXFactory, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), DeviceRenderProperties);

            LoopThread = new Thread(new ParameterizedThreadStart(SDXThread));

            LoopThread.Priority     = ThreadPriority.Highest;
            LoopThread.IsBackground = true;
            LoopThread.Start();

            MainForm mf = new MainForm(this);

            mf.ShowDialog();
        }
Exemplo n.º 10
0
        /// <summary>
        /// Initializes a Direct2D device used to draw to the screen.
        /// For drawing, you MUST add your methods to the Direct2D Rendering Delegate (direct2DRenderMethod).
        /// </summary>
        public void InitializeDirectX(IntPtr targetWindowHandle)
        {
            try
            {
                // Wait for any draw operation to finish.
                lock (RenderLock)
                {
                    // Mark D2D Setup as incomplete, disallow Rendering.
                    Direct2DSetupComplete = false;

                    // Dispose Render Target if Necessary
                    Direct2DWindowTarget?.Dispose();

                    // Create the D2D Factory which aids with the creation of a WindowRenderTarget object.
                    Factory direct2DFactory = new Factory(FactoryType.SingleThreaded);

                    // Retrieve window size of target window.
                    Point windowSize = WindowProperties.GetClientAreaSize2(targetWindowHandle);

                    // Set the render properties!
                    HwndRenderTargetProperties direct2DRenderTargetProperties = new HwndRenderTargetProperties
                    {
                        Hwnd           = targetWindowHandle,
                        PixelSize      = new Size2(windowSize.X, windowSize.Y),
                        PresentOptions = PresentOptions.None
                    };

                    // Assign the Window Render Target
                    Direct2DWindowTarget = new WindowRenderTarget
                                           (
                        direct2DFactory,
                        new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)),
                        direct2DRenderTargetProperties
                                           );

                    // Clear the screen of the Window Render Target.
                    DirectXClearScreen();

                    // Mark D2D Setup as Complete, allow Rendering.
                    Direct2DSetupComplete = true;
                }
            }
            catch (Exception ex)
            {
                Bindings.PrintError?.Invoke("[libReloaded] Failed to initialize DirectX rendering to window | " + ex.Message);
            }
        }
Exemplo n.º 11
0
        private void LoadOverlay(object sender, EventArgs e)
        {
            DoubleBuffered = true;
            SetStyle(
                ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
                ControlStyles.Opaque | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor,
                true);

            // Find VNC window and position itself on top.
            KeepWindowInPlace();

            factory          = new Factory();
            renderProperties = new HwndRenderTargetProperties
            {
                Hwnd      = Handle,
                PixelSize = new SharpDX.Size2(Size.Width, Size.Height),

                PresentOptions = PresentOptions.None
            };

            // Initialize DirectX
            marg.Left   = 0;
            marg.Top    = 0;
            marg.Right  = Width;
            marg.Bottom = Height;

            DwmExtendFrameIntoClientArea(Handle, ref marg);
            device = new WindowRenderTarget(factory, new RenderTargetProperties(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), renderProperties)
            {
                AntialiasMode     = AntialiasMode.PerPrimitive,
                TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Default
            };

            overlayLoaded = true;
            TopMost       = true;

            // Timer that follows our VNC window, if we decide to move it.
            windowFollower = new System.Timers.Timer
            {
                Interval = 3000
            };

            windowFollower.Elapsed  += KeepWindowInPlace;
            windowFollower.AutoReset = false;
            windowFollower.Enabled   = true;
            windowFollower.Stop();
        }
Exemplo n.º 12
0
        private void Overlay_Load(object sender, EventArgs e)
        {
            if (!BEbypass)
            {
                ServiceController sc = new ServiceController();
                sc.ServiceName = "BEService";
                if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.Paused) // Check if the Battleye service is running, if it is, stop it
                {
                    sc.Stop();
                    sc.WaitForStatus(ServiceControllerStatus.Stopped);
                }
                Stuff.OpenProc();
            }
            else
            {
                Stuff.GetBaseAddress();
            }

            BackColor       = Color.White;
            TransparencyKey = Color.White;
            TopMost         = true;
            FormBorderStyle = FormBorderStyle.None;
            DoubleBuffered  = true;

            GetWindowRect(handle, out rect);
            Size = new Size(rect.right - rect.left, rect.bottom - rect.top);
            Top  = rect.top;
            Left = rect.left;

            factory          = new Factory();
            RenderProperties = new HwndRenderTargetProperties
            {
                Hwnd           = Handle,
                PixelSize      = new SharpDX.Size2(Size.Width, Size.Height),
                PresentOptions = PresentOptions.None
            };
            device   = new WindowRenderTarget(factory, new RenderTargetProperties(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), RenderProperties);
            BoxBrush = new SolidColorBrush(device, new RawColor4(255, 0, 0, 2f));

            thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(DrawingThread))
            {
                Priority     = System.Threading.ThreadPriority.Highest,
                IsBackground = true
            };
            thread.Start();
        }
Exemplo n.º 13
0
        private void setupInstance(bool deleteOld = false)
        {
            if (deleteOld)
            {
                try
                {
                    _brush.Dispose();
                    _font.Dispose();

                    _fontFactory.Dispose();
                    _factory.Dispose();
                    _device.Dispose();
                }
                catch
                {
                }
            }

            _factory     = new Factory(SharpDX.Direct2D1.FactoryType.MultiThreaded, DebugLevel.None);
            _fontFactory = new FontFactory();

            RECT bounds = default(RECT);

            User32.GetWindowRect(TargetHandle, out bounds);

            _targetProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = TargetHandle,
                PixelSize      = new Size2(Math.Abs(bounds.Right - bounds.Left), Math.Abs(bounds.Bottom - bounds.Top)),
                PresentOptions = VSync ? PresentOptions.None : PresentOptions.Immediately
            };

            var renderTargetProperties = new RenderTargetProperties(
                RenderTargetType.Hardware,
                new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                0, 0,
                RenderTargetUsage.None, FeatureLevel.Level_DEFAULT
                );

            _device = new WindowRenderTarget(_factory, renderTargetProperties, _targetProperties);

            _device.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Aliased;
            _device.AntialiasMode     = AntialiasMode.Aliased;

            _brush = new SolidColorBrush(_device, new RawColor4(0, 0, 0, 0));
        }
Exemplo n.º 14
0
        /// <summary>
        /// Inits the direct2D and direct write.
        /// </summary>
        private void InitDirect2DAndDirectWrite()
        {
            Factory2D     = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            var properties = new HwndRenderTargetProperties {
                Hwnd = panel1.Handle, PixelSize = new Size2(panel1.ClientSize.Width, panel1.ClientSize.Height), PresentOptions = PresentOptions.None
            };

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(), properties)
            {
                AntialiasMode     = AntialiasMode.PerPrimitive,
                TextAntialiasMode = TextAntialiasMode.Cleartype
            };

            brush = new SolidColorBrush(RenderTarget2D, Color.Black);
        }
Exemplo n.º 15
0
        private void InitRender(Control container)
        {
            // Direct2D
            var renderTargetProperties = new RenderTargetProperties()
            {
                PixelFormat = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore)
            };
            var hwndRenderTargetProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = container.Handle,
                PixelSize      = new Size2(container.Width, container.Height),
                PresentOptions = PresentOptions.Immediately,
            };
            var renderTarget = new WindowRenderTarget(factory, renderTargetProperties, hwndRenderTargetProperties);

            render[container.Handle] = renderTarget;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Inits the direct2D and direct write.
        /// </summary>
        private void InitDirect2DAndDirectWrite()
        {
            Factory2D     = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            var properties = new HwndRenderTargetProperties();

            properties.Hwnd           = renderControl.Handle;
            properties.PixelSize      = new Size2(renderControl.ClientSize.Width, renderControl.ClientSize.Height);
            properties.PresentOptions = PresentOptions.None;

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);
            RenderTarget2D.AntialiasMode     = AntialiasMode.PerPrimitive;
            RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype;

            SceneColorBrush = new SolidColorBrush(RenderTarget2D, Color.Black);
        }
Exemplo n.º 17
0
        public void SetTargetControl(IHasWidthAndHeight box)
        {
            _box = box;
            var factory2D  = new Factory(FactoryType.MultiThreaded, DebugLevel.None);
            var properties = new HwndRenderTargetProperties
            {
                Hwnd           = _box.Handle,
                PixelSize      = new Size2(512, 448),
                PresentOptions = PresentOptions.None
            };

            _renderTarget2D = new WindowRenderTarget(factory2D,
                                                     new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), properties)
            {
                AntialiasMode = AntialiasMode.PerPrimitive
            };
        }
Exemplo n.º 18
0
        static void Main()
        {
            using (RenderForm form = new RenderForm(FormCaption))
                using (D2DFactory d2dFactory = new D2DFactory())
                {
                    form.ClientSize = new System.Drawing.Size(Width, Height);

                    HwndRenderTargetProperties wtp = new HwndRenderTargetProperties();
                    wtp.Hwnd           = form.Handle;
                    wtp.PixelSize      = new Size2(Width, Height);
                    wtp.PresentOptions = PresentOptions.Immediately;
                    using (WindowRenderTarget renderTarget = new WindowRenderTarget(d2dFactory, new RenderTargetProperties(), wtp))
                    {
                        RunGame(form, renderTarget);
                    }
                }
        }
Exemplo n.º 19
0
        private void Form1_Load(object sender, EventArgs e)
        {
            this.DoubleBuffered = true;
            this.Width          = (int)xResolution;// set your own size
            this.Height         = (int)yResolution;
            WindowState         = FormWindowState.Maximized;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |// this reduce the flicker
                          ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.DoubleBuffer |
                          ControlStyles.UserPaint |
                          ControlStyles.Opaque |
                          ControlStyles.ResizeRedraw |
                          ControlStyles.SupportsTransparentBackColor, true);
            this.TopMost = true;
            this.Visible = true;

            factory          = new Factory();
            fontFactory      = new FontFactory();
            renderProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2((int)xResolution, (int)yResolution),
                PresentOptions = PresentOptions.None
            };

            //SetLayeredWindowAttributes(this.Handle, 0, 255, Managed.LWA_ALPHA);// caution directx error

            //Init DirectX
            device = new WindowRenderTarget(factory, new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), renderProperties);
            SharpDX.Mathematics.Interop.RawColor4 r4color = new SharpDX.Mathematics.Interop.RawColor4();
            r4color.A       = 255;
            r4color.R       = 122;
            r4color.G       = 122;
            r4color.B       = 122;
            solidColorBrush = new SolidColorBrush(device, r4color);
            // Init font's
            font      = new TextFormat(fontFactory, fontFamily, fontSize);
            fontSmall = new TextFormat(fontFactory, fontFamily, fontSizeSmall);
            //line = new device.DrawLine;

            sDX = new Thread(new ParameterizedThreadStart(sDXThread));

            sDX.Priority     = ThreadPriority.Highest;
            sDX.IsBackground = true;
            sDX.Start();
        }
Exemplo n.º 20
0
        public GPURenderer1(System.Windows.Forms.Control cltr)
        {
            Program.MainLog.Add(MessageType.MSG, "Creating D2D Renderer");
            IsHardware = true;
            refTrack.Add(this);
            float scale = GetScaleFactor();

            this.cltr = cltr;
            if (factory == null || factory.IsDisposed)
            {
                factory = new Factory(FactoryType.MultiThreaded, DebugLevel.Error);
                Program.MainLog.Add(MessageType.MSG, "Factory created: {0}", factory.NativePointer);
                factoryAccesCount = 1;
            }
            else
            {
                factoryAccesCount++;
                Program.MainLog.Add(MessageType.MSG, "Accessing Factory {0}, cnt = {1}", factory.NativePointer, factoryAccesCount);
            }
            HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties();
            RenderTargetProperties     rtProps   = new RenderTargetProperties();

            hwndProps.Hwnd           = cltr.Handle;
            hwndProps.PixelSize      = new Size2(cltr.Width, cltr.Height);
            hwndProps.PresentOptions = PresentOptions.Immediately;
            rtProps.Type             = RenderTargetType.Default;
            mainRenderTarget         = new WindowRenderTarget(factory, rtProps, hwndProps);
            Program.MainLog.Add(MessageType.MSG, "Render Target created: {0}", mainRenderTarget.NativePointer);
            //RenderTarget = mainRenderTarget;
            Width  = cltr.Width;
            Height = cltr.Height;
            mainRenderTarget.AntialiasMode = AntialiasMode.PerPrimitive;
            mainRenderTarget.DotsPerInch   = new Size2F(
                mainRenderTarget.DotsPerInch.Width / scale, mainRenderTarget.DotsPerInch.Height / scale);

            writeFactory = new text.Factory();
            Program.MainLog.Add(MessageType.MSG, "Direct Write Factory created: {0}", writeFactory.NativePointer);

            if (brush_gray == null)
            {
                brush_gray = new SolidColorBrush(mainRenderTarget, gdi.Color.Gray.ToColor4());
            }

            renderTarget = mainRenderTarget;
            Program.MainLog.Add(MessageType.MSG, "Renderer initialized");
        }
 /// <summary>
 /// 使用指定的渲染目标控件创建渲染目标。
 /// </summary>
 /// <param name="control">渲染的目标控件。</param>
 public void CreateRenderTarget(Control control)
 {
     if (SupportD3D)
     {
         // 创建 Direct3D SwapChain。
         SwapChainDescription swapChainDesc = new SwapChainDescription()
         {
             BufferCount       = 1,
             Usage             = Usage.RenderTargetOutput,
             OutputHandle      = control.Handle,
             IsWindowed        = true,
             ModeDescription   = new ModeDescription(0, 0, new Rational(60, 1), Format),
             SampleDescription = new SampleDescription(1, 0),
             SwapEffect        = SwapEffect.Discard
         };
         this.swapChain = new SwapChain(dxgiDevice.GetParent <Adapter>().GetParent <DXGIFactory>(),
                                        d3DDevice, swapChainDesc);
         this.backBuffer   = Surface.FromSwapChain(this.swapChain, 0);
         this.targetBitmap = new Bitmap1(this.d2DContext, backBuffer);
         this.renderTarget = new DeviceContext(this.d2DDevice, DeviceContextOptions.None);
         ((DeviceContext)this.renderTarget).Target = targetBitmap;
     }
     else
     {
         // 创建 Direct2D 工厂。
         // 渲染参数。
         RenderTargetProperties renderProps = new RenderTargetProperties
         {
             PixelFormat = D2PixelFormat,
             Usage       = RenderTargetUsage.None,
             Type        = RenderTargetType.Default
         };
         // 渲染目标属性。
         HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties()
         {
             Hwnd           = control.Handle,
             PixelSize      = new Size2(control.ClientSize.Width, control.ClientSize.Height),
             PresentOptions = PresentOptions.None
         };
         // 渲染目标。
         this.renderTarget = new WindowRenderTarget(d2DFactory, renderProps, hwndProps)
         {
             AntialiasMode = AntialiasMode.PerPrimitive
         };
     }
 }
Exemplo n.º 22
0
        private void Form1_Load(object sender, EventArgs e)
        {
            this.DoubleBuffered = true;
            this.Width          = 1920;                         // set your own size
            this.Height         = 1080;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | // this reduce the flicker
                          ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.DoubleBuffer |
                          ControlStyles.UserPaint |
                          ControlStyles.Opaque |
                          ControlStyles.ResizeRedraw |
                          ControlStyles.SupportsTransparentBackColor, true);
            this.TopMost     = true;
            this.Visible     = true;
            factory          = new Factory();
            fontFactory      = new FontFactory();
            renderProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2(1920, 1090),
                PresentOptions = PresentOptions.None
            };
            RegisterHotKey(this.Handle, 1, 2, (int)Keys.F2); //CTRL + F2
            RegisterHotKey(this.Handle, 2, 2, (int)Keys.F3); //CTRL + F3
            RegisterHotKey(this.Handle, 3, 2, (int)Keys.F4); //CTRL + F4
            RegisterHotKey(this.Handle, 4, 2, (int)Keys.F5); //CTRL + F5

            //Init DirectX
            device = new WindowRenderTarget(factory, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), renderProperties)
            {
                AntialiasMode = AntialiasMode.PerPrimitive
            };
            // Init font's
            font      = new TextFormat(fontFactory, fontFamily, fontSize);
            fontSmall = new TextFormat(fontFactory, fontFamily, fontSizeSmall);
            // line = new device.DrawLine;

            //sDX.Start();
            sDX = new Thread(new ParameterizedThreadStart(SDXThread))
            {
                //Priority = ThreadPriority.,
                IsBackground = true
            };
            sDX.Start();
        }
Exemplo n.º 23
0
        /// <summary>
        /// This method creates the render target and all associated D2D and DWrite resources
        /// </summary>
        void CreateDeviceResources()
        {
            // Only calls if resources have not been initialize before
            if (renderTarget == null)
            {
                // The text format
                textFormat = dwriteFactory.CreateTextFormat("Bodoni MT", 24, DWrite.FontWeight.Normal, DWrite.FontStyle.Italic, DWrite.FontStretch.Normal);

                // Create the render target
                SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight);
                RenderTargetProperties props = new RenderTargetProperties();
                HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None);
                renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps);

                // A black brush to be used for drawing text
                ColorF cf = new ColorF(0, 0, 0, 1);
                blackBrush = renderTarget.CreateSolidColorBrush(cf);

                // Create a linear gradient.
                GradientStop[] stops = 
                { 
                    new GradientStop(1, new ColorF(1f, 0f, 0f, 0.25f)),
                    new GradientStop(0, new ColorF(0f, 0f, 1f, 1f))
                };

                GradientStopCollection pGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap);
                LinearGradientBrushProperties gradBrushProps = new LinearGradientBrushProperties(new Point2F(50, 25), new Point2F(25, 50));

                linearGradientBrush = renderTarget.CreateLinearGradientBrush(gradBrushProps, pGradientStops);

                gridPatternBitmapBrush = CreateGridPatternBrush(renderTarget);

                solidBrush1 = renderTarget.CreateSolidColorBrush(new ColorF(0.3F, 0.5F, 0.65F, 0.25F));
                solidBrush2 = renderTarget.CreateSolidColorBrush(new ColorF(0.0F, 0.0F, 0.65F, 0.5F));
                solidBrush3 = renderTarget.CreateSolidColorBrush(new ColorF(0.9F, 0.5F, 0.3F, 0.75F));

                // Create a linear gradient.
                stops[0] = new GradientStop(1, new ColorF(0f, 0f, 0f, 0.25f));
                stops[1] = new GradientStop(0, new ColorF(1f, 1f, 0.2f, 1f));
                GradientStopCollection radiantGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap);

                RadialGradientBrushProperties radialBrushProps = new RadialGradientBrushProperties(new Point2F(25, 25), new Point2F(0, 0), 10, 10);
                radialGradientBrush = renderTarget.CreateRadialGradientBrush(radialBrushProps, radiantGradientStops);
            }
        }
Exemplo n.º 24
0
        /// <summary>
        /// This method creates the render target and all associated D2D and DWrite resources
        /// </summary>
        void CreateDeviceResources()
        {
            // Only calls if resources have not been initialize before
            if (renderTarget == null)
            {
                // The text format
                textFormat = dwriteFactory.CreateTextFormat("Bodoni MT", 24, DWrite.FontWeight.Normal, DWrite.FontStyle.Italic, DWrite.FontStretch.Normal);

                // Create the render target
                SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight);
                RenderTargetProperties     props     = new RenderTargetProperties();
                HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None);
                renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps);

                // A black brush to be used for drawing text
                ColorF cf = new ColorF(0, 0, 0, 1);
                blackBrush = renderTarget.CreateSolidColorBrush(cf);

                // Create a linear gradient.
                GradientStop[] stops =
                {
                    new GradientStop(1, new ColorF(1f, 0f, 0f, 0.25f)),
                    new GradientStop(0, new ColorF(0f, 0f, 1f, 1f))
                };

                GradientStopCollection        pGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap);
                LinearGradientBrushProperties gradBrushProps = new LinearGradientBrushProperties(new Point2F(50, 25), new Point2F(25, 50));

                linearGradientBrush = renderTarget.CreateLinearGradientBrush(gradBrushProps, pGradientStops);

                gridPatternBitmapBrush = CreateGridPatternBrush(renderTarget);

                solidBrush1 = renderTarget.CreateSolidColorBrush(new ColorF(0.3F, 0.5F, 0.65F, 0.25F));
                solidBrush2 = renderTarget.CreateSolidColorBrush(new ColorF(0.0F, 0.0F, 0.65F, 0.5F));
                solidBrush3 = renderTarget.CreateSolidColorBrush(new ColorF(0.9F, 0.5F, 0.3F, 0.75F));

                // Create a linear gradient.
                stops[0] = new GradientStop(1, new ColorF(0f, 0f, 0f, 0.25f));
                stops[1] = new GradientStop(0, new ColorF(1f, 1f, 0.2f, 1f));
                GradientStopCollection radiantGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap);

                RadialGradientBrushProperties radialBrushProps = new RadialGradientBrushProperties(new Point2F(25, 25), new Point2F(0, 0), 10, 10);
                radialGradientBrush = renderTarget.CreateRadialGradientBrush(radialBrushProps, radiantGradientStops);
            }
        }
Exemplo n.º 25
0
        public KarnaughGraphDraw(KarnaughGraphControl target)
        {
            this.width  = target.Width;
            this.height = target.Height;

            var hwnd = new HwndRenderTargetProperties();

            hwnd.Hwnd      = target.Handle;
            hwnd.PixelSize = new Size2(this.width, this.height);

            this.fact   = new SharpDX.Direct2D1.Factory();
            render      = new SharpDX.Direct2D1.WindowRenderTarget(this.fact, new RenderTargetProperties(), hwnd);
            this.target = target;

            //いろいろ設定
            LineColor  = new SharpDX.Direct2D1.SolidColorBrush(render, new RawColor4(1f, 1f, 1f, 1f));
            background = new RawColor4(0.3f, 0.3f, 0.3f, 0f);
        }
        public SharpDxGraphicsManagerWrapper(Panel targetPanel, Action renderAction)
            : base(renderAction)
        {
            var Factory2D     = new SharpDX.Direct2D1.Factory();
            var FactoryDWrite = new SharpDX.DirectWrite.Factory();

            var properties = new HwndRenderTargetProperties();

            properties.Hwnd           = targetPanel.Handle;
            properties.PixelSize      = new SharpDX.Size2(targetPanel.Width, targetPanel.Height);
            properties.PresentOptions = PresentOptions.None;

            _renderTarget = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);

            _renderTarget.AntialiasMode = AntialiasMode.PerPrimitive;

            _wrapper = new DxGraphicsWrapper(_renderTarget, FactoryDWrite, new ImagingFactory());
        }
        protected override void Initialize(DemoConfiguration demoConfiguration)
        {
            Factory2D     = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();

            properties.Hwnd           = DisplayHandle;
            properties.PixelSize      = new SharpDX.Size2(demoConfiguration.Width, demoConfiguration.Height);
            properties.PresentOptions = PresentOptions.None;

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);

            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;


            SceneColorBrush = new SolidColorBrush(RenderTarget2D, Color.White);
        }
Exemplo n.º 28
0
        private void MainWindow_Load(object sender, EventArgs e)
        {
            this.DoubleBuffered = true;
            this.Width          = 1920;// set your own size
            this.Height         = 1080;
            this.Left           = 0;
            this.Top            = 0;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |// this reduce the flicker
                          ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.DoubleBuffer |
                          ControlStyles.UserPaint |
                          ControlStyles.Opaque |
                          ControlStyles.ResizeRedraw |
                          ControlStyles.SupportsTransparentBackColor, true);
            this.TopMost = true;
            this.Visible = true;

            factory          = new Factory();
            fontFactory      = new FontFactory();
            renderProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2(1920, 1080),
                PresentOptions = PresentOptions.None
            };

            //SetLayeredWindowAttributes(this.Handle, 0, 255, Managed.LWA_ALPHA);// caution directx error

            //Init DirectX
            device = new WindowRenderTarget(factory, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), renderProperties);

            solidColorBrush = new SolidColorBrush(device, Color.White);
            // Init font's
            font       = new TextFormat(fontFactory, fontFamily, FontWeight.Bold, FontStyle.Normal, FontStretch.Normal, fontSize);
            fontSmall  = new TextFormat(fontFactory, fontFamily, fontSizeSmall);
            textLayout = new TextLayout(fontFactory, "placeholder", font, 400f, 200f);
            //line = new device.DrawLine;

            sDX = new Thread(new ParameterizedThreadStart(Draw));

            sDX.Priority     = ThreadPriority.AboveNormal;
            sDX.IsBackground = true;
            sDX.Start();
        }
        /// <summary>
        /// Initializes a Direct2D device used to draw to the screen. You MUST set the Direct2D Drawing Delegate to your own method, see the wiki for sample.
        /// </summary>
        public void Initialize_DirectX()
        {
            // Init DirectX
            // This initializes the DirectX device. It needs to be done once.

            // Wait for Heroes Window to spawn, when spawned, set the DirectX properties.
            Thread Get_Heroes_Window_Thread = new Thread
                                              (
                () =>
            {
                try
                {
                    // Wait for Sonic Heroes Window to spawn.
                    while (overlayWinForm.Window_Setup_Complete == false)
                    {
                        Thread.Sleep(8);
                    }
                    // Create the D2D1 Factory
                    SharpDX.Direct2D1.Factory Direct2D_Factory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded);

                    // Set the render properties!
                    SharpDX.Direct2D1.HwndRenderTargetProperties Direct2D_Render_Target_Properties = new HwndRenderTargetProperties();
                    Direct2D_Render_Target_Properties.Hwnd           = overlayWinForm.Handle;
                    Direct2D_Render_Target_Properties.PixelSize      = new SharpDX.Size2(overlayWinForm.Width, overlayWinForm.Height);
                    Direct2D_Render_Target_Properties.PresentOptions = PresentOptions.None;

                    direct2DWindowTarget = new SharpDX.Direct2D1.WindowRenderTarget
                                           (
                        Direct2D_Factory,
                        new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)),
                        Direct2D_Render_Target_Properties
                                           );

                    DirectX_Clear_Screen();     // Clear the residue graphics left from the Windows form which was made transparent and clickthrough.
                    Ready_To_Render = true;
                }
                catch
                {
                }
            }
                                              );

            Get_Heroes_Window_Thread.Start();
        }
Exemplo n.º 30
0
        /// <summary>
        /// Inits the direct2D and direct write.
        /// </summary>
        private void InitDirect2DAndDirectWrite()
        {
            Factory2D     = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            var properties = new HwndRenderTargetProperties {
                Hwnd = Handle, PixelSize = ClientSize, PresentOptions = PresentOptions.None
            };

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(), properties)
            {
                AntialiasMode     = AntialiasMode.PerPrimitive,
                TextAntialiasMode = TextAntialiasMode.Cleartype
            };

            SceneColorBrush = new SolidColorBrush(RenderTarget2D, new Color4(1, 0, 0, 0));

            CustomTextRenderer = new CustomTextRenderer(Factory2D, RenderTarget2D);
        }
Exemplo n.º 31
0
        // Remember change the values of the form in the designer.
        private void Overlay_SharpDX_Load(object sender, EventArgs e)
        {
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            UpdatePos();

            this.DoubleBuffered = true; // reduce the flicker
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.Opaque | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
            this.TopMost = true;
            this.Visible = true;
            var exStyle = User32.GetWindowLong(this.Handle, User32.GWL_EXSTYLE);

            exStyle |= User32.WS_EX_LAYERED;
            exStyle |= User32.WS_EX_TRANSPARENT;
            User32.SetWindowLong(this.Handle, User32.GWL_EXSTYLE, (IntPtr)exStyle);
            factory          = new Factory();
            fontFactory      = new FontFactory();
            renderProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2(this.Width, this.Height),
                PresentOptions = PresentOptions.None
            };

            //Init DirectX
            device = new WindowRenderTarget(factory, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), renderProperties);

            // if you want use DirectX direct renderer, you can use this brush and fonts.
            // of course you can change this as you want.
            solidColorBrush = new SolidColorBrush(device, new SharpDX.Mathematics.Interop.RawColor4(255, 0, 0, 255));
            font            = new TextFormat(fontFactory, fontFamily, fontSize);


            threadDX = new Thread(new ParameterizedThreadStart(_loop_DXThread));

            threadDX.Priority     = ThreadPriority.Highest;
            threadDX.IsBackground = true;
            threadDX.Start();

            device.BeginDraw();
            device.Clear(new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 0));
            device.EndDraw();
        }
Exemplo n.º 32
0
        /// <summary>
        /// 初始化 HwndRenderTarget。
        /// </summary>
        private void InitHwndRenderTarget()
        {
            // 创建 Direct2D 单线程工厂。
            using (DXFactory dxFactory = new DXFactory(FactoryType.SingleThreaded))
            {
                // 渲染参数。
                RenderTargetProperties renderProps = new RenderTargetProperties
                {
                    PixelFormat = D2PixelFormat,
                    Usage       = RenderTargetUsage.None,
                    Type        = RenderTargetType.Default
                };
                // 渲染目标属性。
                HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties()
                {
                    // 承载控件的句柄。
                    Hwnd = _ctrl.Handle,
                    // 控件的尺寸。
                    PixelSize      = RenderTargetClientSize,
                    PresentOptions = PresentOptions.None,
                };
                // 渲染目标。
                hwndRenderTarget = new WindowRenderTarget(dxFactory, renderProps, hwndProps)
                {
                    AntialiasMode = AntialiasMode.PerPrimitive,
                };
            }
            // 初始化画刷资源。
            //hwndBrush = new SolidColorBrush(hwndRenderTarget, Color.Black);

            //_textFormat = new SharpDX.DirectWrite.TextFormat(new SharpDX.DirectWrite.Factory(), _ctrl.Font.Name, 15);

            // 在控件大小被改变的时候必须改变渲染目标的大小,
            // 否则会导致绘制结果被拉伸,引起失真。
            _ctrl.SizeChanged += (sender, e) =>
            {
                lock (_lockObj)
                {
                    hwndRenderTarget.Resize(RenderTargetClientSize);
                }
            };
            _ctrl.HandleDestroyed += (sender, e) => _cancelTask.Cancel(true);
        }
Exemplo n.º 33
0
        private void wnd_overlay_Resize(object sender, EventArgs e)
        {
            var factory    = new Factory();
            var renderProp = new HwndRenderTargetProperties
            {
                Hwnd           = Handle,
                PixelSize      = new Size2(Width, Height),
                PresentOptions = PresentOptions.Immediately
            };

            var initialStyle = WinAPI.GetWindowLong(Handle, -20);

            WinAPI.SetWindowLong(Handle, -20, initialStyle | 0x80000 | 0x20 | 0x80);

            Device = new WindowRenderTarget(factory,
                                            new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)),
                                            renderProp);
            factory.Dispose();
        }
Exemplo n.º 34
0
        public wnd_overlay()
        {
            if (!Sig.Init())
            {
                MessageBox.Show("Failed to Load!\nMake Sure You Have CS:GO Started Before Launching.");
                Environment.Exit(Environment.ExitCode);
            }

            Paint += Wnd_overlay_Paint;

            var initialStyle = WinAPI.GetWindowLong(Handle, -20);

            WinAPI.SetWindowLong(Handle, -20, initialStyle | 0x80000 | 0x20 | 0x80);

            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.DoubleBuffer |
                     ControlStyles.UserPaint |
                     ControlStyles.Opaque |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.SupportsTransparentBackColor, true);

            InitializeComponent();

            var factory    = new Factory();
            var renderProp = new HwndRenderTargetProperties
            {
                Hwnd           = Handle,
                PixelSize      = new Size2(Width, Height),
                PresentOptions = PresentOptions.Immediately
            };

            Device = new WindowRenderTarget(factory,
                                            new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)),
                                            renderProp);


            factory.Dispose();

            updateWnd.RunWorkerAsync();
            OnResize(null);
            SharpDXThread.Start();
        }
Exemplo n.º 35
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RenderTarget"/> class.
        /// </summary>
        /// <param name="hwnd">The window handle.</param>
        /// <param name="width">The width of the window.</param>
        /// <param name="height">The height of the window.</param>
        public RenderTarget(IntPtr hwnd, double width, double height)
        {
            Direct2DFactory = PerspexLocator.Current.GetService<Factory>();
            DirectWriteFactory = PerspexLocator.Current.GetService<DwFactory>();

            RenderTargetProperties renderTargetProperties = new RenderTargetProperties
            {
            };

            HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties
            {
                Hwnd = hwnd,
                PixelSize = new Size2((int)width, (int)height),
                PresentOptions = PresentOptions.Immediately,
            };

            _renderTarget = new WindowRenderTarget(
                Direct2DFactory,
                renderTargetProperties,
                hwndProperties);
        }
Exemplo n.º 36
0
		private void InitializeGraphics()
		{
			factory = new Factory(FactoryType.SingleThreaded, DebugLevel.None);

			renderTargetProperties = new RenderTargetProperties(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));

			hwndRenderTargetProperties = new HwndRenderTargetProperties();
			hwndRenderTargetProperties.Hwnd = this.Handle;
			hwndRenderTargetProperties.PixelSize = new SharpDX.Size2(Width, Height);
			hwndRenderTargetProperties.PresentOptions = PresentOptions.None;

			windowRenderTarget = new WindowRenderTarget(factory, renderTargetProperties, hwndRenderTargetProperties);

			backgroundColor = new RawColor4(BackColor.R, BackColor.G, BackColor.B, BackColor.A);

			borderColor = new SolidColorBrush(windowRenderTarget, new RawColor4(0.5f, 0.5f, 0.5f, 1));
			wayColor = new SolidColorBrush(windowRenderTarget, new RawColor4(0, 0, 1, 0.3f));
			currentPositionColor = new SolidColorBrush(windowRenderTarget, new RawColor4(0, 0, 1, 1));

			graphicsInitialized = true;
		}
Exemplo n.º 37
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RenderTarget"/> class.
        /// </summary>
        /// <param name="hwnd">The window handle.</param>
        /// <param name="width">The width of the window.</param>
        /// <param name="height">The height of the window.</param>
        public RenderTarget(IntPtr hwnd)
        {
            _hwnd = hwnd;
            Direct2DFactory = PerspexLocator.Current.GetService<Factory>();
            DirectWriteFactory = PerspexLocator.Current.GetService<DwFactory>();

            RenderTargetProperties renderTargetProperties = new RenderTargetProperties
            {
            };

            HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties
            {
                Hwnd = hwnd,
                PixelSize = _savedSize = GetWindowSize(),
                PresentOptions = PresentOptions.Immediately,
            };

            _renderTarget = new WindowRenderTarget(
                Direct2DFactory,
                renderTargetProperties,
                hwndProperties);
        }
        public Direct2DRenderer(IntPtr hwnd, bool limitFps)
        {
            _factory = new Factory();

            _fontFactory = new FontFactory();

            RECT bounds;//immer 1920x1080. resizing muss durch die Overlay klasse geregelt sein
            NativeMethods.GetWindowRect(hwnd, out bounds);

            var targetProperties = new HwndRenderTargetProperties
            {
                Hwnd = hwnd,
                PixelSize = new Size2(bounds.Right - bounds.Left, bounds.Bottom - bounds.Top),
                PresentOptions = limitFps ? PresentOptions.None : PresentOptions.Immediately //Immediatly -> Zeichnet sofort ohne auf 60fps zu locken. None lockt auf 60fps
            };

            var prop = new RenderTargetProperties(RenderTargetType.Hardware, new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);

            _device = new WindowRenderTarget(_factory, prop, targetProperties)
            {
                TextAntialiasMode = TextAntialiasMode.Cleartype,
                AntialiasMode = AntialiasMode.PerPrimitive
            };
        }
Exemplo n.º 39
0
        /// <summary>
        /// Setup DirectX Renderer.
        /// </summary>
        private void SetupDX()
        {
            d2dFactory = new SharpDX.Direct2D1.Factory();
            fontFactory = new SharpDX.DirectWrite.Factory();

            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
            properties.Hwnd = this.Handle;
            properties.PixelSize = new System.Drawing.Size(this.Width, this.Height);
            properties.PresentOptions = PresentOptions.None;

            d2dRenderTarget = new WindowRenderTarget(d2dFactory, new RenderTargetProperties(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore)), properties);
            d2dRenderTarget.AntialiasMode = AntialiasMode.Aliased;
            d2dRenderTarget.TextAntialiasMode = TextAntialiasMode.Cleartype;

            EmptyTileBrush = new SolidColorBrush(d2dRenderTarget, Colors.Aquamarine);
            solidColorBrush = new SolidColorBrush(d2dRenderTarget, Colors.White);
            MonsterBrush = new SolidColorBrush(d2dRenderTarget, Colors.Wheat);
            TowerBrush = new SolidColorBrush(d2dRenderTarget, Colors.Purple);
        }
Exemplo n.º 40
0
 private void CreateRenderObjects()
 {
     RenderTargetProperties renderTargetProperties = new RenderTargetProperties();
     HwndRenderTargetProperties hwRenderTargetProperties = new HwndRenderTargetProperties()
     {
         Hwnd = this.Handle,
         PixelSize = new Size2(ClientSize.Width, ClientSize.Height)
     };
     renderTarget = new WindowRenderTarget(factory, renderTargetProperties, hwRenderTargetProperties);
     renderObjectsCreated = true;
     OnCreateRenderObjects(EventArgs.Empty);
 }
Exemplo n.º 41
0
		/// <summary>
		/// 使用指定的渲染目标控件创建渲染目标。
		/// </summary>
		/// <param name="control">渲染的目标控件。</param>
		public void CreateRenderTarget(Control control)
		{
			if (SupportD3D)
			{
				// 创建 Direct3D SwapChain。
				SwapChainDescription swapChainDesc = new SwapChainDescription()
				{
					BufferCount = 1,
					Usage = Usage.RenderTargetOutput,
					OutputHandle = control.Handle,
					IsWindowed = true,
					ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format),
					SampleDescription = new SampleDescription(1, 0),
					SwapEffect = SwapEffect.Discard
				};
				this.swapChain = new SwapChain(dxgiDevice.GetParent<Adapter>().GetParent<DXGIFactory>(),
					d3DDevice, swapChainDesc);
				this.backBuffer = Surface.FromSwapChain(this.swapChain, 0);
				this.targetBitmap = new Bitmap1(this.d2DContext, backBuffer);
				this.renderTarget = new DeviceContext(this.d2DDevice, DeviceContextOptions.None);
				((DeviceContext)this.renderTarget).Target = targetBitmap;
			}
			else
			{
				// 创建 Direct2D 工厂。
				// 渲染参数。
				RenderTargetProperties renderProps = new RenderTargetProperties
				{
					PixelFormat = D2PixelFormat,
					Usage = RenderTargetUsage.None,
					Type = RenderTargetType.Default
				};
				// 渲染目标属性。
				HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties()
				{
					Hwnd = control.Handle,
					PixelSize = new Size2(control.ClientSize.Width, control.ClientSize.Height),
					PresentOptions = PresentOptions.None
				};
				// 渲染目标。
				this.renderTarget = new WindowRenderTarget(d2DFactory, renderProps, hwndProps)
				{
					AntialiasMode = AntialiasMode.PerPrimitive
				};
			}
		}
        public virtual void InitDevice()
        {
            FactoryManager.Init();
            this.TopMost = true;

            renderProperties = new HwndRenderTargetProperties()
            {
                Hwnd = this.Handle,
                PixelSize = new Size2(this.Width, this.Height),
                PresentOptions = PresentOptions.None
            };
            device = new WindowRenderTarget(FactoryManager.Factory, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), renderProperties);
            device.TextAntialiasMode = TextAntialiasMode.Cleartype;

            this.OnInitDevice();

        }
Exemplo n.º 43
0
        private static void CreateResources()
        {
            if (renderTarget != null) { renderTarget.Dispose(); }
            if (defaultBrush != null) { defaultBrush.Dispose(); }
            if (greenBrush != null) { greenBrush.Dispose(); }
            if (redBrush != null) { redBrush.Dispose(); }
            if (backgroundBrush != null) { backgroundBrush.Dispose(); }


            HwndRenderTargetProperties wtp = new HwndRenderTargetProperties();
            wtp.Hwnd = mainForm.Handle;
            wtp.PixelSize = new Size2(mainForm.ClientSize.Width, mainForm.ClientSize.Height);
            wtp.PresentOptions = PresentOptions.Immediately;
            renderTarget = new WindowRenderTarget(d2dFactory, new RenderTargetProperties(), wtp);

            defaultBrush = new SolidColorBrush(renderTarget, Color.White);
            greenBrush = new SolidColorBrush(renderTarget, Color.Green);
            redBrush = new SolidColorBrush(renderTarget, Color.Red);
            backgroundBrush = new SolidColorBrush(renderTarget, new Color4(0.3f, 0.3f, 0.3f, 0.5f));

            textRenderer.AssignResources(renderTarget, defaultBrush);

        }
Exemplo n.º 44
0
        /// <summary>
        /// This method creates the render target and all associated D2D and DWrite resources
        /// </summary>
        void CreateDeviceResources()
        {
            // Only calls if resources have not been initialize before
            if (renderTarget == null)
            {
                // Create the render target
                SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight);
                RenderTargetProperties props = new RenderTargetProperties();
                HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None);
                renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps);

                // Create the black brush for text
                blackBrush = renderTarget.CreateSolidColorBrush(new ColorF(0 ,0, 0, 1));

                inlineImage = new ImageInlineObject(renderTarget, wicFactory, "TextInlineImage.img1.jpg");

                TextRange textRange = new TextRange(14, 1);
                textLayout.SetInlineObject(inlineImage, textRange);
            }
        }
Exemplo n.º 45
0
        void InitD2D()
        {
            _factoryD2D = new SharpDX.Direct2D1.Factory();
            _factoryDWrite = new SharpDX.DirectWrite.Factory();

            HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
            properties.Hwnd = this.Handle;
            properties.PixelSize = new SharpDX.Size2(this.Width, this.Height);
            properties.PresentOptions = PresentOptions.Immediately;

            _renderTarget = new WindowRenderTarget(_factoryD2D,
                new RenderTargetProperties(_renderTargetType,
                    new PixelFormat(Format.Unknown, AlphaMode.Premultiplied), 96.0f, 96.0f,
                    RenderTargetUsage.GdiCompatible, FeatureLevel.Level_DEFAULT), properties);
        }
Exemplo n.º 46
0
        private void SetRenderMode(RenderModes rm)
        {
            lock (renderSyncObject)
            {
                renderMode = rm;
                //if (!IsInitialized && !isInitializing)
                //    return;

                //clean up objects that will be invalid after RenderTarget change
                if (dcRenderTarget != null)
                {
                    dcRenderTarget.Dispose();
                    dcRenderTarget = null;
                }
                if (hwndRenderTarget != null)
                {
                    hwndRenderTarget.Dispose();
                    hwndRenderTarget = null;
                }
                if (bitmapRenderTarget != null)
                {
                    bitmapRenderTarget.Dispose();
                    bitmapRenderTarget = null;
                }
                //bitmap = null; //the bitmap created in dc render target can't be used in hwnd render target

                // Create the screen render target
                RECT cRect;
                User32.GetClientRect(fWindowHandle, out cRect);
                
                var size = new SizeU((uint)cRect.Width, (uint)cRect.Height);
                PixelFormat pFormat = new PixelFormat(Format.B8G8R8A8_UNORM, AlphaMode.Ignore);
                var props = new RenderTargetProperties {PixelFormat = pFormat, Usage = RenderTargetUsage.GdiCompatible};

                //if (renderMode == RenderModes.DCRenderTarget || renderMode == RenderModes.BitmapRenderTargetOnPaint)
                //{
                //    dcRenderTarget = d2DFactory.CreateDCRenderTarget(props);
                //    if (renderMode == RenderModes.BitmapRenderTargetOnPaint)
                //    {
                //        bitmapRenderTarget =
                //            dcRenderTarget.CreateCompatibleRenderTarget(
                //            CompatibleRenderTargetOptions.GdiCompatible,
                //            new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height));
                //    }
                //    render = null;
                //}
                //else
                {
                    HwndRenderTargetProperties hProps = new HwndRenderTargetProperties(fWindowHandle, size, PresentOptions.RetainContents);
                    hwndRenderTarget = d2DFactory.CreateHwndRenderTarget(props,  hProps);
                    
                    //if (renderMode == RenderModes.BitmapRenderTargetRealTime)
                    //{
                    //    bitmapRenderTarget =
                    //        hwndRenderTarget.CreateCompatibleRenderTarget(
                    //        CompatibleRenderTargetOptions.GdiCompatible,
                    //        new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height));
                    //}
                    //render = RenderSceneInBackground;
                }
            }
        }
Exemplo n.º 47
0
        /// <summary>
        /// Inits the direct2D and direct write.
        /// </summary>
        private void InitDirect2DAndDirectWrite()
        {
            Factory2D = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            var properties = new HwndRenderTargetProperties();
            properties.Hwnd = renderControl.Handle;
            properties.PixelSize = renderControl.ClientSize;
            properties.PresentOptions = PresentOptions.None;

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);
            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;
            RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype;

            SceneColorBrush = new SolidColorBrush(RenderTarget2D, new Color4(1, 0, 0, 0));
        }
        /// <summary>
        /// This method creates the render target and associated D2D and DWrite resources
        /// </summary>
        void CreateDeviceResources()
        {
            // Only calls if resources have not been initialize before
            if (renderTarget == null)
            {
                // Create the render target
                SizeU size = new SizeU((uint)renderControl.ClientSize.Width, (uint)renderControl.ClientSize.Height);
                HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(renderControl.Handle, size, PresentOptions.RetainContents);
                renderTarget = d2dFactory.CreateHwndRenderTarget(renderProps, hwndProps);

                // Create an initial black brush
                brushes.Add(renderTarget.CreateSolidColorBrush(new ColorF(Color.Black.ToArgb())));
                currentBrushIndex = 0;
            }
        } 
Exemplo n.º 49
0
 /// <summary>	
 /// Creates an <see cref="WindowRenderTarget"/>, a render target that renders to a window.	
 /// </summary>	
 /// <remarks>	
 /// When you create a render target and hardware acceleration is available, you allocate resources on the computer's GPU. By creating a render target once and retaining it as long as possible, you gain performance benefits. Your application should create render targets once and hold onto them for the life of the application or until the {{D2DERR_RECREATE_TARGET}} error is received. When you receive this error, you need to recreate the render target (and any resources it created).	
 /// </remarks>
 /// <param name="factory">an instance of <see cref = "SharpDX.Direct2D1.Factory" /></param>
 /// <param name="renderTargetProperties">The rendering mode, pixel format, remoting options, DPI information, and the minimum DirectX support required for hardware rendering. For information about supported pixel formats, see  {{Supported Pixel  Formats and Alpha Modes}}.</param>
 /// <param name="hwndProperties">The window handle, initial size (in pixels), and present options.</param>
 public WindowRenderTarget(Factory factory, RenderTargetProperties renderTargetProperties, HwndRenderTargetProperties hwndProperties)
     : base(IntPtr.Zero)
 {
     factory.CreateHwndRenderTarget(ref renderTargetProperties, hwndProperties, this);
 }
Exemplo n.º 50
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //Init Direct Draw
            //Set Rendering properties
            RenderTargetProperties renderProp = new RenderTargetProperties()
            {
                DpiX = 0,
                DpiY = 0,
                MinLevel = FeatureLevel.Level_10,
                PixelFormat = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied),
                Type = RenderTargetType.Hardware,
                Usage = RenderTargetUsage.None
            };

            //set hwnd target properties (permit to attach Direct2D to window)
            HwndRenderTargetProperties winProp = new HwndRenderTargetProperties()
            {
                Hwnd = this.Handle,
                PixelSize = new DrawingSize(this.ClientSize.Width, this.ClientSize.Height),
                PresentOptions = PresentOptions.Immediately
            };

            //target creation
            target = new WindowRenderTarget(factory, renderProp, winProp);

            //create red and white brushes
            redBrush = new SharpDX.Direct2D1.SolidColorBrush(target, SharpDX.Color.Red);
            whiteBrush = new SharpDX.Direct2D1.SolidColorBrush(target, SharpDX.Color.White);

            //create a linear gradient brush
            var grad = new LinearGradientBrushProperties()
            {
                StartPoint = new DrawingPointF(ClientSize.Width / 2, ClientSize.Height / 2),
                EndPoint = new DrawingPointF(ClientSize.Width, ClientSize.Height)
            };

            var stopCollection = new GradientStopCollection(target, new GradientStop[]
            {
                new GradientStop(){Color=SharpDX.Color.Azure ,Position=0F},
                new GradientStop(){Color=SharpDX.Color.Yellow,Position=0.2F},
                new GradientStop(){Color=SharpDX.Color.Green,Position=0.4F},
                new GradientStop(){Color=SharpDX.Color.Red,Position=1F},
            }, ExtendMode.Mirror);

            gradient = new SharpDX.Direct2D1.LinearGradientBrush(target, grad, stopCollection);

            //create textformat
            textFormat = new SharpDX.DirectWrite.TextFormat(factoryWrite, "Arial", 36);

            //avoid artifacts
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        }
Exemplo n.º 51
0
        /// <summary>
        /// Inits the direct2D and direct write.
        /// </summary>
        private void InitDirect2DAndDirectWrite()
        {
            Factory2D = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            var properties = new HwndRenderTargetProperties();
            properties.Hwnd = renderControl.Handle;
            properties.PixelSize = new Size2(renderControl.ClientSize.Width, renderControl.ClientSize.Height);
            properties.PresentOptions = PresentOptions.None;

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);
            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;
            RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype;

            SceneColorBrush = new SolidColorBrush(RenderTarget2D, Color.Black);

            renderControl.Paint += new PaintEventHandler(renderControl_Paint);
            renderControl.Resize += new EventHandler(renderControl_Resize);
        }
Exemplo n.º 52
0
        protected void Initialize()
        {
            // Load all of the factories
            Factories.LoadFactories();

            // Setup the graphics
            Console.WriteLine("Setup Graphics");

            var hwndprop = new HwndRenderTargetProperties();
            hwndprop.Hwnd = Handle;
            hwndprop.PixelSize = new Size2(Constants.Width, Constants.Height);
            hwndprop.PresentOptions = PresentOptions.Immediately;

            var rendprop = new RenderTargetProperties();
            rendprop.PixelFormat = new PixelFormat(DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied);
            rendprop.Type = RenderTargetType.Hardware;

            SetClientSizeCore(Constants.Width, Constants.Height);
            RenderTarget2D = new WindowRenderTarget(Factories.Factory2D, rendprop, hwndprop);

            // Tick timer
            Console.WriteLine("Setup Timer");
            stopwatch = new Stopwatch();
            stopwatch.Start();
            physicsDelayTicks = Stopwatch.Frequency / updatesPerSecond;
            ticksToMilliFactor = 1.0 / (Stopwatch.Frequency / 1000.0);
            ticksToPhysicsStepPercentFactor = 1.0 / physicsDelayTicks;
            lastPhysicsStepTicks = 0.0;

            // Load all assets and resources
            Constants.Load();

            // Setup the main menu
            Console.WriteLine("Setup Menu");
            GameController.LoadInitialLevel(new LevelMenu());

            // Start the loop
            MainLoop();
        }
Exemplo n.º 53
0
        public Canvas()
        {
            InitializeComponent();

            // start the factory
            RenderVariables.factory = new Factory(FactoryType.MultiThreaded);

            this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.UserPaint |
                          ControlStyles.OptimizedDoubleBuffer, true);

            // criate the properties of the controler
            HwndRenderTargetProperties windowProperties = new HwndRenderTargetProperties();

            // fill the properties of the controller
            windowProperties.Hwnd = RenderArea.Handle;
            windowProperties.PixelSize = new Size2(RenderArea.Size.Width, RenderArea.Size.Height);
            windowProperties.PresentOptions = PresentOptions.None;

            // create the render target
            RenderVariables.windowsTarget = new WindowRenderTarget(RenderVariables.factory,
                new RenderTargetProperties(new PixelFormat(SharpDX.DXGI.Format.Unknown, AlphaMode.Premultiplied)),
                windowProperties);

            // link the render target to the windows target
            RenderVariables.renderTarget = RenderVariables.windowsTarget;

            // create the write factory
            RenderVariables.WriteFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);

            // create write target
            // set the antialias mode
            RenderVariables.renderTarget.AntialiasMode = AntialiasMode.PerPrimitive;

            // set that the control must pe paint one time
            isDirty = false;

            // set the initial tradformation
            RenderVariables.renderTarget.Transform = Matrix3x2.Identity;

            // init the offset
            _ScreenOffset = new Vector2(10, 10);

            // init zoom
            _Zoom = new SizeF(1, 1);

            // set the origineBrush
            if (originBrush != null)
                originBrush.Dispose();
            originBrush = new SolidColorBrush(RenderVariables.renderTarget, new Color4(Color.Bisque.R, Color.Bisque.G, Color.Bisque.B, 1.0f));

            // init elements list
            ElementsList = new List<CanvasElements>();

            // init the color for the borders
            SelectedBorderColor = Color.Beige;
            EditBorderColor = Color.Brown;
            SelectedElementInEditMode = false;

            // draw the canvas
            ReDraw();

            // get mouse events from RenderArea
            RenderArea.MouseDown += new MouseEventHandler(RenderArea_MouseDown);
            RenderArea.MouseMove += new MouseEventHandler(RenderArea_MouseMove);
            RenderArea.MouseUp += new MouseEventHandler(RenderArea_MouseUp);
            RenderArea.MouseClick += new MouseEventHandler(RenderArea_MouseClick);
            RenderArea.MouseDoubleClick += new MouseEventHandler(RenderArea_MouseDoubleClick);

            // link propertie grid
            propertyGrid1.SelectedObject = this;
        }
Exemplo n.º 54
0
        /// <summary>
        /// Inits the direct2D and direct write.
        /// </summary>
        private void InitDirect2DAndDirectWrite()
        {
            Factory2D = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            var properties = new HwndRenderTargetProperties { Hwnd = panel1.Handle, PixelSize = new Size2(panel1.ClientSize.Width, panel1.ClientSize.Height), PresentOptions = PresentOptions.None };

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(), properties)
            {
                AntialiasMode = AntialiasMode.PerPrimitive,
                TextAntialiasMode = TextAntialiasMode.Cleartype
            };

            brush = new SolidColorBrush(RenderTarget2D, Color.Black);
        }
        /// <summary>
        /// Inits the direct2D and direct write.
        /// </summary>
        private void InitDirect2DAndDirectWrite()
        {
            Factory2D = new SharpDX.Direct2D1.Factory();
            FactoryDWrite = new SharpDX.DirectWrite.Factory();

            var properties = new HwndRenderTargetProperties {Hwnd = Handle, PixelSize = ClientSize, PresentOptions = PresentOptions.None};

            RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(), properties)
                                 {
                                     AntialiasMode = AntialiasMode.PerPrimitive,
                                     TextAntialiasMode = TextAntialiasMode.Cleartype
                                 };

            SceneColorBrush = new SolidColorBrush(RenderTarget2D, new Color4(1, 0, 0, 0));

            CustomTextRenderer = new CustomTextRenderer(Factory2D, RenderTarget2D);
        }
Exemplo n.º 56
0
        /// <summary>
        /// 初始化绘图有关组件
        /// </summary>
        /// <param name="width">游戏窗口宽度</param>
        /// <param name="height">游戏窗口高度</param>
        /// <param name="title">游戏窗口标题</param>
        private void InitRenderComponent(int width, int height, string title)
        {
            WindowSize = new Size2(width, height);

            var factory = new Factory(FactoryType.SingleThreaded);

            var hwndRenderTargetProperties = new HwndRenderTargetProperties {
                Hwnd = Form.Handle,
                PixelSize = new Size2(Form.Width, Form.Height),
                PresentOptions = PresentOptions.RetainContents
            };

            var pixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Ignore);
            var renderTargetProperties = new RenderTargetProperties(
                RenderTargetType.Hardware, pixelFormat, 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);

            renderTarget = new WindowRenderTarget(factory, renderTargetProperties, hwndRenderTargetProperties);
        }