(五十九)c#Winform自定義控制項-池子(工業)

来源:https://www.cnblogs.com/bfyx/archive/2019/09/06/11477241.html
-Advertisement-
Play Games

前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...


前提

入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果覺得寫的還行,請點個 star 支持一下吧

歡迎前來交流探討: 企鵝群568015492 企鵝群568015492

麻煩博客下方點個【推薦】,謝謝

NuGet

Install-Package HZH_Controls

目錄

https://www.cnblogs.com/bfyx/p/11364884.html

用處及效果

 

 

準備工作

這個也是用GDI+畫的,應該算是最簡單的控制項了,本來不打算單獨寫一篇文章的,但是好歹也是個控制項吧,於是就寫這裡了

開始

添加一個類UCPond ,繼承UserControl

屬性

  1  /// <summary>
  2         /// The maximum value
  3         /// </summary>
  4         private decimal maxValue = 100;
  5 
  6         /// <summary>
  7         /// Gets or sets the maximum value.
  8         /// </summary>
  9         /// <value>The maximum value.</value>
 10         [Description("最大值"), Category("自定義")]
 11         public decimal MaxValue
 12         {
 13             get { return maxValue; }
 14             set
 15             {
 16                 if (value < m_value)
 17                     return;
 18                 maxValue = value;
 19                 Refresh();
 20             }
 21         }
 22 
 23         /// <summary>
 24         /// The m value
 25         /// </summary>
 26         private decimal m_value = 0;
 27 
 28         /// <summary>
 29         /// Gets or sets the value.
 30         /// </summary>
 31         /// <value>The value.</value>
 32         [Description(""), Category("自定義")]
 33         public decimal Value
 34         {
 35             get { return m_value; }
 36             set
 37             {
 38                 if (value < 0)
 39                     return;
 40                 if (value > maxValue)
 41                     m_value = maxValue;
 42                 else
 43                     m_value = value;
 44                 Refresh();
 45             }
 46         }
 47 
 48         /// <summary>
 49         /// The wall color
 50         /// </summary>
 51         private Color wallColor = Color.FromArgb(255, 77, 59);
 52 
 53         /// <summary>
 54         /// Gets or sets the color of the wall.
 55         /// </summary>
 56         /// <value>The color of the wall.</value>
 57         [Description("池壁顏色"), Category("自定義")]
 58         public Color WallColor
 59         {
 60             get { return wallColor; }
 61             set
 62             {
 63                 wallColor = value;
 64                 Refresh();
 65             }
 66         }
 67 
 68         /// <summary>
 69         /// The wall width
 70         /// </summary>
 71         private int wallWidth = 2;
 72 
 73         /// <summary>
 74         /// Gets or sets the width of the wall.
 75         /// </summary>
 76         /// <value>The width of the wall.</value>
 77         [Description("池壁寬度"), Category("自定義")]
 78         public int WallWidth
 79         {
 80             get { return wallWidth; }
 81             set
 82             {
 83                 if (value <= 0)
 84                     return;
 85                 wallWidth = value;
 86                 Refresh();
 87             }
 88         }
 89 
 90         /// <summary>
 91         /// The liquid color
 92         /// </summary>
 93         private Color liquidColor = Color.FromArgb(3, 169, 243);
 94 
 95         [Description("液體顏色"), Category("自定義")]
 96         public Color LiquidColor
 97         {
 98             get { return liquidColor; }
 99             set { liquidColor = value; }
100         }

重繪

  protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (Height <= 0)
                return;
            var g = e.Graphics;
            g.SetGDIHigh();
            int intHeight = (int)(m_value / maxValue * this.Height);
            if (intHeight != 0)
            {
                g.FillRectangle(new SolidBrush(liquidColor), new Rectangle(0, this.Height - intHeight, this.Width, intHeight));
            }
            //劃邊
            g.FillRectangle(new SolidBrush(wallColor), 0, 0, wallWidth, this.Height);
            g.FillRectangle(new SolidBrush(wallColor), 0, this.Height - wallWidth, this.Width, wallWidth);
            g.FillRectangle(new SolidBrush(wallColor), this.Width - wallWidth-1, 0, wallWidth, this.Height);
        }

