自定义一个带边框 透明的Panel

创建透明带边框的自定义Panel
本文介绍如何创建一个自定义的Panel控件,该控件支持透明背景并在Paint事件中绘制边框。通过继承Panel,设置ControlStyles并调整构造函数,实现了背景透明。在Paint事件中使用ControlPaint.DrawBorder方法画出蓝色边框,同时提供了测试代码展示如何在PictureBox上使用此自定义Panel。

1.首先添加一个类库项目,项目里的类继承于Panel控件。

2.构造函数里添加

public MyPanel()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;
        }

3.使用时 在Paint事件里添加

 private void myPanel1_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, myPanel1.ClientRectangle, Color.Blue, 3,
                ButtonBorderStyle.Solid, Color.Blue, 3,
                ButtonBorderStyle.Solid, Color.Blue, 3,
                ButtonBorderStyle.Solid, Color.Blue, 3,
                ButtonBorderStyle.Solid);
        }

测试代码:

Image img = Image.FromFile(@"C:/Documents and Settings/All Users/Documents/My Pictures/示例图片/Blue hills.jpg");
this.pictureBox1.Image = img;
this.myPanel1.Parent = this.pictureBox1;

OK,到这里.这个扩展的控件就已经完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值