Winform 圆角

//Paint circle.
        public void SetWindowRegion()
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 22, this.Width, this.Height - 22);//this.Left-10,this.Top-10,this.Width-10,this.Height-10);                
            FormPath = GetRoundedRectPath(rect, 30);
            this.Region = new Region(FormPath);
        }

        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            //   左上角  
            path.AddArc(arcRect, 180, 90);
            //   右上角  
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            //   右下角  
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            //   左下角  
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }

        protected override void OnResize(System.EventArgs e)
        {
            this.Region = null;
            SetWindowRegion();
        }

### 实现 WinForms 应用程序中的圆角效果 为了在 Windows Forms (WinForms) 应用程序中创建具有圆角的窗口或控件,可以采用多种方法来达到这一目的。一种常见的方式是通过自定义绘制技术以及使用 `GraphicsPath` 类。 #### 使用 GraphicsPath 创建圆角矩形 可以通过重写窗体或控件的 `OnPaint` 方法,在其中利用 `GraphicsPath` 来构建带有弧度边缘的路径对象,并将其应用到当前绘图表面: ```csharp using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; public class RoundedForm : Form { protected override void OnPaint(PaintEventArgs e){ base.OnPaint(e); int cornerRadius = 20; // 设置圆角度数 using(GraphicsPath path = new GraphicsPath()){ Rectangle rect = this.ClientRectangle; // 定义四个角落的位置和大小 rect.Inflate(-cornerRadius, -cornerRadius); // 添加左上角弧线 path.AddArc(rect.Left, rect.Top, cornerRadius * 2, cornerRadius * 2, 180, 90); // 右上角 path.AddArc(rect.Right - cornerRadius * 2, rect.Top, cornerRadius * 2, cornerRadius * 2, 270, 90); // 右下角 path.AddArc(rect.Right - cornerRadius * 2, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90); // 左下角 path.AddArc(rect.Left, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90); // 关闭图形路径 path.CloseFigure(); Region region = new Region(path); this.Region = region; } } } ``` 这段代码展示了如何创建一个拥有四角均为圆滑过渡的新窗体实例[^1]。对于想要单独调整某个特定控件的情况,则可以在该控件类内部执行相似的操作,只需确保替换掉上述例子中的 `this` 关键字为具体的控件名称即可。 另外值得注意的是,如果追求更高级别的视觉体验或者功能需求较为复杂时,考虑第三方库可能是更好的选择。例如 DevExpress 提供了一系列强大的 UI 组件支持,虽然官方提到圆形面板存在一定的局限性[^2],但对于大多数场景来说仍然能够满足开发人员的需求并简化工作流程。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值