Trasparent Form不能正常工作

时间:2015-05-27 20:00:18

标签: c# winforms transparency hud

我试图创建一个简单的HUD来向我显示统计数据,我正在绘制一个透明的表单并在其上添加控件,问题是,如果我在其上绘制一个字符串,那么trasparency key I' m使用不会完全消失:

Text drawed

正如你所看到的,如果文字在我绘制的矩形之上,它就会起作用,但如果它不在前面"背景",它就会弄乱,如你所见,洋红色是那里。

这是我的代码:

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 { }
        }
你知道那有什么问题吗? Tahnk你。<​​/ p>

1 个答案:

答案 0 :(得分:0)

如果您将表单设为分层窗口,则可以使用Alpha-Blending。 请参阅https://code.msdn.microsoft.com/windowsapps/CSWinFormLayeredWindow-23cdc375以获取一个好例子。