(六十)c#Winform自定義控制項-鼓風機(工業)

来源:https://www.cnblogs.com/bfyx/archive/2019/09/09/11491253.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+畫的,不懂的可以自行百度一下

開始

添加2個枚舉,分別控制進出風口的位置

 1   /// <summary>
 2     /// Enum BlowerEntranceDirection
 3     /// </summary>
 4     public enum BlowerEntranceDirection
 5     {
 6         /// <summary>
 7         /// The none
 8         /// </summary>
 9         None,
10         /// <summary>
11         /// The left
12         /// </summary>
13         Left,
14         /// <summary>
15         /// The right
16         /// </summary>
17         Right,
18         /// <summary>
19         /// Up
20         /// </summary>
21         Up
22     }
23 
24     /// <summary>
25     /// Enum BlowerExitDirection
26     /// </summary>
27     public enum BlowerExitDirection
28     {
29         /// <summary>
30         /// The left
31         /// </summary>
32         Left,
33         /// <summary>
34         /// The right
35         /// </summary>
36         Right,
37         /// <summary>
38         /// Up
39         /// </summary>
40         Up
41     }

屬性

 1  /// <summary>
 2         /// The entrance direction
 3         /// </summary>
 4         private BlowerEntranceDirection entranceDirection = BlowerEntranceDirection.None;
 5 
 6         /// <summary>
 7         /// Gets or sets the entrance direction.
 8         /// </summary>
 9         /// <value>The entrance direction.</value>
10         [Description("入口方向"), Category("自定義")]
11         public BlowerEntranceDirection EntranceDirection
12         {
13             get { return entranceDirection; }
14             set
15             {
16                 entranceDirection = value;
17                 Refresh();
18             }
19         }
20 
21         /// <summary>
22         /// The exit direction
23         /// </summary>
24         private BlowerExitDirection exitDirection = BlowerExitDirection.Right;
25 
26         /// <summary>
27         /// Gets or sets the exit direction.
28         /// </summary>
29         /// <value>The exit direction.</value>
30         [Description("出口方向"), Category("自定義")]
31         public BlowerExitDirection ExitDirection
32         {
33             get { return exitDirection; }
34             set
35             {
36                 exitDirection = value;
37                 Refresh();
38             }
39         }
40 
41         /// <summary>
42         /// The blower color
43         /// </summary>
44         private Color blowerColor = Color.FromArgb(255, 77, 59);
45 
46         /// <summary>
47         /// Gets or sets the color of the blower.
48         /// </summary>
49         /// <value>The color of the blower.</value>
50         [Description("風機顏色"), Category("自定義")]
51         public Color BlowerColor
52         {
53             get { return blowerColor; }
54             set
55             {
56                 blowerColor = value;
57                 Refresh();
58             }
59         }
60 
61         /// <summary>
62         /// The fan color
63         /// </summary>
64         private Color fanColor = Color.FromArgb(3, 169, 243);
65 
66         /// <summary>
67         /// Gets or sets the color of the fan.
68         /// </summary>
69         /// <value>The color of the fan.</value>
70         [Description("風葉顏色"), Category("自定義")]
71         public Color FanColor
72         {
73             get { return fanColor; }
74             set
75             {
76                 fanColor = value;
77                 Refresh();
78             }
79         }
80 
81         /// <summary>
82         /// The m rect working
83         /// </summary>
84         Rectangle m_rectWorking;

