五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > C#/VB.NET中如何將LABEL(標(biāo)簽控件)背景設(shè)置為透明

C#/VB.NET中如何將LABEL(標(biāo)簽控件)背景設(shè)置為透明

文章來(lái)源:365jz.com     點(diǎn)擊數(shù):7148    更新時(shí)間:2018-07-23 12:43   參與評(píng)論

有幾種情況:

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)入論壇

發(fā)表評(píng)論 (7148人查看0條評(píng)論)
請(qǐng)自覺(jué)遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評(píng)論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)