(六十三)c#Winform自定義控制項-箭頭(工業)

来源:https://www.cnblogs.com/bfyx/archive/2019/09/10/11496921.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+,請自行百度瞭解

開始

添加一個類UCArrow,繼承UserControl

添加枚舉,控制方向

 1 /// <summary>
 2     /// Enum ArrowDirection
 3     /// </summary>
 4     public enum ArrowDirection
 5     {
 6         /// <summary>
 7         /// The left
 8         /// </summary>
 9         Left,
10         /// <summary>
11         /// The right
12         /// </summary>
13         Right,
14         /// <summary>
15         /// The top
16         /// </summary>
17         Top,
18         /// <summary>
19         /// The bottom
20         /// </summary>
21         Bottom,
22         /// <summary>
23         /// The left right
24         /// </summary>
25         Left_Right,
26         /// <summary>
27         /// The top bottom
28         /// </summary>
29         Top_Bottom
30     }

一些屬性

  1  /// <summary>
  2         /// The arrow color
  3         /// </summary>
  4         private Color arrowColor = Color.FromArgb(255, 77, 59);
  5 
  6         /// <summary>
  7         /// Gets or sets the color of the arrow.
  8         /// </summary>
  9         /// <value>The color of the arrow.</value>
 10         [Description("箭頭顏色"), Category("自定義")]
 11         public Color ArrowColor
 12         {
 13             get { return arrowColor; }
 14             set
 15             {
 16                 arrowColor = value;
 17                 Refresh();
 18             }
 19         }
 20 
 21         /// <summary>
 22         /// The border color
 23         /// </summary>
 24         private Color? borderColor = null;
 25 
 26         /// <summary>
 27         /// Gets or sets the color of the border.
 28         /// </summary>
 29         /// <value>The color of the border.</value>
 30         [Description("箭頭邊框顏色,為空則無邊框"), Category("自定義")]
 31         public Color? BorderColor
 32         {
 33             get { return borderColor; }
 34             set
 35             {
 36                 borderColor = value;
 37                 Refresh();
 38             }
 39         }
 40 
 41         /// <summary>
 42         /// The direction
 43         /// </summary>
 44         private ArrowDirection direction = ArrowDirection.Right;
 45 
 46         /// <summary>
 47         /// Gets or sets the direction.
 48         /// </summary>
 49         /// <value>The direction.</value>
 50         [Description("箭頭方向"), Category("自定義")]
 51         public ArrowDirection Direction
 52         {
 53             get { return direction; }
 54             set
 55             {
 56                 direction = value;
 57                 ResetPath();
 58                 Refresh();
 59             }
 60         }
 61         /// <summary>
 62         /// 獲取或設置控制項顯示的文字的字體。
 63         /// </summary>
 64         /// <value>The font.</value>
 65         /// <PermissionSet>
 66         ///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 67         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 68         ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
 69         ///   <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 70         /// </PermissionSet>
 71         public override Font Font
 72         {
 73             get
 74             {
 75                 return base.Font;
 76             }
 77             set
 78             {
 79                 base.Font = value;
 80                 Refresh();
 81             }
 82         }
 83         /// <summary>
 84         /// 獲取或設置控制項的前景色。
 85         /// </summary>
 86         /// <value>The color of the fore.</value>
 87         /// <PermissionSet>
 88         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 89         /// </PermissionSet>
 90         public override Color ForeColor
 91         {
 92             get
 93             {
 94                 return base.ForeColor;
 95             }
 96             set
 97             {
 98                 base.ForeColor = value;
 99                 Refresh();
100             }
101         }
102         /// <summary>
103         /// The text
104         /// </summary>
105         private string text;
106         /// <summary>
107         /// Gets or sets the text.
108         /// </summary>
109         /// <value>The text.</value>
110         [Bindable(true)]
111         [Browsable(true)]
112         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
113         [EditorBrowsable(EditorBrowsableState.Always)]
114         [Localizable(true)]
115         [Description("箭頭文字"), Category("自定義")]
116         public override string Text
117         {
118             get
119             {
120                 return text;
121             }
122             set
123             {
124                 text = value;
125                 Refresh();
126             }
127         }
128         /// <summary>
129         /// The m path
130         /// </summary>
131         GraphicsPath m_path;