重繪

  1 protected override void OnPaint(PaintEventArgs e)
  2         {
  3             base.OnPaint(e);
  4             var g = e.Graphics;
  5             g.SetGDIHigh();
  6             GraphicsPath pathLineIn = new GraphicsPath();
  7             GraphicsPath pathLineOut = new GraphicsPath();
  8             int intLinePenWidth = 0;
  9 
 10             switch (exitDirection)
 11             {
 12                 case BlowerExitDirection.Left:
 13                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
 14                     intLinePenWidth = m_rectWorking.Height / 2 - 5;
 15                     pathLineOut.AddLine(new Point(-10, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2));
 16                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(1, m_rectWorking.Top - 2), new Point(1, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) + 2));
 17                     break;
 18                 case BlowerExitDirection.Right:
 19                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(this.Width / 2, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
 20                     intLinePenWidth = m_rectWorking.Height / 2 - 5;
 21                     pathLineOut.AddLine(new Point(this.Width + 10, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2));
 22                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(this.Width - 2, m_rectWorking.Top - 2), new Point(this.Width - 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) + 2));
 23                     break;
 24                 case BlowerExitDirection.Up:
 25                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5), 0, m_rectWorking.Width / 2 - 5, this.Height / 2));
 26                     intLinePenWidth = m_rectWorking.Width / 2 - 5;
 27                     pathLineOut.AddLine(new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) / 2, -10), new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) / 2, m_rectWorking.Top + m_rectWorking.Height / 2));
 28                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Right + 2, 1), new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) - 2, 1));
 29                     break;
 30             }
 31 
 32             switch (entranceDirection)
 33             {
 34                 case BlowerEntranceDirection.Left:
 35                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5, this.Width / 2, m_rectWorking.Height / 2 - 5));
 36                     pathLineIn.AddLine(new Point(-10, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2));
 37                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(1, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 - 2), new Point(1, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) + 2));
 38                     break;
 39                 case BlowerEntranceDirection.Right:
 40                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(this.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5, this.Width / 2, m_rectWorking.Height / 2 - 5));
 41                     pathLineIn.AddLine(new Point(this.Width + 10, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2));
 42                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(this.Width - 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 - 2), new Point(this.Width - 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) + 2));
 43                     break;
 44                 case BlowerEntranceDirection.Up:
 45                     g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(m_rectWorking.Left, 0, m_rectWorking.Width / 2 - 5, this.Height / 2));
 46                     pathLineIn.AddLine(new Point(m_rectWorking.Left + (m_rectWorking.Width / 2 - 5) / 2, -10), new Point(m_rectWorking.Left + (m_rectWorking.Width / 2 - 5) / 2, m_rectWorking.Top + m_rectWorking.Height / 2));
 47                     g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Left - 2, 1), new Point(m_rectWorking.Left + (m_rectWorking.Width / 2 - 5) + 2, 1));
 48                     break;
 49             }
 50 
 51             //漸變色
 52             int _intPenWidth = intLinePenWidth;
 53             int intCount = _intPenWidth / 2 / 4;
 54             for (int i = 0; i < intCount; i++)
 55             {
 56                 int _penWidth = _intPenWidth / 2 - 4 * i;
 57                 if (_penWidth <= 0)
 58                     _penWidth = 1;
 59                 if (entranceDirection != BlowerEntranceDirection.None)
 60                     g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(40, Color.White.R, Color.White.G, Color.White.B)), _penWidth), pathLineIn);
 61                 g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(40, Color.White.R, Color.White.G, Color.White.B)), _penWidth), pathLineOut);
 62                 if (_penWidth == 1)
 63                     break;
 64             }
 65 
 66             //底座
 67             GraphicsPath gpDZ = new GraphicsPath();
 68             gpDZ.AddLines(new Point[] 
 69             {
 70                 new Point( m_rectWorking.Left+m_rectWorking.Width/2,m_rectWorking.Top+m_rectWorking.Height/2),
 71                 new Point(m_rectWorking.Left+2,this.Height),
 72                 new Point(m_rectWorking.Right-2,this.Height)
 73             });
 74             gpDZ.CloseAllFigures();
 75             g.FillPath(new SolidBrush(blowerColor), gpDZ);
 76             g.FillPath(new SolidBrush(Color.FromArgb(50, Color.White)), gpDZ);
 77             g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Left, this.Height - 2), new Point(m_rectWorking.Right, this.Height - 2));
 78 
 79             //中心
 80             g.FillEllipse(new SolidBrush(blowerColor), m_rectWorking);
 81             g.FillEllipse(new SolidBrush(Color.FromArgb(20, Color.White)), m_rectWorking);
 82 
 83 
 84             //扇葉
 85             Rectangle _rect = new Rectangle(m_rectWorking.Left + (m_rectWorking.Width - (m_rectWorking.Width / 3 * 2)) / 2, m_rectWorking.Top + (m_rectWorking.Height - (m_rectWorking.Width / 3 * 2)) / 2, (m_rectWorking.Width / 3 * 2), (m_rectWorking.Width / 3 * 2));
 86 
 87             int _splitCount = 8;
 88             float fltSplitValue = 360F / (float)_splitCount;
 89             for (int i = 0; i <= _splitCount; i++)
 90             {
 91                 float fltAngle = (fltSplitValue * i - 180) % 360;
 92                 float fltY1 = (float)(_rect.Top + _rect.Width / 2 - ((_rect.Width / 2) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
 93                 float fltX1 = (float)(_rect.Left + (_rect.Width / 2 - ((_rect.Width / 2) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
 94                 float fltY2 = 0;
 95                 float fltX2 = 0;
 96 
 97                 fltY2 = (float)(_rect.Top + _rect.Width / 2 - ((_rect.Width / 4) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
 98                 fltX2 = (float)(_rect.Left + (_rect.Width / 2 - ((_rect.Width / 4) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
 99 
100                 g.DrawLine(new Pen(new SolidBrush(fanColor), 2), new PointF(fltX1, fltY1), new PointF(fltX2, fltY2));
101             }
102 
103             g.FillEllipse(new SolidBrush(fanColor), new Rectangle(_rect.Left + _rect.Width / 2 - _rect.Width / 4 + 2, _rect.Top + _rect.Width / 2 - _rect.Width / 4 + 2, _rect.Width / 2 - 4, _rect.Width / 2 - 4));
104             g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), new Rectangle(_rect.Left - 5, _rect.Top - 5, _rect.Width + 10, _rect.Height + 10));
105         }

全部代碼

  1 // ***********************************************************************
  2 // Assembly         : HZH_Controls
  3 // Created          : 2019-09-09
  4 //
  5 // ***********************************************************************
  6 // <copyright file="UCBlower.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 UCBlower.
 29     /// Implements the <see cref="System.Windows.Forms.UserControl" />
 30     /// </summary>
 31     /// <seealso cref="System.Windows.Forms.UserControl" />
 32     public class UCBlower : UserControl
 33     {
 34         /// <summary>
 35         /// The entrance direction
 36         /// </summary>
 37         private BlowerEntranceDirection entranceDirection = BlowerEntranceDirection.None;
 38 
 39         /// <summary>
 40         /// Gets or sets the entrance direction.
 41         /// </summary>
 42         /// <value>The entrance direction.</value>
 43         [Description("入口方向"), Category("自定義")]
 44         public BlowerEntranceDirection EntranceDirection
 45         {
 46             get { return entranceDirection; }
 47             set
 48             {
 49                 entranceDirection = value;
 50                 Refresh();
 51             }
 52         }
 53 
 54         /// <summary>
 55         /// The exit direction
 56         /// </summary>
 57         private BlowerExitDirection exitDirection = BlowerExitDirection.Right;
 58 
 59         /// <summary>
 60         /// Gets or sets the exit direction.
 61         /// </summary>
 62         /// <value>The exit direction.</value>
 63         [Description("出口方向"), Category("自定義")]
 64         public BlowerExitDirection ExitDirection
 65         {
 66             get { return exitDirection; }
 67             set
 68             {
 69                 exitDirection = value;
 70                 Refresh();
 71             }
 72         }
 73 
 74         /// <summary>
 75         /// The blower color
 76         /// </summary>
 77         private Color blowerColor = Color.FromArgb(255, 77, 59);
 78 
 79         /// <summary>
 80         /// Gets or sets the color of the blower.
 81         /// </summary>
 82         /// <value>The color of the blower.</value>
 83         [Description("風機顏色"), Category("自定義")]
 84         public Color BlowerColor
 85         {
 86             get { return blowerColor; }
 87             set
 88             {
 89                 blowerColor = value;
 90                 Refresh();
 91             }
 92         }
 93 
 94         /// <summary>
 95         /// The fan color
 96         /// </summary>
 97         private Color fanColor = Color.FromArgb(3, 169, 243);
 98 
 99         /// <summary>
100         /// Gets or sets the color of the fan.
101         /// </summary>
102         /// <value>The color of the fan.</value>
103         [Description("風葉顏色"), Category("自定義")]
104         public Color FanColor
105         {
106             get { return fanColor; }
107             set
108             {
109                 fanColor = value;
110                 Refresh();
111             }
112         }
113 
114         /// <summary>
115         /// The m rect working
116         /// </summary>
117         Rectangle m_rectWorking;
118 
119         /// <summary>
120         /// Initializes a new in

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

-Advertisement-
Play Games
更多相關文章
  • 本文介紹使用Git初始化本地倉庫,並首次提交代碼到遠程倉庫GitLab上面。 首先,登錄GitLab,創建一個新項目的私人倉庫; 然後,在本地倉庫(就是你寫代碼文件夾),右鍵,Git Bash Here,打開Git命令視窗; 在Git命令視窗輸入 git init,初始化本地倉庫,初始化完成後,本地 ...
  • 簡要介紹微服務架構及其特點,並引入Spring Cloud與其中一部分核心組件。 ...
  • 前幾天微軟發佈了 .NET Core 3.0 Preview 9 ,這是.NET Core 3.0 最後一個預覽版。 .NET Core 3.0 正式發佈將在.NET Conf 上發佈,.NET Conf 時間是9月23日至25日。 Visual Studio 2019 16.3預覽版3和Visua ...
  • 做了很久碼農,也沒個寫博客的習慣,這次開始第一次寫博客。 這個問題也是折騰了我接近一天時間,網上也沒有任何的相關博文,於是決定分享一下,以供同樣擁有此問題的小伙伴們參考。 內容源於目前在做的一個項目,已經封好的功能里,在生成構件時,會產生以 下彈窗。 原以為是前輩留下的信息提示,沒想到是revit自 ...
  • Prism.Unity 中UnityBootStrapper已經不用了,可以繼承PrismApplication 1.Install-package Prism.Unity -v 7.2.0.1367 2. 3. ...
  • 前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
  • 菜鳥作品,不喜勿噴 前兩年自己花了很久想仿製一款火車頭採集器 然後也付出了很多努力,最終未能修成正果 代碼一直在電腦中吃灰,本著無私奉獻的精神 免費開源,給有需要的人參考和完善 軟體功能大部分都已實現 任務新建和編輯 網址採集 標簽編輯 數據採集 數據發佈 發佈配置的修改,編輯和測試 發佈模塊的修改 ...
  • string path = @"C:\Users\Administrator\Desktop\無人智能便利店\install\收銀端\XMLRFI.xml"; XmlDocument xmlDoc = new XmlDocument();//新建XML文件 xmlDoc.Load(path);//加 ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...