完整代碼

  1 // ***********************************************************************
  2 // Assembly         : HZH_Controls
  3 // Created          : 2019-09-06
  4 //
  5 // ***********************************************************************
  6 // <copyright file="UCPond.cs">
  7 //     Copyright by Huang Zhenghui(黃正輝) All, QQ group:568015492 QQ:623128629 Email:[email protected]
  8 // </copyright>
  9 //
 10 // Blog: https://www.cnblogs.com/bfyx
 11 // GitHub:https://github.com/kwwwvagaa/NetWinformControl
 12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
 13 //
 14 // If you use this code, please keep this note.
 15 // ***********************************************************************
 16 using System;
 17 using System.Collections.Generic;
 18 using System.Linq;
 19 using System.Text;
 20 using System.Windows.Forms;
 21 using System.Drawing;
 22 using System.Drawing.Drawing2D;
 23 using System.ComponentModel;
 24 
 25 namespace HZH_Controls.Controls
 26 {
 27     /// <summary>
 28     /// Class UCPond.
 29     /// Implements the <see cref="System.Windows.Forms.UserControl" />
 30     /// </summary>
 31     /// <seealso cref="System.Windows.Forms.UserControl" />
 32     public class UCPond : UserControl
 33     {
 34         /// <summary>
 35         /// The maximum value
 36         /// </summary>
 37         private decimal maxValue = 100;
 38 
 39         /// <summary>
 40         /// Gets or sets the maximum value.
 41         /// </summary>
 42         /// <value>The maximum value.</value>
 43         [Description("最大值"), Category("自定義")]
 44         public decimal MaxValue
 45         {
 46             get { return maxValue; }
 47             set
 48             {
 49                 if (value < m_value)
 50                     return;
 51                 maxValue = value;
 52                 Refresh();
 53             }
 54         }
 55 
 56         /// <summary>
 57         /// The m value
 58         /// </summary>
 59         private decimal m_value = 0;
 60 
 61         /// <summary>
 62         /// Gets or sets the value.
 63         /// </summary>
 64         /// <value>The value.</value>
 65         [Description(""), Category("自定義")]
 66         public decimal Value
 67         {
 68             get { return m_value; }
 69             set
 70             {
 71                 if (value < 0)
 72                     return;
 73                 if (value > maxValue)
 74                     m_value = maxValue;
 75                 else
 76                     m_value = value;
 77                 Refresh();
 78             }
 79         }
 80 
 81         /// <summary>
 82         /// The wall color
 83         /// </summary>
 84         private Color wallColor = Color.FromArgb(255, 77, 59);
 85 
 86         /// <summary>
 87         /// Gets or sets the color of the wall.
 88         /// </summary>
 89         /// <value>The color of the wall.</value>
 90         [Description("池壁顏色"), Category("自定義")]
 91         public Color WallColor
 92         {
 93             get { return wallColor; }
 94             set
 95             {
 96                 wallColor = value;
 97                 Refresh();
 98             }
 99         }
100 
101         /// <summary>
102         /// The wall width
103         /// </summary>
104         private int wallWidth = 2;
105 
106         /// <summary>
107         /// Gets or sets the width of the wall.
108         /// </summary>
109         /// <value>The width of the wall.</value>
110         [Description("池壁寬度"), Category("自定義")]
111         public int WallWidth
112         {
113             get { return wallWidth; }
114             set
115             {
116                 if (value <= 0)
117                     return;
118                 wallWidth = value;
119                 Refresh();
120             }
121         }
122 
123         /// <summary>
124         /// The liquid color
125         /// </summary>
126         private Color liquidColor = Color.FromArgb(3, 169, 243);
127 
128         [Description("液體顏色"), Category("自定義")]
129         public Color LiquidColor
130         {
131             get { return liquidColor; }
132             set { liquidColor = value; }
133         }
134         /// <summary>
135         /// Initializes a new instance of the <see cref="UCPond"/> class.
136         /// </summary>
137         public UCPond()
138         {
139             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
140             this.SetStyle(ControlStyles.DoubleBuffer, true);
141             this.SetStyle(ControlStyles.ResizeRedraw, true);
142             this.SetStyle(ControlStyles.Selectable, true);
143             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
144             this.SetStyle(ControlStyles.UserPaint, true);
145             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
146             this.Size = new Size(150, 50);
147         }
148         /// <summary>
149         /// 引發 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
150         /// </summary>
151         /// <param name="e">包含事件數據的 <see cref="T:System.Windows.Forms.PaintEventArgs" /></param>
152         protected override void OnPaint(PaintEventArgs e)
153         {
154             base.OnPaint(e);
155             if (Height <= 0)
156                 return;
157             var g = e.Graphics;
158             g.SetGDIHigh();
159             int intHeight = (int)(m_value / maxValue * this.Height);
160             if (intHeight != 0)
161             {
162                 g.FillRectangle(new SolidBrush(liquidColor), new Rectangle(0, this.Height - intHeight, this.Width, intHeight));
163             }
164             //劃邊
165             g.FillRectangle(new SolidBrush(wallColor), 0, 0, wallWidth, this.Height);
166             g.FillRectangle(new SolidBrush(wallColor), 0, this.Height - wallWidth, this.Width, wallWidth);
167             g.FillRectangle(new SolidBrush(wallColor), this.Width - wallWidth-1, 0, wallWidth, this.Height);
168         }
169     }
170 }

 

