• C# 开发圆角控件(窗体)


    最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能。其中方法三既适应于控件,也适应于窗体。

    先上传效果图:

    方法一:

    增加命名空间:using System.Drawing.Drawing2D;  
    添加方法如下:当然各角的点可根据需要确定. 

     1 private void Type(Control sender, int p_1, double p_2)
     2         {
     3             GraphicsPath oPath = new GraphicsPath();
     4             oPath.AddClosedCurve(
     5                 new Point[] {
     6             new Point(0, sender.Height / p_1),
     7             new Point(sender.Width / p_1, 0), 
     8             new Point(sender.Width - sender.Width / p_1, 0), 
     9             new Point(sender.Width, sender.Height / p_1),
    10             new Point(sender.Width, sender.Height - sender.Height / p_1), 
    11             new Point(sender.Width - sender.Width / p_1, sender.Height), 
    12             new Point(sender.Width / p_1, sender.Height),
    13             new Point(0, sender.Height - sender.Height / p_1) },
    14 
    15                 (float)p_2);
    16 
    17             sender.Region = new Region(oPath);
    18         }

    在窗体的paint和resize事件中增加:Type(this,20,0.1);  
    参数20和0.1也可以根据自己的需要调整到最佳效

    方法二:

     1 public void SetWindowRegion()
     2         {
     3 
     4             System.Drawing.Drawing2D.GraphicsPath FormPath;
     5 
     6             FormPath = new System.Drawing.Drawing2D.GraphicsPath();
     7 
     8             Rectangle rect = new Rectangle(0, 22, this.Width, this.Height - 22);//this.Left-10,this.Top-10,this.Width-10,this.Height-10);                 
     9 
    10             FormPath = GetRoundedRectPath(rect, 30);
    11 
    12             this.Region = new Region(FormPath);
    13 
    14         }
    15 
    16         private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
    17         {
    18 
    19             int diameter = radius;
    20 
    21             Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
    22 
    23             GraphicsPath path = new GraphicsPath();
    24 
    25             //   左上角   
    26 
    27             path.AddArc(arcRect, 180, 90);
    28 
    29             //   右上角   
    30 
    31             arcRect.X = rect.Right - diameter;
    32 
    33             path.AddArc(arcRect, 270, 90);
    34 
    35             //   右下角   
    36 
    37             arcRect.Y = rect.Bottom - diameter;
    38 
    39             path.AddArc(arcRect, 0, 90);
    40 
    41             //   左下角   
    42 
    43             arcRect.X = rect.Left;
    44 
    45             path.AddArc(arcRect, 90, 90);
    46 
    47             path.CloseFigure();
    48 
    49             return path;
    50 
    51         }

    在窗体的resize事件中增加:SetWindowRegion();  

    方法三:通过Window系统API行数,修改控件和窗体为椭圆形状。代码如下所示:

     1 [System.Runtime.InteropServices.DllImport("gdi32")]
     2         private static extern IntPtr BeginPath(IntPtr hdc);
     3         [System.Runtime.InteropServices.DllImport("gdi32")]
     4         private static extern int SetBkMode(IntPtr hdc, int nBkMode);
     5         const int TRANSPARENT = 1;
     6         [System.Runtime.InteropServices.DllImport("gdi32")]
     7         private static extern IntPtr EndPath(IntPtr hdc);
     8         [System.Runtime.InteropServices.DllImport("gdi32")]
     9         private static extern IntPtr PathToRegion(IntPtr hdc);
    10         [System.Runtime.InteropServices.DllImport("gdi32")]
    11         private static extern int Ellipse(IntPtr hdc, int x1, int y1, int x2, int y2);
    12         [System.Runtime.InteropServices.DllImport("user32")]
    13         private static extern IntPtr SetWindowRgn(IntPtr hwnd, IntPtr hRgn, bool bRedraw);
    14         [System.Runtime.InteropServices.DllImport("user32")]
    15         private static extern IntPtr GetDC(IntPtr hwnd);

     1 protected override void OnPaint(PaintEventArgs e)
     2         {
     3             base.OnPaint(e);
     4 
     5             IntPtr dc;
     6             IntPtr region;
     7 
     8             dc = GetDC(this.Handle);
     9             BeginPath(dc);
    10             SetBkMode(dc, TRANSPARENT);
    11             Ellipse(dc, 0, 0, this.Width - 3, this.Height - 2);
    12             EndPath(dc);
    13             region = PathToRegion(dc);
    14             SetWindowRgn(this.Handle, region, false);
    15         }
  • 相关阅读:
    JavaScript函数中的this四种绑定形式
    jQuery的html()、text()和val()的使用和区别
    iframe-父子-兄弟页面相互传值(jq和js两种方法)
    Spring Boot 嵌入式 Tomcat 文件上传、url 映射虚拟路径
    SpringMVC上传图片
    <iframe>和<frame>标签属性详解
    Mybatis 事物回滚最简单的操作方式
    SpringBoot配置log4j
    springboot整合redis(集群)
    Maven setting.xml简易配置
  • 原文地址:https://www.cnblogs.com/Peter-Luo/p/3571859.html
Copyright © 2020-2023  润新知