根據方向和大小設置path

 1   private void ResetPath()
 2         {
 3             Point[] ps = null;
 4             switch (direction)
 5             {
 6                 case ArrowDirection.Left:
 7                     ps = new Point[] 
 8                     { 
 9                         new Point(0,this.Height/2),
10                         new Point(40,0),
11                         new Point(40,this.Height/4),
12                         new Point(this.Width-1,this.Height/4),
13                         new Point(this.Width-1,this.Height-this.Height/4),
14                         new Point(40,this.Height-this.Height/4),
15                         new Point(40,this.Height),
16                         new Point(0,this.Height/2)
17                     };
18                     break;
19                 case ArrowDirection.Right:
20                     ps = new Point[] 
21                     {
22                         new Point(0,this.Height/4),
23                         new Point(this.Width-40,this.Height/4),
24                         new Point(this.Width-40,0),
25                         new Point(this.Width-1,this.Height/2),
26                         new Point(this.Width-40,this.Height),
27                         new Point(this.Width-40,this.Height-this.Height/4),                      
28                         new Point(0,this.Height-this.Height/4),
29                         new Point(0,this.Height/4)
30                     };
31                     break;
32                 case ArrowDirection.Top:
33                     ps = new Point[] 
34                     {
35                        new Point(this.Width/2,0),
36                        new Point(this.Width,40),
37                        new Point(this.Width-this.Width/4,40),
38                        new Point(this.Width-this.Width/4,this.Height-1),
39                        new Point(this.Width/4,this.Height-1),
40                        new Point(this.Width/4,40),
41                        new Point(0,40),
42                        new Point(this.Width/2,0),
43                     };
44                     break;
45                 case ArrowDirection.Bottom:
46                     ps = new Point[] 
47                     {
48                        new Point(this.Width-this.Width/4,0),
49                        new Point(this.Width-this.Width/4,this.Height-40),
50                        new Point(this.Width,this.Height-40),
51                        new Point(this.Width/2,this.Height-1),
52                        new Point(0,this.Height-40),
53                        new Point(this.Width/4,this.Height-40),
54                        new Point(this.Width/4,0),
55                        new Point(this.Width-this.Width/4,0),                      
56                     };
57                     break;
58                 case ArrowDirection.Left_Right:
59                     ps = new Point[] 
60                     { 
61                         new Point(0,this.Height/2),
62                         new Point(40,0),
63                         new Point(40,this.Height/4),
64                         new Point(this.Width-40,this.Height/4),
65                         new Point(this.Width-40,0),
66                         new Point(this.Width-1,this.Height/2),
67                         new Point(this.Width-40,this.Height),
68                         new Point(this.Width-40,this.Height-this.Height/4),
69                         new Point(40,this.Height-this.Height/4),
70                         new Point(40,this.Height),
71                         new Point(0,this.Height/2),                       
72                     };
73                     break;
74                 case ArrowDirection.Top_Bottom:
75                     ps = new Point[] 
76                     {
77                        new Point(this.Width/2,0),
78                        new Point(this.Width,40),
79                        new Point(this.Width-this.Width/4,40),
80                        new Point(this.Width-this.Width/4,this.Height-40),
81                        new Point(this.Width,this.Height-40),
82                        new Point(this.Width/2,this.Height-1),
83                        new Point(0,this.Height-40),
84                        new Point(this.Width/4,this.Height-40),
85                        new Point(this.Width/4,40),
86                        new Point(0,40),
87                        new Point(this.Width/2,0),                      
88                     };
89                     break;
90             }
91             m_path = new GraphicsPath();
92             m_path.AddLines(ps);
93             m_path.CloseAllFigures();
94         }

重繪

 1   protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4             var g = e.Graphics;
 5             g.SetGDIHigh();
 6 
 7             g.FillPath(new SolidBrush(arrowColor), m_path);
 8 
 9             if (borderColor != null && borderColor != Color.Empty)
10                 g.DrawPath(new Pen(new SolidBrush(borderColor.Value)), m_path);
11             if (!string.IsNullOrEmpty(text))
12             {
13                 var size = g.MeasureString(Text, Font);
14                 g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, (this.Height - size.Height) / 2));
15             }
16         }