最後的話

如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧


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

-Advertisement-
Play Games
更多相關文章
  • 一、此處主要介紹在springboot工程下如何使用 logback + slf4j 進行日誌記錄。 logback主要包含三個組成部分:Loggers(日誌記錄器)、Appenders(輸出目的在)、Layouts(日誌輸出格式) slf4j :如jdbc一樣,定義了一套介面,是一個日誌門面,可實 ...
  • 引言 我們知道開發最好用Mac/Linux,效率很高,但是對於很多還是Windows用戶的我們來說,編寫代碼再到linux上運行也是很常有的事情,但對於我們寫一些小demo使用上面的流程難免有點興師動眾,傷元氣的事情程式員只會掉發更快,所以再Windows搭建gcc開發環境還是很有必要的,MinGW ...
  • websocket消息服務 目的:搭建websocket服務,用瀏覽器與服務進行消息交互(寫的第一個Go程式) 代碼目錄結構: 前端html頁面: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script> 6 wi ...
  • 題目描述: 給定兩個整數 a, b (a, b 均不超過 int 類型的表示範圍),求出 a + b 的和。輸入描述: 多組輸入,每組輸入為一行,裡面有 2 個數 a, b。輸出描述: 對於每一組輸入,輸出一個值為該組 a + b 的和。樣例輸入: 1 2 2 3樣例輸出: 3 5 ...
  • Linux系統大多數都支持OpenSSH,生成公鑰、私鑰的最好用ssh-keygen命令,如果用putty自帶的PUTTYGEN.EXE生成會不相容OpenSSH,從而會導致登錄時出現server refused our key錯誤。 ...
  • 繼承 什麼是繼承?就是一個派生類(derived class)繼承基類(base class)的欄位和方法。一個類可以被多個類繼承;在python中,一個類可以繼承多個類。 父類可以稱為基類和超類,而子類可以稱為派生類 在繼承中可分為單繼承和多繼承兩種 下麵是繼承的用法,語法為'class 子類的名 ...
  • 參考鏈接:https://www.cnblogs.com/taiyonghai/p/5604159.html 類: /******************************************************************************************* ...
  • 部分一 14年底進入目前公司時,領導準備開發一款新軟體平臺以取代原有平臺。原平臺採用C++Build開發界面(window c/s客戶端) 、Visual Studio(封裝dll模塊)。過完年,領導已把框架搭建完畢(過年期間領導加班了 ^_^ )。當時菜鳥一個(目前老鳥了),新框架用wpf模式,R ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...