C# 透明背景Panel, 透明图像, PitureBox透明效果

来源:互联网 发布:mac怎样删除文件 编辑:程序博客网 时间:2024/06/10 14:19


1、自定义透明 背景Panel控件:在项目中添加类TransparentPanel.cs


using System.Windows.Forms;using System.Drawing;
public class TransparentPanel : Control    {        public TransparentPanel(){}        protected override void OnPaintBackground(PaintEventArgs e)        {            //不进行背景的绘制        }        protected override CreateParams CreateParams        {            get            {                CreateParams cp = base.CreateParams;                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT                return cp;            }        }        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)        {            //绘制panel的背景图像            if(BackgroundImage!= null) e.Graphics.DrawImage(this.BackgroundImage, new Point(0, 0));        }        ////为控件添加自定义属性值num1        //private int num1 = 1;        //[Bindable(true), Category("自定义属性栏"), DefaultValue(1), Description("此处为自定义属性Attr1的说明信息!")]        //public int Attr1        //{        //    get { return num1; }        //    set { this.Invalidate(); }        //}    }



2、F5编译运行一次 后,可在工具栏找到控件TransparentPanel。

之后添加控件到窗体Form, 设置其BackgroundImage属性,为带有透明度信息的*.png图像即可看到示意图中的透明效果。


0 0
原创粉丝点击