Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

WindowRenderTarget Clear getting Black #1107

Open
igormaozao opened this issue Nov 29, 2018 · 0 comments
Open

WindowRenderTarget Clear getting Black #1107

igormaozao opened this issue Nov 29, 2018 · 0 comments

Comments

@igormaozao
Copy link

[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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant