2015-05-27 88 views
-1

我試圖創建一個簡單的HUD來顯示我的統計信息,我正在繪製一個透明窗體並在其上添加控件,問題是,如果我在其上繪製字符串,那麼透明度鍵我'使用的是不完全地消失L:Trasparent窗體不能正常工作

Text drawed

正如你可以看到,如果文字是我畫,它的工作原理是,矩形上方,但如果它不盈「背景」,它messup,正如你所看到的,洋紅色就在那裏。

這裏是我的代碼:

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 { } 
     } 

你看到了什麼是錯?塔克你。

+0

不要吞下異常。 – SLaks

+0

這可能是因爲抗鋸齒。嘗試關閉它? – Blorgbeard

+0

你正在看到反鋸齒。你需要一個分層窗口。這並不簡單;考慮切換到WPF。 – SLaks

回答