2011-03-05 95 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); 
}