Winform中實現自定義水晶按鈕控制項(附代碼下載)

来源:https://www.cnblogs.com/badaoliumangqizhi/archive/2020/03/11/12462781.html
-Advertisement-
Play Games

場景 效果 註: 博客主頁: https://blog.csdn.net/badao_liumang_qizhi 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 新建一個用戶控制項TransparencyButton,修改其代碼如下 using System; using S ...


場景

效果

 

 

註:

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關註公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載。

實現

新建一個用戶控制項TransparencyButton,修改其代碼如下

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

namespace 自定義水晶按鈕控制項
{
    public partial class TransparencyButton : UserControl
    {
        public TransparencyButton()
        {
            InitializeComponent();
        }

        #region 公共變數
        public static SmoothingMode sm;
        public static bool pub_ButtonClick = true;//判斷按鈕是否按下(false為按下)
        public static int pub_Degree = 0;//記錄四角弧度的大小範圍
        public static int pub_RGB_r0 = 0x130;//按鈕背景的R色值
        public static int pub_RGB_r1 = 0x99;//按鈕其它顏色的R色值
        #endregion

        #region 添加屬性
        private string BNText = "";
        [Browsable(true), Category("透明按鈕的屬性設置"), Description("設置顯示的文本")]   //在“屬性”視窗中顯示NText屬性
        public string NText
        {
            get { return BNText; }
            set
            {
                BNText = value;
                if (BNText.Length > 0)
                    Invalidate();
            }
        }

