有幾種情況:
1.如果LABEL控件未放在其他控件上,如PICTURES控件或其他容器控件中,可以修改LABEL控件的
Backcolor屬性-> web-> Transparent
2.如果LABEL控件存在于其他控件中/或放置在其他控件上則上面的方法無(wú)效,需按下面的方法操作:
A.在窗體初始事件中添加代碼:(這里以PICTURES控件為例)
label.Parent = pictureBox;
label.BackColor = Color.Transparent;
B.也可以在構(gòu)造函數(shù)里添加以下代碼:
this.label1.BackColor = Color.FromArgb(0, Color.Transparent);
最近項(xiàng)目中用到需要控件背景透明的功能,但在.Net中子控件只能相對(duì)于父控件才能透明,如果控件覆蓋在兩個(gè)或多個(gè)其他控件上的時(shí)候,就無(wú)法達(dá)到透明效果。
經(jīng)過(guò)多次驗(yàn)證找到了如下方法,效果如下圖:
方法一 僅對(duì)父控件透明
1. 在控件的構(gòu)造函數(shù)中用SetStyle設(shè)定控件支持背景透明,然后將Me.BackColor 設(shè)定為 Color.Transparent(透明) 或 Alpha值設(shè)定為小于255(半透明)。代碼如下:
?VB
Public Sub New()
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Transparent ' 透明
Me.BackColor = Color.FromArgb(100, 255, 255, 255) ' 半透明
End Sub
?C#
public TransparentPictureBox() // {
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent; // 透明
this.BackColor = Color.FromArgb(100, 255, 255, 255); // 半透明
}
使用該控件時(shí)?VB
Me.Label1.Parent = Me.PictureBox1
Me.Label1.Location = New Point(Me.Label1.Left - Me.PictureBox1.Left, Me.Label1.Top - Me.PictureBox1.Top)
?C#
this.label1.Parent = this.pictureBox1;
this.label1.Location = new Point(this.label1.Left - this.pictureBox1.Left, this.label1.Top - this.pictureBox1.Top);
方法二 對(duì)所有控件透明
對(duì)所有控件透明的方法,就是將要實(shí)現(xiàn)透明效果的控件下覆蓋的區(qū)域繪制成BMP圖片,并將圖片作為透明控件的背景繪制在透明控件上。
分兩步: 1. 繪制父控件 2. 繪制透明控件下覆蓋的區(qū)域的控件
首先, 重載OnPaintBackground并在該函數(shù)中回調(diào)Control.InvokePaintBackground 方法和 Control.InvokePaint方法。繪制父控件
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent) {
this.DrawParentControl(this.Parent, pevent);
}
private void DrawParentControl(Control c, System.Windows.Forms.PaintEventArgs pevent) {
using (Bitmap bmp = new Bitmap(c.Width, c.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) {
using (Graphics g = Graphics.FromImage(bmp)) {
using (PaintEventArgs p = new PaintEventArgs(g, c.ClientRectangle)) {
this.InvokePaintBackground(c, p);
this.InvokePaint(c, p);
}
}
int offsetX = this.Left + (int)Math.Floor((double)(this.Bounds.Width - this.ClientRectangle.Width) / 2.0);
int offsetY = this.Top + (int)Math.Floor((double)(this.Bounds.Height - this.ClientRectangle.Height) / 2.0);
pevent.Graphics.DrawImage(bmp, this.ClientRectangle, new Rectangle(offsetX, offsetY, this.ClientRectangle.Width, this.ClientRectangle.Height), GraphicsUnit.Pixel);
}
}
而后,根據(jù)父控件上放置的各個(gè)控件逐個(gè)循環(huán)繪制背景。
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent) {
//
for (int i = this.Parent.Controls.Count - 1; i >= 0; i--) {
Control c = this.Parent.Controls[i];
if (c == this) {
break;
}
if (this.Bounds.IntersectsWith(c.Bounds) == false) {
continue;
}
this.DrawBackControl(c, pevent);
}
}
private void DrawBackControl(Control c, System.Windows.Forms.PaintEventArgs pevent) {
using (Bitmap bmp = new Bitmap(c.Width, c.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) {
c.DrawToBitmap(bmp, new Rectangle(0, 0, c.Width, c.Height));
int offsetX = (c.Left - this.Left) - (int)Math.Floor((double)(this.Bounds.Width - this.ClientRectangle.Width) / 2.0);
int offsetY = (c.Top - this.Top) - (int)Math.Floor((double)(this.Bounds.Height - this.ClientRectangle.Height) / 2.0);
pevent.Graphics.DrawImage(bmp, offsetX, offsetY, c.Width, c.Height);
}
}
如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答??! 點(diǎn)擊進(jìn)入論壇