2011-03-05 94 views
6

控件如何讓C#窗口半透明形式形成應用半透明形式,但不透明的C#

我已經試過了TransparentKey這使得它完全透明。並試圖Opacity,但它影響整個形式(與控件)。

我只想形成半透明的部分,但不是控件。

回答

7

可以使用hatch brush具有一定百分比,例如:

using System.Drawing.Drawing2D; 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     var hb = new HatchBrush(HatchStyle.Percent50, this.TransparencyKey); 

     e.Graphics.FillRectangle(hb,this.DisplayRectangle); 
    } 
+6

哦我的。結果令人厭惡。對不起,但它確實如此。 – 2014-06-17 17:01:52

2

有其中添加半透明度爲控制(未表格)中的溶液:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     // Apply opacity (0 to 255) 
     panel1.BackColor = Color.FromArgb(25, panel1.BackColor); 
    } 

在visual Studio中:(僅alpha激活d uring執行)

enter image description here

上執行Windows 7的:

enter image description here

上執行的舊的Windows 2003服務器:

enter image description here

來源:https://stackoverflow.com/a/4464161/1529139

+0

您的示例中表單不是半透明的,或者我錯過了什麼? – user1027167 2015-08-26 11:09:40

+0

也許背景非常統一,它不夠清晰,但它確實與alpha通道一起使用。如果你仔細觀察,你會看到陰影:) – 56ka 2015-08-26 11:17:07

+1

我測試過了,結果如下:button1是不透明的,panel1是半透明的,form1是不透明的。你看不到,表單背後是什麼,但問題是關於半透明表單。所以我有同樣的問題,但你的答案似乎是不正確的... – user1027167 2015-08-26 11:22:24

0

我發現哈奇刷怪誕,

相反的:

protected override void OnPaintBackground(PaintEventArgs e) { 
    var hb = new HatchBrush(HatchStyle.Percent80, this.TransparencyKey); 
    e.Graphics.FillRectangle(hb, this.DisplayRectangle); 
} 

我用:

protected override void OnPaintBackground(PaintEventArgs e) { 
    var sb = new SolidBrush(Color.FromArgb(100, 100, 100, 100)); 
    e.Graphics.FillRectangle(sb, this.DisplayRectangle); 
}