(四十二)c#Winform自定義控制項-進度條擴展

来源:https://www.cnblogs.com/bfyx/archive/2019/08/22/11392351.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

用處及效果

準備工作

這個是基於(四十一)c#Winform自定義控制項-進度條 擴展的,如果你還沒有瞭解,請先移步瞭解一下

開始

添加一個用戶控制項,命名UCProcessLineExt

屬性

 1   [Description("值變更事件"), Category("自定義")]
 2         public event EventHandler ValueChanged;
 3 
 4         [Description("當前屬性"), Category("自定義")]
 5         public int Value
 6         {
 7             set
 8             {
 9                 ucProcessLine1.Value = value;
10                 Refresh();
11             }
12             get
13             {
14                 return ucProcessLine1.Value;
15             }
16         }
17 
18 
19 
20         [Description("最大值"), Category("自定義")]
21         public int MaxValue
22         {
23             get { return ucProcessLine1.MaxValue; }
24             set
25             {
26                 ucProcessLine1.MaxValue = value;
27                 Refresh();
28             }
29         }
30 
31 
32         [Description("值進度條顏色"), Category("自定義")]
33         public Color ValueColor
34         {
35             get { return ucProcessLine1.ValueColor; }
36             set
37             {
38                 ucProcessLine1.ValueColor = value;
39                 Refresh();
40             }
41         }
42 
43 
44         [Description("值背景色"), Category("自定義")]
45         public Color ValueBGColor
46         {
47             get { return ucProcessLine1.ValueBGColor; }
48             set
49             {
50                 ucProcessLine1.ValueBGColor = value;
51                 Refresh();
52             }
53         }
54 
55 
56         [Description("邊框顏色"), Category("自定義")]
57         public Color BorderColor
58         {
59             get { return ucProcessLine1.BorderColor; }
60             set
61             {
62                 ucProcessLine1.BorderColor = value;
63                 Refresh();
64             }
65         }
66 
67         [Description("值字體"), Category("自定義")]
68         public override Font Font
69         {
70             get
71             {
72                 return ucProcessLine1.Font;
73             }
74             set
75             {
76                 ucProcessLine1.Font = value;
77                 Refresh();
78             }
79         }
80 
81         [Description("值塊顏色"), Category("自定義")]
82         public override System.Drawing.Color ForeColor
83         {
84             get
85             {
86                 return base.ForeColor;
87             }
88             set
89             {
90                 base.ForeColor = value;
91                 Refresh();
92             }
93         }

重繪

 1  protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4             e.Graphics.SetGDIHigh();
 5             float fltIndex = (float)this.ucProcessLine1.Value / (float)this.ucProcessLine1.MaxValue;
 6 
 7             int x = (int)(fltIndex * this.ucProcessLine1.Width + this.ucProcessLine1.Location.X - 15) - 2;
 8             GraphicsPath path = new GraphicsPath();
 9             Rectangle rect = new Rectangle(x, 1, 30, 20);
10             int cornerRadius = 2;
11             path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
12             path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
13             path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
14             path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
15             path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
16             path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Right - cornerRadius * 2 - 5, rect.Bottom);//
17             path.AddLine(rect.Right - cornerRadius * 2 - 5, 21, x + 15, ucProcessLine1.Location.Y);
18             path.AddLine(x + 15, ucProcessLine1.Location.Y, rect.X + cornerRadius * 2 + 5, 21);
19             path.AddLine(rect.X + cornerRadius * 2 + 5, 20, rect.X + cornerRadius * 2, rect.Bottom);//
20             path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
21             path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//
22             path.CloseFigure();
23 
24             e.Graphics.FillPath(new SolidBrush(ForeColor), path);
25 
26             string strValue = ((float)Value / (float)MaxValue).ToString("0%");
27             System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
28             e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.White), new PointF(x + (30 - sizeF.Width) / 2+1, (20 - sizeF.Height) / 2 + 1));
29         }

