• winform绘制圆形控件抗锯齿


    效果

    步骤

    1.vs2019新建窗体项目

    2.右键单击项目名称,选择添加->用户控件

    3.命名为CircleControl,选择添加

    4.右键单击我们的自定义控件,选择查看代码

    5.添加以下属性

    1. ///
    2. /// 中心颜色
    3. ///
    4. private Color centerColor = Color.White;
    5. ///
    6. /// 中心颜色
    7. ///
    8. public Color CenterColor
    9. {
    10. set
    11. {
    12. centerColor = value;
    13. }
    14. get
    15. {
    16. return centerColor;
    17. }
    18. }
    19. ///
    20. /// 边缘颜色
    21. ///
    22. private Color edgeColor = Color.Yellow;
    23. ///
    24. /// 边缘颜色
    25. ///
    26. public Color EdgeColor
    27. {
    28. get
    29. {
    30. return edgeColor;
    31. }
    32. set
    33. {
    34. edgeColor = value;
    35. }
    36. }
    37. ///
    38. /// 中心点
    39. ///
    40. private PointF centerPoint = new PointF(3.5f, 3.5f);
    41. ///
    42. /// 中心点横坐标
    43. ///
    44. public float CenterX
    45. {
    46. set
    47. {
    48. centerPoint.X = value;
    49. }
    50. get
    51. {
    52. return centerPoint.X;
    53. }
    54. }
    55. ///
    56. /// 中心点纵坐标
    57. ///
    58. public float CenterY
    59. {
    60. set
    61. {
    62. centerPoint.Y = value;
    63. }
    64. get
    65. {
    66. return centerPoint.Y;
    67. }
    68. }

     6.重写OnLayout事件

    1. ///
    2. /// 布局更改事件
    3. ///
    4. ///
    5. protected override void OnLayout(LayoutEventArgs e)
    6. {
    7. base.OnLayout(e);
    8. GraphicsPath path = new GraphicsPath();
    9. path.AddEllipse(new RectangleF(0f, 0f, ClientSize.Width * 1.0f, ClientSize.Height * 1.0f));
    10. PathGradientBrush brush = new PathGradientBrush(path);
    11. brush.CenterColor = centerColor;
    12. brush.SurroundColors = new Color[] { edgeColor };
    13. brush.WrapMode = WrapMode.TileFlipXY;
    14. brush.CenterPoint = centerPoint;
    15. Bitmap bitmap = new Bitmap(ClientSize.Width, ClientSize.Height);
    16. Graphics g = Graphics.FromImage(bitmap);
    17. g.SmoothingMode = SmoothingMode.AntiAlias;
    18. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    19. g.CompositingQuality = CompositingQuality.HighQuality;
    20. g.FillPath(brush, path);
    21. g.DrawArc(new Pen(BackColor, 4), new RectangleF(0, 0, ClientSize.Width, ClientSize.Height), 0, 360);
    22. BackgroundImage = bitmap;
    23. }

    7.构造方法中开启双缓冲

    1. ///
    2. /// 构造函数
    3. ///
    4. public CircleControl()
    5. {
    6. DoubleBuffered = true;
    7. InitializeComponent();
    8. }

    8.重新生成解决方案

    9.回到Form1窗体,在工具箱里可以看到我们的自定义控件已经加载完毕,将其拖到Form1窗体中,并按自己风格修改圆形尺寸和中心点位置,渐变颜色等,即可运行。

     

    技术要点

    避免闪屏【开启双缓冲】

    DoubleBuffered = true;

    避免PathGradientBrush的锯齿效果,在其外围画一个环【环的厚度刚好把锯齿填完就好】,圈的锯齿效果可以通过修改Graphics对象三个属性解决

    1. Graphics g = Graphics.FromImage(bitmap);
    2. g.SmoothingMode = SmoothingMode.AntiAlias;
    3. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    4. g.CompositingQuality = CompositingQuality.HighQuality;

    中心点大约在宽高的35%~40%之间看起来效果比较自然【个人认为】 

    源文件代码

    请登录码云进行下载

    圆形控件: C# winform实现圆形控件 

  • 相关阅读:
    【CSS】H7_浮动
    每日一题:LeetCode-589.N叉树的前序遍历
    excel文件运行报错(xx.xlsx)不是有效的win32应用程序
    数据分析的基本要求:学习数据分析需要掌握哪些能力
    商城小程序开发|二级分销裂变商城小程序怎么赚钱?
    2023最新版Android逆向教程——第3天:ADB原理及其常用命令
    【Linux修炼】开篇
    【Unity好插件之PlayMaker系列一上半部分】如何只用一个插件和一个脚本完成制作一个简易的游戏
    漏洞发现-web应用发现探针类型利用(43)
    java设计模式之组合设计模式
  • 原文地址:https://blog.csdn.net/qq_36694133/article/details/126776712