完整代碼

  1 // ***********************************************************************
  2 // Assembly         : HZH_Controls
  3 // Created          : 2019-09-10
  4 //
  5 // ***********************************************************************
  6 // <copyright file="UCArrow.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 UCArrow.
 29     /// Implements the <see cref="System.Windows.Forms.UserControl" />
 30     /// </summary>
 31     /// <seealso cref="System.Windows.Forms.UserControl" />
 32     public class UCArrow : UserControl
 33     {
 34         /// <summary>
 35         /// The arrow color
 36         /// </summary>
 37         private Color arrowColor = Color.FromArgb(255, 77, 59);
 38 
 39         /// <summary>
 40         /// Gets or sets the color of the arrow.
 41         /// </summary>
 42         /// <value>The color of the arrow.</value>
 43         [Description("箭頭顏色"), Category("自定義")]
 44         public Color ArrowColor
 45         {
 46             get { return arrowColor; }
 47             set
 48             {
 49                 arrowColor = value;
 50                 Refresh();
 51             }
 52         }
 53 
 54         /// <summary>
 55         /// The border color
 56         /// </summary>
 57         private Color? borderColor = null;
 58 
 59         /// <summary>
 60         /// Gets or sets the color of the border.
 61         /// </summary>
 62         /// <value>The color of the border.</value>
 63         [Description("箭頭邊框顏色,為空則無邊框"), Category("自定義")]
 64         public Color? BorderColor
 65         {
 66             get { return borderColor; }
 67             set
 68             {
 69                 borderColor = value;
 70                 Refresh();
 71             }
 72         }
 73 
 74         /// <summary>
 75         /// The direction
 76         /// </summary>
 77         private ArrowDirection direction = ArrowDirection.Right;
 78 
 79         /// <summary>
 80         /// Gets or sets the direction.
 81         /// </summary>
 82         /// <value>The direction.</value>
 83         [Description("箭頭方向"), Category("自定義")]
 84         public ArrowDirection Direction
 85         {
 86             get { return direction; }
 87             set
 88             {
 89                 direction = value;
 90                 ResetPath();
 91                 Refresh();
 92             }
 93         }
 94         /// <summary>
 95         /// 獲取或設置控制項顯示的文字的字體。
 96         /// </summary>
 97         /// <value>The font.</value>
 98         /// <PermissionSet>
 99         ///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
100         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
101         ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
102         ///   <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
103         /// </PermissionSet>
104         public override Font Font
105         {
106             get
107             {
108                 retu

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

-Advertisement-
Play Games
更多相關文章
  • 官網地址:https://framework7.io/docs/autocomplete.html#autocomplete-parameters 效果圖: <meta charset="UTF-8"><meta name="viewport" content="width=device-width ...
  • Http請求資源的過程可以看成一個管道:“Pipe”,並不是所有的請求都是合法的、安全的,其於功能、性能或安全方面的考慮,通常需要在這管道中裝配一些處理程式來篩選和加工這些請求。這些處理程式就是中間件。 中間件之間的調用順序就是添加中間件組件的順序,調用順序以於應用程式的安全性、性能、和功能至關重要 ...
  • 前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
  • 背景:臨時提供一個簡單的網頁,供其他人瀏覽資料庫(Oracel、MSSQL)的某些數據,並導出Excel。 ...
  • 依賴註入主要是一種結構性的模式,註重的是類與類之間的結構,它要達到的目的就是設計原則中最少知道和合成復用的原則,減少內部依賴,履行單一職責,最終就是強解耦。依賴註入目前最好的實現就是依賴註入容器。 Unity是微軟Patterns & Practices團隊所開發的一個輕量級的,並且可擴展的依賴註入 ...
  • 場景 DevExpress的PanelControl常用進行窗體頁面的佈局。 一般是拖拽一個PannelControl,然後是再拖拽其他控制項。 如果是由代碼生成控制項並控制佈局的話,怎樣實現。 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 比如說要在PanelContrl中 ...
  • 通過 abp(net core)+easyui+efcore實現倉儲管理系統——菜單-上 (十六)這篇文章,我們已經瞭解了ABP中的菜單相關的類及類的屬性與方法,接下我們通過實例來實現一個動態載入菜單的功能。動態菜單是我們在abp(net core)+easyui+efcore實現倉儲管理系統——領... ...
  • 場景 使用DevExpress的EditText控制項時,需要限制其輸入類型為數字。 正常來說是窗體上拖拽一個TextEdit,然後在設計視窗點擊小三角,選擇Change Mask 但是如果說TextEdit控制項不是拖拽上去而是由代碼生成的,那麼在代碼中怎樣設置只能輸入數字。 關註公眾號 霸道的程式猿 ...
一周排行
    -Advertisement-
    Play Games
  • 概述:在C#中,++i和i++都是自增運算符,其中++i先增加值再返回,而i++先返回值再增加。應用場景根據需求選擇,首碼適合先增後用,尾碼適合先用後增。詳細示例提供清晰的代碼演示這兩者的操作時機和實際應用。 在C#中,++i 和 i++ 都是自增運算符,但它們在操作上有細微的差異,主要體現在操作的 ...
  • 上次發佈了:Taurus.MVC 性能壓力測試(ap 壓測 和 linux 下wrk 壓測):.NET Core 版本,今天計劃準備壓測一下 .NET 版本,來測試並記錄一下 Taurus.MVC 框架在 .NET 版本的性能,以便後續持續優化改進。 為了方便對比,本文章的電腦環境和測試思路,儘量和... ...
  • .NET WebAPI作為一種構建RESTful服務的強大工具,為開發者提供了便捷的方式來定義、處理HTTP請求並返迴響應。在設計API介面時,正確地接收和解析客戶端發送的數據至關重要。.NET WebAPI提供了一系列特性,如[FromRoute]、[FromQuery]和[FromBody],用 ...
  • 原因:我之所以想做這個項目,是因為在之前查找關於C#/WPF相關資料時,我發現講解圖像濾鏡的資源非常稀缺。此外,我註意到許多現有的開源庫主要基於CPU進行圖像渲染。這種方式在處理大量圖像時,會導致CPU的渲染負擔過重。因此,我將在下文中介紹如何通過GPU渲染來有效實現圖像的各種濾鏡效果。 生成的效果 ...
  • 引言 上一章我們介紹了在xUnit單元測試中用xUnit.DependencyInject來使用依賴註入,上一章我們的Sample.Repository倉儲層有一個批量註入的介面沒有做單元測試,今天用這個示例來演示一下如何用Bogus創建模擬數據 ,和 EFCore 的種子數據生成 Bogus 的優 ...
  • 一、前言 在自己的項目中,涉及到實時心率曲線的繪製,項目上的曲線繪製,一般很難找到能直接用的第三方庫,而且有些還是定製化的功能,所以還是自己繪製比較方便。很多人一聽到自己畫就害怕,感覺很難,今天就分享一個完整的實時心率數據繪製心率曲線圖的例子;之前的博客也分享給DrawingVisual繪製曲線的方 ...
  • 如果你在自定義的 Main 方法中直接使用 App 類並啟動應用程式,但發現 App.xaml 中定義的資源沒有被正確載入,那麼問題可能在於如何正確配置 App.xaml 與你的 App 類的交互。 確保 App.xaml 文件中的 x:Class 屬性正確指向你的 App 類。這樣,當你創建 Ap ...
  • 一:背景 1. 講故事 上個月有個朋友在微信上找到我,說他們的軟體在客戶那邊隔幾天就要崩潰一次,一直都沒有找到原因,讓我幫忙看下怎麼回事,確實工控類的軟體環境複雜難搞,朋友手上有一個崩潰的dump,剛好丟給我來分析一下。 二:WinDbg分析 1. 程式為什麼會崩潰 windbg 有一個厲害之處在於 ...
  • 前言 .NET生態中有許多依賴註入容器。在大多數情況下,微軟提供的內置容器在易用性和性能方面都非常優秀。外加ASP.NET Core預設使用內置容器,使用很方便。 但是筆者在使用中一直有一個頭疼的問題:服務工廠無法提供請求的服務類型相關的信息。這在一般情況下並沒有影響,但是內置容器支持註冊開放泛型服 ...
  • 一、前言 在項目開發過程中,DataGrid是經常使用到的一個數據展示控制項,而通常表格的最後一列是作為操作列存在,比如會有編輯、刪除等功能按鈕。但WPF的原始DataGrid中,預設只支持固定左側列,這跟大家習慣性操作列放最後不符,今天就來介紹一種簡單的方式實現固定右側列。(這裡的實現方式參考的大佬 ...