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

本文介绍如何在项目中自定义一个透明背景Panel控件,并通过实例演示如何将其应用于窗体中,设置带有透明度信息的背景图片以达到预期的透明效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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图像即可看到示意图中的透明效果。


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值