        private int BDegree = 1;
        [Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕四個角的弧度")]   //在“屬性”視窗中顯示Degree屬性
        public int Degree
        {
            get { return BDegree; }
            set
            {
                BDegree = value;
                if (this.Width >= this.Height)
                    pub_Degree = (int)(this.Height / 2);
                else
                    pub_Degree = (int)(this.Width / 2);
                if (BDegree <= 0)
                    BDegree = 1;
                if (BDegree > pub_Degree)
                    BDegree = pub_Degree;
                if (BDegree > 0)
                    Invalidate();
            }
        }

        private Color DShineColor = Color.Black;
        [Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕的光澤度顏色")]   //在“屬性”視窗中顯示ShineColor屬性
        public Color ShineColor
        {
            get { return DShineColor; }
            set
            {
                DShineColor = value;
                Invalidate();
            }
        }

        private Color DUndersideShine = Color.LightGray;
        [Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕的下部的光澤度")]   //在“屬性”視窗中顯示UndersideShine屬性
        public Color UndersideShine
        {
            get { return DUndersideShine; }
            set
            {
                DUndersideShine = value;
                Invalidate();
            }
        }

        private int DCTransparence = 0;
        [Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕的透明度數")]   //在“屬性”視窗中顯示CTransparence屬性
        public int CTransparence
        {
            get { return DCTransparence; }
            set
            {
                DCTransparence = value;
                if (DCTransparence > 20)
                    DCTransparence = 20;
                if (DCTransparence < 0)
                    DCTransparence = 0;
                if (DCTransparence >= 0)
                    Invalidate();
            }
        }

        private int DCFontDeepness = 1;
        [Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕文本的深度")]   //在“屬性”視窗中顯示CFontDeepness屬性
        public int CFontDeepness
        {
            get { return DCFontDeepness; }
            set
            {
                DCFontDeepness = value;
                if (DCFontDeepness > 20)
                    DCFontDeepness = 20;
                if (DCFontDeepness < 0)
                    DCFontDeepness = 0;
                if (DCFontDeepness >= 0)
                    Invalidate();
            }
        }

        #endregion

        #region 事件
        private void TransparencyButton_Paint(object sender, PaintEventArgs e)
        {
            this.BackColor = Color.Transparent;//使當前控制項透明
            sm = e.Graphics.SmoothingMode;//設置呈現質量
            Color shineColor = Color.Black;
            Rectangle rect2 = new Rectangle(0, 0, this.Width, this.Height);//設置繪製按鈕的矩形區域
            Rectangle rect1 = new Rectangle(0, this.Height / 2, this.Width, this.Height / 2);//設置繪製按鈕下半部的矩形區域
            if (this.CTransparence == 0)//如果按鈕的透明度為0
            {
                CobOblongDown(rect2, e.Graphics);//繪製按扭的背景
                CobOblong(rect2, e.Graphics, this.ShineColor);//繪製按扭的背景
            }
            else
            {
                if (this.CTransparence > 0)//如果按鈕的透明度不為0
                {
                    CobOblongDown(rect2, e.Graphics);//繪製按扭的背景
                    for (int i = 0; i < CTransparence; i++)
                    {
                        CobOblong(rect2, e.Graphics, this.ShineColor);//繪製按扭的背景顏色
                    }
                }
            }

            int tem_n = (int)(this.CTransparence / 3);//獲取一個值,用於設置下半部按鈕的顏色深度
            if (tem_n == 0)//如果為0
                CobAjar(rect1, e.Graphics, this.ShineColor);//繪製按扭的下半部背景
            else
            {
                if (tem_n > 0)//如果不為0
                {
                    for (int i = 0; i < tem_n; i++)//加深下部按鈕的顏色
                    {
                        CobAjar(rect1, e.Graphics, this.ShineColor);//繪製按扭的下半部背景顏色
                    }
                }
            }
            CobOblong(rect2, e.Graphics, this.UndersideShine);//設置下半部按鈕的光澤度
            if (pub_ButtonClick == false)//判斷按鈕是否按下(false為按下)
            {
                CobOblongDown(rect2, e.Graphics);//繪製按扭的背景
            }
            if (this.NText.Length > 0)//如果Text屬性中有值
                ProtractText(e.Graphics);//繪製透明按鈕的文本信息
        }

        private void TransparencyButton_SizeChanged(object sender, EventArgs e)
        {
            Invalidate();//對控制項進行重繪
        }

        private void TransparencyButton_MouseDown(object sender, MouseEventArgs e)
        {
            pub_ButtonClick = false;//按下按鈕
            Invalidate();//對控制項進行重繪
        }

        private void TransparencyButton_MouseUp(object sender, MouseEventArgs e)
        {
            pub_ButtonClick = true;//鬆開按鈕
            Invalidate();//對控制項進行重繪
        }
        #endregion

        #region 自定義方法
        /// <summary>
        /// 繪製透明按扭的文本
        /// </summary>
        /// <param g="Graphics">封裝一個繪圖的類對象</param>
        private void ProtractText(Graphics g)
        {
            Graphics TitG = this.CreateGraphics();//創建Graphics類對象
            string TitS = this.NText;//獲取圖表標題的名稱
            SizeF TitSize = TitG.MeasureString(TitS, this.Font);//將繪製的字元串進行格式化
            float TitWidth = TitSize.Width;//獲取字元串的寬度
            float TitHeight = TitSize.Height;//獲取字元串的高度
            float TitX = 0;//標題的橫向坐標
            float TitY = 0;//標題的縱向坐標
            if (this.Height > TitHeight)//如果按鈕的高度大於文本的高度
                TitY = (this.Height - TitHeight) / 2;//使文本水平方向局中
            else
                TitY = this.BDegree;//文本置頂
            if (this.Width > TitWidth)//如果按鈕的寬度大於文本的寬度
                TitX = (this.Width - TitWidth) / 2;//使文本水平局中
            else
                TitX = this.BDegree;//文本置左
            //設置文本的繪製區域
            Rectangle rect = new Rectangle((int)Math.Floor(TitX), (int)Math.Floor(TitY), (int)Math.Ceiling(TitWidth), (int)Math.Ceiling(TitHeight));
            int opacity = pub_RGB_r1;//設置R色值
            opacity = (int)(.4f * opacity + .5f);//設置漸變值
            for (int i = 0; i < DCFontDeepness; i++)//設置文本的深度
            {
                //設置文本的漸變顏色
                using (LinearGradientBrush br = new LinearGradientBrush(rect, Color.FromArgb(opacity, this.ForeColor), Color.FromArgb(opacity, this.ForeColor), LinearGradientMode.Vertical))
                {
                    g.DrawString(TitS, this.Font, br, new PointF(TitX, TitY));//繪製帶有漸變效果的文本
                }
            }
        }

        /// <summary>
        /// 繪製透明按扭的背景色
        /// </summary>
        /// <param rect="Rectangle">繪製按鈕的區域</param>
        /// <param g="Graphics">封裝一個繪圖的類對象</param>
        /// <param fillColor="Color">填充的顏色</param>
        private void CobOblong(Rectangle rect, Graphics g, Color fillColor)
        {
            using (GraphicsPath bh = CreateCobOblong(rect, this.BDegree))//繪製一個圓角矩形
            {
                int opacity = pub_RGB_r0;//設置按鈕的R色值
                opacity = (int)(.4f * opacity + .5f);//設置漸變的變化值
                //設置按鈕的漸變顏色
                using (LinearGradientBrush br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, fillColor), Color.FromArgb(opacity, fillColor), LinearGradientMode.Vertical))
                {
                    g.FillPath(br, bh);//填充按鈕背景
                }
                g.SmoothingMode = sm;//設置呈現的質量
            }
        }

        /// <summary>
        /// 繪製透明按扭的下半部背景色
        /// </summary>
        /// <param rect="Rectangle">繪製按鈕的下半部區域</param>
        /// <param g="Graphics">封裝一個繪圖的類對象</param>
        /// <param fillColor="Color">填充的顏色</param>
        private void CobAjar(Rectangle rect, Graphics g, Color fillColor)
        {
            using (GraphicsPath bh = CreateCobAjar(rect, this.BDegree))
            {
                int opacity = pub_RGB_r1;
                opacity = (int)(.4f * opacity + .5f);
                using (LinearGradientBrush br = new LinearGradientBrush(rect, Color.FromArgb(opacity, fillColor), Color.FromArgb(pub_RGB_r1 / 5, fillColor), LinearGradientMode.Vertical))
                {
                    g.FillPath(br, bh);//填充按鈕背景
                }
                g.SmoothingMode = sm;//設置呈現的質量
            }
        }

        /// <summary>
        /// 繪製透明按扭按下時的效果
        /// </summary>
        /// <param rect="Rectangle">繪製按鈕的區域</param>
        /// <param g="Graphics">封裝一個繪圖的類對象</param>
        private void CobOblongDown(Rectangle rect, Graphics g)
        {
            using (GraphicsPath bh = CreateCobOblong(rect, this.BDegree))//按鈕的圓角繪製
            {
                int opacity = pub_RGB_r1;//設置按鈕的R色值
                Color tem_Color = Color.Black;//設置按鈕的背景顏色為黑色
                if (pub_ButtonClick == true)//如果按鈕沒有按下
                {
                    opacity = pub_RGB_r0;//設置按鈕的R色值
                    tem_Color = Color.White;//設置按鈕的背景顏色為白色
                }
                opacity = (int)(.4f * opacity + .5f);//設置漸變的變化值
                //設置按鈕的漸變顏色
                using (LinearGradientBrush br = new LinearGradientBrush(rect, Color.FromArgb(opacity + 20, tem_Color), Color.FromArgb(opacity, tem_Color), LinearGradientMode.Vertical))
                {
                    g.FillPath(br, bh);//填充按鈕背景
                }
                g.SmoothingMode = sm;//設置呈現的質量
            }
        }

        /// <summary>
        /// 按鈕的圓角繪製
        /// </summary>
        /// <param rect="Rectangle">繪製按鈕的區域</param>
        /// <param radius="int">圓角的度數</param>
        private static GraphicsPath CreateCobOblong(Rectangle rectangle, int radius)
        {
            GraphicsPath path = new GraphicsPath();//實例化GraphicsPath類
            int l = rectangle.Left;//獲取矩形左上角的X坐標
            int t = rectangle.Top;//獲取矩形左上角的Y坐標
            int w = rectangle.Width;//獲取矩形的寬度
            int h = rectangle.Height;//獲取矩形的高度
            path.AddArc(l, t, 2 * radius, 2 * radius, 180, 90);//在矩形的左上角繪製圓角
            path.AddLine(l + radius, t, l + w - radius, t);//繪製左上角圓角與右上角之間的線段
            path.AddArc(l + w - 2 * radius, t, 2 * radius, 2 * radius, 270, 90);//繪製右上角的圓角
            path.AddLine(l + w, t + radius, l + w, t + h - radius);//繪製左上角、右上角和右下角所形成的三角形
            path.AddArc(l + w - 2 * radius, t + h - 2 * radius, 2 * radius, 2 * radius, 0, 90);//繪製右下角圓角
            path.AddLine(l + radius, t + h, l + w - radius, t + h);//繪製右下角圓角與左上角圓之間的線段
            path.AddArc(l, t + h - 2 * radius, 2 * radius, 2 * radius, 90, 90);//繪製左下角的圓角
            path.AddLine(l, t + radius, l, t + h - radius);//繪製左上角、左下角和右下角之間的三角形
            return path;
        }

        /// <summary>
        /// 按鈕的下半個圓角繪製
        /// </summary>
        /// <param rect="Rectangle">繪製下半部按鈕的區域</param>
        /// <param radius="int">圓角的度數</param>
        private static GraphicsPath CreateCobAjar(Rectangle rectangle, int radius)
        {
            GraphicsPath path = new GraphicsPath();
            int l = rectangle.Left;//獲取矩形左上角的X坐標
            int t = rectangle.Top;//獲取矩形左上角的Y坐標
            int w = rectangle.Width;//獲取矩形的寬度
            int h = rectangle.Height;//獲取矩形的高度
            path.AddArc(l, t, 2 * radius, 2 * radius, 0, 0);//繪製左上角的點
            path.AddLine(l, t, l + w, t);//繪製左上角與右上角之間的線段
            path.AddArc(l + w, t, 2 * radius, 2 * radius, 0, 0);//繪製右上角的點
            path.AddLine(l + w, t + radius, l + w, t + h - radius);//繪製左上角、右上角和右下角所形成的三角形
            path.AddArc(l + w - 2 * radius, t + h - 2 * radius, 2 * radius, 2 * radius, 0, 90);//繪製右下角圓角
            path.AddLine(l + radius, t + h, l + w - radius, t + h);//繪製右下角圓角與左上角圓之間的線段
            path.AddArc(l, t + h - 2 * radius, 2 * radius, 2 * radius, 90, 90);//繪製左下角的圓角
            path.AddLine(l, t + radius, l, t + h - radius);//繪製左上角、左下角和右下角之間的三角形
            return path;
        }
        #endregion
    }
}

 

然後新建一個窗體Form1然後拖拽一個新建的自定義水晶控制項

 

 

代碼下載

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12241085


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

-Advertisement-
Play Games
更多相關文章
  • 一、列表 1、作用 列表,是用於按位置存放多個值的數據類型。 即專門用來記錄多個同種屬性的值(比如同一個班級多個學生的姓名、同一個人的多個愛好等),並且存取都十分方便。 2、定義 3、類型轉換 —但凡能夠被for迴圈遍歷的類型都可以當做參數傳給list()轉成列表 4、使用 4.1 取值與嵌套 4. ...
  • Glide是Go語言的一種包管理工具,官網:, Github地址: 。當然從Go 1.13開始,模塊模式變成所有開發的預設模式。關於Go module,可閱讀Go官方博客: "Using Go Modules" 以下是Glide的一些使用心得: 一般使用glide install,這樣會讀取配置文件 ...
  • 本人免費整理了Java高級資料,涵蓋了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高併發分散式等教程,一共30G,需要自己領取。傳送門:https://mp.weixin.qq.com/s/osB-BOl6W-ZLTSttTkqMPQ 前 ...
  • 本筆記摘抄自:https://www.cnblogs.com/susufufu/p/6882498.html,記錄一下學習過程以備後續查用。 一、官方概述 特性提供功能強大的方法,用以將元數據或聲明信息與代碼(程式集、類型、方法、屬性等)相關聯。 特性與程式實體關聯後,即可在運行時使用名 為“反射” ...
  • 在前面章節中,花費大量時間分析了視窗的內容模型——換句話說,研究瞭如何在其他元素中嵌套元素,進而構建完整的視窗。 例如,考慮下圖中顯示的一個非常簡單的視窗,該視窗包含兩個按鈕。為創建該按鈕,在視窗中嵌套了一個StackPanel控制項。在StackPanel控制項中,放置了兩個Button控制項,並且在每 ...
  • public enum Seasons { Spring,Summer,Autumn,Winter } static string GetEnumItemName(Enum enumItem) { string itemName = Enum.GetName(enumItem.GetType(), ...
  • 在C#中有許多音頻播放的方案,例如WinForm里調用系統自帶MediaPlayer的COM組件和WPF的MediaPlayer(實質上還是WindowsMediaPlayer) 以及一堆API播放和DirectX (SDK一大堆) 於是我找到了適用於全平臺、高效、小巧的音頻解碼器--Bass (主 ...
  • 1 public string ExcelFile() 2 { //指定文件路徑, 3 string fileName=@"d:\Stu.xls"; //創建一個文件流,並指定其中屬性 4 using(FileStream fs = new FileStream(fileName,FileMode. ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...