全部代碼

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Data;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Drawing.Drawing2D;
 10 
 11 namespace HZH_Controls.Controls
 12 {
 13     public partial class UCProcessLineExt : UserControl
 14     {
 15         [Description("值變更事件"), Category("自定義")]
 16         public event EventHandler ValueChanged;
 17 
 18         [Description("當前屬性"), Category("自定義")]
 19         public int Value
 20         {
 21             set
 22             {
 23                 ucProcessLine1.Value = value;
 24                 Refresh();
 25             }
 26             get
 27             {
 28                 return ucProcessLine1.Value;
 29             }
 30         }
 31 
 32 
 33 
 34         [Description("最大值"), Category("自定義")]
 35         public int MaxValue
 36         {
 37             get { return ucProcessLine1.MaxValue; }
 38             set
 39             {
 40                 ucProcessLine1.MaxValue = value;
 41                 Refresh();
 42             }
 43         }
 44 
 45 
 46         [Description("值進度條顏色"), Category("自定義")]
 47         public Color ValueColor
 48         {
 49             get { return ucProcessLine1.ValueColor; }
 50             set
 51             {
 52                 ucProcessLine1.ValueColor = value;
 53                 Refresh();
 54             }
 55         }
 56 
 57 
 58         [Description("值背景色"), Category("自定義")]
 59         public Color ValueBGColor
 60         {
 61             get { return ucProcessLine1.ValueBGColor; }
 62             set
 63             {
 64                 ucProcessLine1.ValueBGColor = value;
 65                 Refresh();
 66             }
 67         }
 68 
 69 
 70         [Description("邊框顏色"), Category("自定義")]
 71         public Color BorderColor
 72         {
 73             get { return ucProcessLine1.BorderColor; }
 74             set
 75             {
 76                 ucProcessLine1.BorderColor = value;
 77                 Refresh();
 78             }
 79         }
 80 
 81         [Description("值字體"), Category("自定義")]
 82         public override Font Font
 83         {
 84             get
 85             {
 86                 return ucProcessLine1.Font;
 87             }
 88             set
 89             {
 90                 ucProcessLine1.Font = value;
 91                 Refresh();
 92             }
 93         }
 94 
 95         [Description("值塊顏色"), Category("自定義")]
 96         public override System.Drawing.Color ForeColor
 97         {
 98             get
 99             {
100                 return base.ForeColor;
101             }
102             set
103             {
104                 base.ForeColor = value;
105                 Refresh();
106             }
107         }
108 
109         public UCProcessLineExt()
110         {
111             InitializeComponent();
112             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
113             this.SetStyle(ControlStyles.DoubleBuffer, true);
114             this.SetStyle(ControlStyles.ResizeRedraw, true);
115             this.SetStyle(ControlStyles.Selectable, true);
116             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
117             this.SetStyle(ControlStyles.UserPaint, true);
118             ucProcessLine1.ValueChanged += ucProcessLine1_ValueChanged;
119         }
120 
121         void ucProcessLine1_ValueChanged(object sender, EventArgs e)
122         {
123             if (ValueChanged != null)
124             {
125                 ValueChanged(this, e);
126             }
127         }
128 
129         protected override void OnPaint(PaintEventArgs e)
130         {
131             base.OnPaint(e);
132             e.Graphics.SetGDIHigh();
133             float fltIndex = (float)this.ucProcessLine1.Value / (float)this.ucProcessLine1.MaxValue;
134 
135             int x = (int)(fltIndex * this.ucProcessLine1.Width + this.ucProcessLine1.Location.X - 15) - 2;
136             GraphicsPath path = new GraphicsPath();
137             Rectangle rect = new Rectangle(x, 1, 30, 20);
138             int cornerRadius = 2;
139             path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
140             path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
141             path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
142             path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
143             path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
144             path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Right - cornerRadius * 2 - 5, rect.Bottom);//
145             path.AddLine(rect.Right - cornerRadius * 2 - 5, 21, x + 15, ucProcessLine1.Location.Y);
146             path.AddLine(x + 15, ucProcessLine1.Location.Y, rect.X + cornerRadius * 2 + 5, 21);
147             path.AddLine(rect.X + cornerRadius * 2 + 5, 20, rect.X + cornerRadius * 2, rect.Bottom);//
148             path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
149             path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//
150             path.CloseFigure();
151 
152             e.Graphics.FillPath(new SolidBrush(ForeColor), path);
153 
154             string strValue = ((float)Value / (float)MaxValue).ToString("0%");
155             System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
156             e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.White), new PointF(x + (30 - sizeF.Width) / 2+1, (20 - sizeF.Height) / 2 + 1));
157         }
158     }
159 }
View Code
 1 namespace HZH_Controls.Controls
 2 {
 3     partial class UCProcessLineExt
 4     {
 5         /// <summary> 
 6         /// 必需的設計器變數。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary> 
11         /// 清理所有正在使用的資源。
12         /// </summary>
13         /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region 組件設計器生成的代碼
24 
25         /// <summary> 
26         /// 設計器支持所需的方法 - 不要
27         /// 使用代碼編輯器修改此方法的內容。
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.ucProcessLine1 = new HZH_Controls.Controls.UCProcessLine();
32             this.SuspendLayout();
33             // 
34             // ucProcessLine1
35             // 
36             this.ucProcessLine1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
37             | System.Windows.Forms.AnchorStyles.Left) 
38             | System.Windows.Forms.AnchorStyles.Right)));
39             this.ucProcessLine1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
40             this.ucProcessLine1.Font = new System.Drawing.Font("Arial Unicode MS", 10F);
41             this.ucProcessLine1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
42             this.ucProcessLine1.Location = new System.Drawing.Point(18, 33);
43             this.ucProcessLine1.MaxValue = 100;
44             this.ucProcessLine1.Name = "ucProcessLine1";
45             this.ucProcessLine1.Size = new System.Drawing.Size(399, 16);
46             this.ucProcessLine1.TabIndex = 0;
47             this.ucProcessLine1.Text = "ucProcessLine1";
48             this.ucProcessLine1.Value = 0;
49             this.ucProcessLine1.ValueBGColor = System.Drawing.Color.White;
50             this.ucProcessLine1.ValueColor = System.Drawing.Color.FromArgb(((int)(((byte)(73)))), ((int)(((byte)(119)))), ((int)(((byte)(232)))));
51             this.ucProcessLine1.ValueTextType = HZH_Controls.Controls.ValueTextType.None;
52             // 
53             // UCProcessLineExt
54             // 
55             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
56             this.BackColor = System.Drawing.Color.Transparent;
57             this.Controls.Add(this.ucProcessLine1);
58             this.Name = "UCProcessLineExt";
59             this.Size = new System.Drawing.Size(434, 50);
60             this.ResumeLayout(false);
61 
62         }
63 
64         #endregion
65 
66         private UCProcessLine ucProcessLine1;
67     }
68 }
View Code

