按钮去边框

border:none;

 

### 如何除 WinForms 按钮边框 为了除 WinForms 中按钮边框,可以通过继承 `Button` 类并重写其绘制逻辑来实现这一目标。具体来说,可以创建一个新的类,该类从 `Button` 继承,并覆盖 `OnPaint` 方法以自定义绘图行为。 以下是具体的实现方法: #### 创建无边框按钮类 ```csharp using System; using System.Drawing; using System.Windows.Forms; public class BorderlessButton : Button { protected override void OnPaint(PaintEventArgs pevent) { using (var brush = new SolidBrush(this.ForeColor)) { var formatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; TextRenderer.DrawText(pevent.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, formatFlags); } } public BorderlessButton() { SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true); } } ``` 这段代码实现了 `BorderlessButton` 类,它移除了默认的边框样式,并使用 `TextRenderer.DrawText` 来居中文本[^1]。 #### 使用无边框按钮 要在窗体上使用此自定义按钮,只需像下面这样实例化对象即可: ```csharp public partial class MainForm : Form { public MainForm() { InitializeComponent(); var btn = new BorderlessButton(); btn.Text = "Click Me"; btn.Size = new Size(80, 30); btn.Location = new Point(50, 50); Controls.Add(btn); } } ``` 上述代码展示了如何在表单中添加一个不带边框按钮,并设置了位置和大小属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值