-1

Im trying to create a simple HUD to show me statistics, I'm drawing a transparent form and adding controls on it, the problem is, if I draw a string on it, the trasparency key I'm using doesn't vanish completly:

Text drawed

As you can see, if the text is above the rectangle that I draw, it works, but if it is not infront a "background", it messup, as you can see, the Magenta color is there.

Here is my code:

private void InitializeComponent()
        {
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Text = "";

            this.Size = new System.Drawing.Size(500, 500);
            this.Location = new Point(0, 0);

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

            this.BackColor = Color.Magenta;
            this.TransparencyKey = Color.Magenta;

            this.DoubleBuffered = true;

            this.ResumeLayout(false);
            this.PerformLayout();
        }

void HudWindow_Paint(object sender, PaintEventArgs e)
        {
            if (e == null) return;
            if (!Enabled) return;

            try
            {
                Graphics g = e.Graphics;
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                foreach (Control control in this.Controls)
                {
                    GraphicsPath gp = new GraphicsPath();
                    Brush color = new SolidBrush(Color.White);

                    if (control is CustomControls.NormalRectangle) {
                        CustomControls.NormalRectangle rect = control as CustomControls.NormalRectangle;
                        gp.AddRectangle(new Rectangle(rect.FromX, rect.FromY, rect.Width, rect.Height));
                        color = new SolidBrush(rect.Color);
                    }
                    else if (control is CustomControls.LabelText)
                    {
                        CustomControls.LabelText lbl = control as CustomControls.LabelText;
                        color = new SolidBrush(lbl.Color);
                        gp.AddString(lbl.Text, new FontFamily("Tahoma"), (int)FontStyle.Bold, 15, new Point(lbl.X, lbl.Y), new StringFormat());
                    }

                    Pen test = new Pen(Color.Black, 2);
                    g.DrawPath(test, gp);
                    g.FillPath(color, gp);
                }
            }
            catch { }
        }

Do you see what is wrong with that? Tahnk you.

Kyore
  • 388
  • 2
  • 7
  • 29

1 Answers1

0

If you make your Form a layered Window, you can use Alpha-Blending. See https://code.msdn.microsoft.com/windowsapps/CSWinFormLayeredWindow-23cdc375 for a good example.