網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2022-04-27 jQuery實現移動端懸浮拖動效果_jquery
- 2022-12-21 使用RedisAtomicInteger計數出現少計問題及解決_Redis
- 2022-11-15 簡單實現Android應用的啟動頁_Android
- 2022-09-25 注解@Autowired如何自動裝配
- 2022-11-07 Flink?側流輸出源碼示例解析_服務器其它
- 2022-12-01 Apache?Doris?Join?優化原理詳解_Linux
- 2023-07-08 CMake Error at CMakeLists.txt:4 (find_package): By
- 2023-01-26 Redis實戰之Jedis使用技巧詳解_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支