C# 圓角button

来源:http://www.cnblogs.com/onegarden/archive/2017/08/15/7367855.html
-Advertisement-
Play Games

因為自帶的button是尖角的不太好看 這裡在網上找的一份代碼改改做個自用的button,畫的操作不局限於button也可以畫其他的 ...


因為自帶的button是尖角的不太好看 這裡在網上找的一份代碼改改做個自用的button,畫的操作不局限於button也可以畫其他的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExerciseUIPrj
{
    public partial class XButton : Button
    {
        enum model
        {
            hover,
            enter,
            press,
            enable
        }

        public Color HoverBackColor { get; set; }
        public Color EnterBackColor { get; set; }
        public Color PressBackColor { get; set; }
        public Color HoverForeColor { get; set; }
        public Color EnterForeColor { get; set; }
        public Color PressForeColor { get; set; }
        
        public int Radius { get; set; }

        model paintmodel = model.hover;
        public XButton()
        {
            InitializeComponent();
            //這些得帶上,不然會有黑邊
            FlatStyle = FlatStyle.Flat;
            FlatAppearance.BorderSize = 0;
            FlatAppearance.BorderColor = Color.FromArgb(0, 0, 0, 0);
            FlatAppearance.MouseDownBackColor = Color.Transparent;
            FlatAppearance.MouseOverBackColor = Color.Transparent;

            SetDefaultColor();


        }
        public void SetDefaultColor()
        {//給個初始值
            HoverBackColor = Color.LightBlue;
            EnterBackColor = Color.Blue;
            PressBackColor = Color.DarkBlue;
            HoverForeColor = Color.White;
            EnterForeColor = Color.White;
            PressForeColor = Color.White;
            Radius = 18;
        }

      

        protected override void OnLayout(LayoutEventArgs levent)
        {
            SetDefaultColor();
            Refresh();
            base.OnLayout(levent);
        }
        protected override void OnParentChanged(EventArgs e)
        {
            Parent.SizeChanged += delegate { Refresh(); };
            base.OnParentChanged(e);
        }

 

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);//這個不能去,而且得放在前面,不然會有黑框之類的莫名其妙的東西
            var colorback = HoverBackColor;
            var colorfore = HoverForeColor;
            switch (paintmodel)
            {
                case model.hover:
                    colorback = HoverBackColor;
                    colorfore = HoverForeColor;
                    break;
                case model.enter:
                    colorback = EnterBackColor;
                    colorfore = EnterForeColor;
                    break;
                case model.press:
                    colorback = PressBackColor;
                    colorfore = PressForeColor;
                    break;
                case model.enable:
                    colorback = Color.LightGray;
                    break;
                default:
                    colorback = HoverBackColor;
                    colorfore = HoverForeColor;
                    break;
            }
            Draw(e.ClipRectangle, e.Graphics, false, colorback);
            DrawText(e.ClipRectangle, e.Graphics, colorfore);
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            paintmodel = model.enter;
            base.OnMouseEnter(e);
          

        }
        protected override void OnMouseLeave(EventArgs e)
        {
            paintmodel = model.hover;
            base.OnMouseLeave(e);
 
        }
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            paintmodel = model.press;
            base.OnMouseDown(mevent);
        }
        protected override void OnEnabledChanged(EventArgs e)
        {
            paintmodel = Enabled ? model.hover : model.enable;
            Invalidate();//false 轉換為true的時候不會刷新 這裡強制刷新下
            base.OnEnabledChanged(e);

        }
        void Draw(Rectangle rectangle, Graphics g, bool cusp, Color begin_color, Color? end_colorex = null)
        {
            Color end_color = end_colorex == null ? begin_color : (Color)end_colorex;
            int span = 2;
            //抗鋸齒
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //漸變填充
            LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rectangle, begin_color, end_color, LinearGradientMode.Vertical);
            //畫尖角
            if (cusp)
            {
                span = 10;
                PointF p1 = new PointF(rectangle.Width - 12, rectangle.Y + 10);
                PointF p2 = new PointF(rectangle.Width - 12, rectangle.Y + 30);
                PointF p3 = new PointF(rectangle.Width, rectangle.Y + 20);
                PointF[] ptsArray = { p1, p2, p3 };
                g.FillPolygon(myLinearGradientBrush, ptsArray);
            }
            //填充
            g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, Radius));
    
        }
        void DrawText(Rectangle rectangle, Graphics g, Color color)
        {
            SolidBrush sbr = new SolidBrush(color);
            var rect = new RectangleF();
            switch (TextAlign)
            {
                case ContentAlignment.MiddleCenter:
                    rect = getTextRec(rectangle, g);
                    break;
                default:
                    rect = getTextRec(rectangle, g);
                    break;
            }
            g.DrawString(Text, Font, sbr, rect);
        }
        RectangleF getTextRec(Rectangle rectangle,Graphics g)
        {
            var rect = new RectangleF();
            var size = g.MeasureString(Text, Font);
            if (size.Width > rectangle.Width || size.Height > rectangle.Height)
            {
                rect = rectangle;
            }
            else
            {
                rect.Size = size;
                rect.Location = new PointF(rectangle.X + (rectangle.Width - size.Width) / 2, rectangle.Y + (rectangle.Height - size.Height) / 2);
            }
            return rect;
        }
        GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
        {
            //四邊圓角
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(x, y, radius, radius, 180, 90);
            gp.AddArc(width - radius, y, radius, radius, 270, 90);
            gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
            gp.AddArc(x, height - radius, radius, radius, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 泛型集合lisit<>優點1.性能高 對值類型使用非泛型集合類,在把值類型轉換為引用類型,和把引用類型轉換為值類型時,需要進行裝箱和拆箱的操作。裝箱和拆箱的操作很容易實現,但是性能損失較大, 假如使用泛型,就可以避免裝箱和拆箱操作。 此為集合。 ArrayList list=new ArrayLis ...
  • 經歷了很久,.net core 2.0 終於發佈了! 之前一直用的core 1.1,升級了2.0後發現認證的機制(Auth)發生了比較大的變化,在1.1中認證配置是在Configure中完成,而在2.0中,認證配置則是在ConfigureServices中完成,剛好對調了一下。 話不多說,直接看代碼 ...
  • 今天所有開發環境已經遷移到mac OS下的Visual Studio Code + 命令行編譯發佈,而運行伺服器是CentOS7,和windows沒什麼關聯了。 只要你Relese編譯併在本地有一個與伺服器相同的運行環境中運行成功了,遷移到真實伺服器不會有什麼難度。 下麵是遷移到 2.0 版本之後遇 ...
  • asp.net(c#)中String.Empty、NULL、"" 三者到底有啥區別和聯繫? ...
  • 本指南演示了以下 Azure .NET API 的用法,包括設置認證、創建並使用 Azure 存儲、創建並使用 Azure SQL 資料庫、部署虛擬機、從 GitHub 部署 Azure Web 應用。在本教程中完成的所有操作均符合1元試用條件。 開始之前 如果您還沒有 Azure 賬戶,可以申請1 ...
  • 回到目錄 對於在Linq To Entity里使用日期函數需要DbFunctions里的擴展方法,而不能使用.net里的日期函數,因為linq的代碼會被翻譯成SQL發到資料庫端,如你的.net方法對於資料庫是不知道的,所以需要使用DbFunctions里的函數,它是為sqlserver設計的,而如果 ...
  • 目前使用這個框架,搜不到中文資料。 只有英文wiki,翻譯學習摘錄一下。 大部分是機翻的,改了一些地方, 因為博主也是低水平的,標紅的是瞎理解的。 可能會有錯誤的地方,如果有園友發現可以指出。 wiki地址:http://github.com/schotime/NPoco/wiki 第一個查詢: 註 ...
  • 假如現在有一個按鈕 <div class="inp_btn voice_btn active" id="record"> 按住 說話 </div> 下麵就是調用微信jssdk的方法 var recorder; var btnRecord = $('#record'); var startTime = ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...