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 { } } }
C#实现透明WinForm控件
最新推荐文章于 2022-11-25 18:16:16 发布
本文介绍了一个使用 C# 实现的带有透明背景的自定义控件。该控件通过设置样式支持透明背景,并覆盖了 OnPaint 方法以绘制背景图片。此外,还修改了 CreateParams 和 OnLocationChanged 方法以增强控件的特性。
1095

被折叠的 条评论
为什么被折叠?