最後的話

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


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

-Advertisement-
Play Games
更多相關文章
  • 一、Swagger是什麼 Swagger 是一款RESTFUL介面的、基於YAML、JSON語言的文檔線上自動生成、代碼自動生成的工具。 二、如何在項目中加入Swagger Swagger安裝引用 右鍵Web項目依賴項>管理NuGet程式包>在搜索框輸入"Swashbuckle.AspNetCore ...
  • 本次應用DevExpress和C#語言製作了一個批量添加水印的程式,看界面效果圖: 界面中既可以進行文字水印添加,也可以圖片水印添加,同時還可以對水印的位置進行設置,比較實用! 文字水印的具體添加情況,看圖: 還可以文字的預覽: 整個文字水印的預覽: 同時圖片的水印預覽: 最後顯示下圖片的水印效果: ...
  • 摘要 本文將介紹如何通過VS2019創建Xamarin.Forms應用程式,以及如何進行調試。 前言 本文介紹Xamarin.Froms應用程式的創建和調試。 開發環境 1.Visual Studio 2019 2.Xamarin.Forms 3.6.0.344457 創建 1.打開VS2019,選 ...
  • 初學者經常碰到的,即獲取HTML元素集合,迴圈給元素添加事件。在事件響應函數中(event handler)獲取對應的索引。但每次獲取的都是最後一次迴圈的索引。原因是初學者並未理解JavaScript的閉包特性。 1. <!DOCTYPE HTML> 2. <html> 3. <head> 4. < ...
  • 前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
  • 話不多說,上圖: 整體項目結構如圖所示,我的設計初衷是基於.netCore + DI + Vue 打造一個適合初學者的簡捷開發框架。 架構模型採用基於RESTful API風格的前後臺分離框架,總體分為五層:表示層(前端UI)、交互層、業務層、數據訪問層、數據存儲層。 項目中用到的技術如下圖所示: ...
  • 系統環境: Windows + .Net Framework 4.0 問題描述: C#連接FTP下載文件時,在部分電腦上有異常報錯,在一部分電腦上是正常的;異常報錯的信息:System.InvalidOperationException: The requested FTP command is n ...
  • 1. 在WPF怎麼在UI上添加超級鏈接 這篇文章的目的是介紹怎麼在WPF里創建自定義的HyperlinkButton控制項。很神奇的,WPF居然連HyperlinkButton都沒有,不過它提供了另一種方式用於在UI上添加超級鏈接: 如果需要在超級鏈接里放圖片或其它東西,代碼如下: 這真是很怪,為什麼 ...
一周排行
    -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中,預設只支持固定左側列,這跟大家習慣性操作列放最後不符,今天就來介紹一種簡單的方式實現固定右側列。(這裡的實現方式參考的大佬 ...