sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.68k stars 642 forks source link

WindowRenderTarget Clear getting Black #1107

Open igormaozao opened 5 years ago

igormaozao commented 5 years ago

[Question] Hello, I'm trying to make a transparent background in a Windows Form to draw some texts on it to work like a HUD.

But, the Device.Clear(Color.Transparent) does not work, the background gets all black, here is my current code:

public HUD()
        {

            InitializeComponent();

            var renderProp = new HwndRenderTargetProperties
            {
                Hwnd = this.Handle,
                PixelSize = new Size2(Client.GameScreen.width, Client.GameScreen.height),
                PresentOptions = PresentOptions.None
            };

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

        }

private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Text = "";
            this.AutoSize = false;

            // Code to Set the Size Here....

            this.UseWaitCursor = false;
            this.Cursor = null;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.TopMost = true;

            this.BackColor = Color.LimeGreen;
            this.TransparencyKey = Color.LimeGreen;

            this.ForeColor = Color.White;
            this.DoubleBuffered = true;
            this.PerformLayout();
            this.ResumeLayout(false);

        }

The code abouve is the HUD class extending the Form class, and in my main window, I have the loop:

var hud = new HUD();
void RunHud()
        {
            while (true)
            {

                // Clear the target. 
                hud.Device.BeginDraw();

                hud.Device.Clear(Color.Transparent.ToRaw()); //Extension method to convert to RawColor4

                hud.Device.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Aliased;
                hud.Device.AntialiasMode = AntialiasMode.Aliased;

                // Draw a block of text that will be clipped against the specified layout rectangle.
                hud.Device.DrawText("This text is long enough to overflow the designed region but w. ", textFormat, new SharpDX.Mathematics.Interop.RawRectangleF(500, 500, 700, 700), textBruch, DrawTextOptions.Clip);

                hud.Device.EndDraw();

                Thread.Sleep(10);
            }
        }

This is just a "Hello World" to test SharpDX, I've tried a lot of different colors in the Device.Clear() and in my Form BackColor/TransparencyColor.

I'm using Windows 7, VS2017 and SharpDX 4.2, not sure if I'm missing something.