C# Winform实现圆角无锯齿按钮
作者:liaogaobo2008 发布时间:2023-09-16 02:26:29
标签:C#,Winform,按钮
本文实例为大家分享了C# Winform实现圆角无锯齿按钮的具体代码,供大家参考,具体内容如下
发现用Winform做一个圆角按钮遇到麻烦,主要是锯齿问题,后面想了想办法解决问题了。
主要方法是按钮的区域通过Region指定,但按钮需要自己画,否则怎么搞都出现锯齿,网上有朋友提供一个漂亮的方案,可是代码不完整无法使用,我的解决方案现在分享如下:
代码:
public enum ControlState { Hover , Normal, Pressed }
? ? public class RoundButton : Button
? ? {
? ? ? ??
? ? ? ? private int radius;//半径?
? ? ? ? private Color _baseColor = Color.FromArgb(51, 161, 224);//基颜色
? ? ? ? private Color _hoverColor= Color.FromArgb(51, 0, 224);//基颜色
? ? ? ? private Color _normalColor = Color.FromArgb(0, 161, 224);//基颜色
? ? ? ? private Color _pressedColor = Color.FromArgb(51, 161, 0);//基颜色
? ? ? ? //圆形按钮的半径属性
? ? ? ? [CategoryAttribute("布局"), BrowsableAttribute(true), ReadOnlyAttribute(false)]
? ? ? ? public int Radius
? ? ? ? {
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? radius = value;
? ? ? ? ? ? ? ? this.Invalidate();
? ? ? ? ? ? }
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return radius;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? [DefaultValue(typeof(Color), "51, 161, 224")]
? ? ? ? public Color NormalColor
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return this._normalColor;
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this._normalColor = value;
? ? ? ? ? ? ? ? this.Invalidate();
? ? ? ? ? ? }
? ? ? ? }
? ? ? // ?[DefaultValue(typeof(Color), "220, 80, 80")]
? ? ? ? public Color HoverColor {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return this._hoverColor;
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this._hoverColor = value;
? ? ? ? ? ? ? ? this.Invalidate();
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? // ?[DefaultValue(typeof(Color), "251, 161, 0")]
? ? ? ? public Color PressedColor {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return this._pressedColor;
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this._pressedColor = value;
? ? ? ? ? ? ? ? this.Invalidate();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public ControlState ControlState { get; set; }
? ? ? ? protected override void OnMouseEnter(EventArgs e)//鼠标进入时
? ? ? ? {
? ? ? ? ? ? base.OnMouseEnter(e);
? ? ? ? ? ? ControlState = ControlState.Hover;//正常
? ? ? ? }
? ? ? ? protected override void OnMouseLeave(EventArgs e)//鼠标离开
? ? ? ? {
? ? ? ? ? ? base.OnMouseLeave(e);
? ? ? ? ? ? ControlState = ControlState.Normal;//正常
? ? ? ? }
? ? ? ? protected override void OnMouseDown(MouseEventArgs e)//鼠标按下
? ? ? ? {
? ? ? ? ? ? base.OnMouseDown(e);
? ? ? ? ? ? if (e.Button == MouseButtons.Left && e.Clicks == 1)//鼠标左键且点击次数为1
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ControlState = ControlState.Pressed;//按下的状态
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? protected override void OnMouseUp(MouseEventArgs e)//鼠标弹起
? ? ? ? {
? ? ? ? ? ? base.OnMouseUp(e);
? ? ? ? ? ? if (e.Button == MouseButtons.Left && e.Clicks == 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (ClientRectangle.Contains(e.Location))//控件区域包含鼠标的位置
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ControlState = ControlState.Hover;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ControlState = ControlState.Normal;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public RoundButton()
? ? ? ? {
? ? ? ? ? ? Radius = 15;
? ? ? ? ? ? this.FlatStyle = FlatStyle.Flat;
? ? ? ? ? ? this.FlatAppearance.BorderSize = 0;
? ? ? ? ? ? this.ControlState = ControlState.Normal;
? ? ? ? ? ? this.SetStyle(
? ? ? ? ? ? ?ControlStyles.UserPaint | ?//控件自行绘制,而不使用操作系统的绘制
? ? ? ? ? ? ?ControlStyles.AllPaintingInWmPaint | //忽略擦出的消息,减少闪烁。
? ? ? ? ? ? ?ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制,不直接绘制到屏幕上,减少闪烁。
? ? ? ? ? ? ?ControlStyles.ResizeRedraw | //控件大小发生变化时,重绘。 ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ?ControlStyles.SupportsTransparentBackColor, true);//支持透明背景颜色
? ? ? ? }
?
? ? ? ? private Color GetColor(Color colorBase, int a, int r, int g, int b)
? ? ? ? {
? ? ? ? ? ? int a0 = colorBase.A;
? ? ? ? ? ? int r0 = colorBase.R;
? ? ? ? ? ? int g0 = colorBase.G;
? ? ? ? ? ? int b0 = colorBase.B;
? ? ? ? ? ? if (a + a0 > 255) { a = 255; } else { a = Math.Max(a + a0, 0); }
? ? ? ? ? ? if (r + r0 > 255) { r = 255; } else { r = Math.Max(r + r0, 0); }
? ? ? ? ? ? if (g + g0 > 255) { g = 255; } else { g = Math.Max(g + g0, 0); }
? ? ? ? ? ? if (b + b0 > 255) { b = 255; } else { b = Math.Max(b + b0, 0); }
?
? ? ? ? ? ? return Color.FromArgb(a, r, g, b);
? ? ? ? }
?
? ? ? ? //重写OnPaint
? ? ? ? protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
? ? ? ? {
? ? ? ? ? ? base.OnPaint(e);
? ? ? ? ? ? base.OnPaintBackground(e);
? ? ? ? ? ? e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
? ? ? ? ? ? e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
?
? ? ? ? ? ? e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
?
? ? ? ? ? ? Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
? ? ? ? ? ? var path = GetRoundedRectPath(rect, radius);
?
? ? ? ? ? ? this.Region = new Region(path);
?
? ? ? ? ? ? Color baseColor;
? ? ? ? ? ? //Color borderColor;
? ? ? ? ? ? //Color innerBorderColor = this._baseColor;//Color.FromArgb(200, 255, 255, 255); ;
?
? ? ? ? ? ? switch (ControlState)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case ControlState.Hover:
? ? ? ? ? ? ? ? ? ? baseColor = this.HoverColor;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case ControlState.Pressed:
? ? ? ? ? ? ? ? ? ? baseColor = this.PressedColor;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case ControlState.Normal:
? ? ? ? ? ? ? ? ? ? baseColor = this.NormalColor;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? baseColor = this.NormalColor;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
?
? ? ? ? ? ? using (SolidBrush b = new SolidBrush(baseColor))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? e.Graphics.FillPath(b, path);
? ? ? ? ? ? ? ? Font fo = new Font("宋体", 10.5F);
? ? ? ? ? ? ? ? Brush brush = new SolidBrush(this.ForeColor);
? ? ? ? ? ? ? ? StringFormat gs = new StringFormat();
? ? ? ? ? ? ? ? gs.Alignment = StringAlignment.Center; //居中
? ? ? ? ? ? ? ? gs.LineAlignment = StringAlignment.Center;//垂直居中
? ? ? ? ? ? ? ? e.Graphics.DrawString(this.Text, fo, brush, rect, gs);
? ? ? ? ? ? ? ? // ?e.Graphics.DrawPath(p, path);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? 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 OnSizeChanged(EventArgs e)
? ? ? ? {
? ? ? ? ? ? base.OnSizeChanged(e);
? ? ? ? }
?
? ? ? ??
? ? ? ?
? ? }
来源:https://blog.csdn.net/liaogaobo2008/article/details/103649207


猜你喜欢
- 锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) 。这些
- 目录一、常用属性、方法和事件1.常用属性2.常用方法3.常用事件二、应用实例1.实例描述2.属性设置3.事件处理4.实现代码总结一、常用属性
- 说明: 操作系统:deepin20.1一、下载eclipse_2021-03下载jdk-16.0.1下载,选下图所示: 二、安装2
- 一、Java 运行时数据区域友情提示:这部分内容可能大部分同学都有一定的了解了,可以跳过直接进入下一小节哈。Java 虚拟机在执行 Java
- 本文实例为大家分享了Android五子棋游戏的具体代码,供大家参考,具体内容如下1、效果图:2、GobangPanel棋盘面板:public
- 1.使用ASCII码判断您可以使用ASCII码来进行判断字符串中的内容是否为纯数字。步骤如下:先判断字符串是否为空的情况,保证代码运行的稳定
- autoMapping和autoMappingBehavior的区别autoMappingBehaviormybatis核心配置文件中set
- 本文实例为大家分享了Android实现签名涂鸦手写板的具体代码,供大家参考,具体内容如下布局文件<?xml version="
- 背景由于项目是采用java编写的,微信包括微信支付大都是php相关,于是微信支付官方文档对java的支持就不是很友好,在网上找了很多文章,基
- 英文设置加粗可以在xml里面设置: <SPAN style="FONT-SIZE: 18px">androi
- 异步操作C++11为异步操作提供了4个接口std::future : 异步指向某个任务,然后通过future特性去获取任务函数的返回结果。s
- 就把遇到的问题记录一下。写这篇文章时用的TinyMCE编辑器就很强大,但毕竟是第三方的,项目也考虑了这些,如果做些自定义的东西不太方便。 1
- String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要
- 看一段程序String t = "a||b||c||d";String[] temp = t.split("\
- 前言在JDK当中给我们提供的各种并发工具当中,比如ReentrantLock等等工具的内部实现,经常会使用到一个工具,这个工具就是LockS
- 概述在Java环境下创建定时任务有多种方式:使用while循环配合 Thread.sleep(),虽然稍嫌粗陋但也勉强可用使用 Timer和
- 1、通过查找API文档:2、Map.Entry是一个接口,所以不能直接实例化。3、Map.entrySet( )返回的是一个collecti
- Android内部没有控件来直接显示文档,跳转WPS或其他第三方文档App体验性不好,使用腾讯X5内核能很好的解决的这一问题。一、下载腾讯X
- 使用RateLimiter通过AOP方式进行限流1、引入依赖<!-- guava 限流 --><dependency>
- 最近有很多同学,竟然不知道如何使用Intellij IDEA打开Java项目并启动现在来讲一下,希望不要忘记了 1、打开IDEA开机页面 M