C#实现透明WinForm控件

来源:互联网 发布:怎样开淘宝网店卖衣服 编辑:程序博客网 时间:2024/05/16 10:26
public partial class tspControl : UserControl    {        public tspControl()        {            InitializeComponent();            SetStyle(ControlStyles.SupportsTransparentBackColor              | ControlStyles.UserPaint              | ControlStyles.AllPaintingInWmPaint              | ControlStyles.Opaque, true);            this.BackColor = Color.Transparent;        }               private Image img;        public Image Image        {            get            {                return img;            }            set            {                img = value;            }        }        protected override void OnLocationChanged(EventArgs e)        {            //base.OnLocationChanged(e);            Visible = false;            Visible = true;        }        protected override CreateParams CreateParams        {            get            {                //return base.CreateParams;                CreateParams cp = base.CreateParams;                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT                 return cp;            }        }        protected override void OnPaint(PaintEventArgs pe)        {            if (img != null)            {                base.OnPaint(pe);                pe.Graphics.DrawImage(img, 0, 0);            }            else            {            }        }    }
0 0
原创粉丝点击