C#自定義控制項—流動管道

来源:https://www.cnblogs.com/guoenshuo/p/18391637
-Advertisement-
Play Games

國內文章 【音視頻通話】使用asp.net core 8+vue3 實現高效音視頻通話 https://www.cnblogs.com/1996-Chinese-Chen/p/18384394 該文章描述了使用SRS實現音視頻通話和共用桌面的經驗。從最初使用nginx的RTMP到研究SRS和ZLMe ...


C#用戶控制項之流動管道

如何繪製一個動態的流動管道(FlowPipe)?

分兩步繪製

  1. 定義屬性;
  2. 畫布重繪;

主要技能:

  • 管道的繪製(漸變色矩形)
  /// <summary>
  /// 畫漸變色矩形的方法
  /// </summary>
  /// <param name="g">畫布</param>
  /// <param name="brush">畫刷</param>
  /// <param name="pen">筆</param>
  /// <param name="rectangle">矩形</param>
  private void PaintRectangle(Graphics g, Brush brush, Pen pen, Rectangle rectangle)
  {
      //填充矩形
      g.FillRectangle(brush, rectangle);

      switch (this.pipeStyle)
      {
          case PipeStyle.Horizontal:
              g.DrawLine(pen, rectangle.X, rectangle.Y, rectangle.X + rectangle.Width, rectangle.Y);
              g.DrawLine(pen, rectangle.X, rectangle.Y + rectangle.Height - 1, rectangle.X + rectangle.Width, rectangle.Height);
              break;
          case PipeStyle.Vertical:
              g.DrawLine(pen, rectangle.X, rectangle.Y, rectangle.X, rectangle.Y + rectangle.Height);
              g.DrawLine(pen, rectangle.X + rectangle.Width - 1, rectangle.Y, rectangle.X + rectangle.Width - 1, rectangle.Height);
              break;
          default:
              break;
      }
  }
  • 管道的繪製(漸變色半圓)
/// <summary>
/// 畫漸變色半圓的方法
/// </summary>
/// <param name="g">畫布</param>
/// <param name="colorBlend"></param>
/// <param name="p"></param>
/// <param name="rect"></param>
/// <param name="startAngle"></param>
/// <param name="sweepAngle"></param>
private void PaintEllipse(Graphics g, ColorBlend colorBlend, Pen p, Rectangle rect, float startAngle, float sweepAngle)
{
    //第一步:創建GPI路徑
    GraphicsPath path = new GraphicsPath();
    path.AddEllipse(rect);

    //第二步:漸變色填充
    PathGradientBrush brush = new PathGradientBrush(path);
    brush.CenterPoint = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
    brush.InterpolationColors = colorBlend;

    //第三步:繪製管道
    g.FillPie(brush, rect, startAngle, sweepAngle);

    //第四步:繪製邊線
    g.DrawArc(p, rect, startAngle, sweepAngle);
}
  • 流動條的繪製(用筆的虛線)
//畫虛線,關鍵用筆和路徑來畫
Pen pen = new Pen(this.flowColor, this.flowWidth);
pen.DashStyle = DashStyle.Custom;
pen.DashPattern = new float[]
{
    flowLength,flowLengthGap
};
pen.DashOffset = this.startOffset;
g.DrawPath(pen, path);

 //流動條路徑
 GraphicsPath path = new GraphicsPath();

 //虛線路徑—左邊、中間、右邊
 switch (this.pipeTurnLeft)
 {
     case PipeTurn.Up:
         path.AddArc(new Rectangle(this.Height / 2, this.Height / 2 * (-1) -1, this.Height, this.Height), 181.0f, -91.0f);
         break;
     case PipeTurn.Down:
         path.AddArc(new Rectangle(this.Height / 2, this.Height / 2, this.Height, this.Height), 180.0f, 90.0f);
         break;
     default:
         path.AddLine(-1, this.Height / 2, this.Height+1, this.Height / 2);
         break;
 }
  • 關鍵理解:繪製的橢圓、線(Rectangle)<x,y【圓切矩形相對於控制項原點<左上角>的坐標】,寬,高,開始角度,掃描角度>理解了就好畫了
    path.AddArc(new Rectangle(this.Height / 2, this.Height / 2, this.Height, this.Height), 180.0f, 90.0f);
    path.AddLine(-1, this.Height / 2, this.Height+1, this.Height / 2);

  • 可以流動的關鍵要素

     //流動條流動速度(刷新速度)
     this.myTimer = new Timer();
     myTimer.Interval = 50;
     this.myTimer.Tick += MyTimer_Tick; ;

 }

 #region 定時迴圈
 private void MyTimer_Tick(object sender, EventArgs e)
 {
     this.startOffset = this.startOffset - this.moveSpeed;

     if (this.startOffset > this.flowLength + this.flowLengthGap || this.startOffset < (this.flowLength + this.flowLengthGap) * (-1))
     { this.startOffset = 0; }
     this.Invalidate();
 }

 #endregion

1.定義屬性

  • 管道的(兩端轉向、樣式、邊沿顏色、中間顏色、激活)
  • 流動條的(速度、長度、寬度、間隙、顏色)
//屬性示例:按照示例添加以上各種屬性
private float moveSpeed = 0.3f;
[Browsable(true)]
[Category("佈局_G")]
[Description("流動條速度,負數為反向")]  //屬性說明
public float MoveSpeed
{
    get { return moveSpeed; }
    set
    {
        this.moveSpeed = value;
        this.Invalidate();  //重繪
    }
}

2.畫布重繪

【管道分為左、中、右三部分。先畫管道(矩形):左、中、右;再畫流動條(虛線):左、中、右】
//矩形畫刷
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), pipeColorEdge, pipeColorEdge);
linearGradientBrush.InterpolationColors = colorBlend;

//繪製左部分
switch (this.pipeTurnLeft)
{
    case PipeTurn.Up:
        this.PaintEllipse(g, colorBlend, p, new Rectangle(0, this.Height * (-1)-1, this.Height * 2, this.Height * 2), 90.0f, 90.0f);
        break;
    case PipeTurn.Down:
        this.PaintEllipse(g, colorBlend, p, new Rectangle(0, 0, this.Height * 2, this.Height * 2), 180.0f, 90.0f);
        break;
    default:
        this.PaintRectangle(g, linearGradientBrush, p, new Rectangle(-1, 0, this.Height+1, this.Height));
        break;
}

//繪製右部分
switch (this.pipeTurnRight)
{
    case PipeTurn.Up:
        this.PaintEllipse(g, colorBlend, p, new Rectangle(this.Width - this.Height * 2, this.Height * (-1)-1, this.Height * 2, this.Height * 2), 0.0f, 90.0f);
        break;
    case PipeTurn.Down:
        this.PaintEllipse(g, colorBlend, p, new Rectangle(this.Width - this.Height * 2, 0, this.Height * 2, this.Height * 2), 270.0f, 90.0f);
        break;
    default:
        this.PaintRectangle(g, linearGradientBrush, p, new Rectangle(this.Width - this.Height, 0, this.Height, this.Height));
        break;
}

//繪製中間
if (this.Width > this.Height * 2)
{
    this.PaintRectangle(g, linearGradientBrush, p, new Rectangle(this.Height - 1, 0, this.Width - this.Height * 2 + 2, this.Height));
}
//流動條路徑
GraphicsPath path = new GraphicsPath();

//虛線路徑—左邊
switch (this.pipeTurnLeft)
{
    case PipeTurn.Up:
        path.AddArc(new Rectangle(this.Height / 2, this.Height / 2 * (-1) -1, this.Height, this.Height), 181.0f, -91.0f);
        break;
    case PipeTurn.Down:
        path.AddArc(new Rectangle(this.Height / 2, this.Height / 2, this.Height, this.Height), 180.0f, 90.0f);
        break;
    default:
        path.AddLine(-1, this.Height / 2, this.Height+1, this.Height / 2);
        break;
}

//虛線路徑—中間
if (this.Width > this.Height * 2)
{
    path.AddLine(this.Height, this.Height / 2, this.Width - this.Height -1, this.Height / 2);
}

//虛線路徑—右邊
switch (this.pipeTurnRight)
{
    case PipeTurn.Up:
        path.AddArc(new Rectangle(this.Width - 1 - this.Height * 3 / 2, -this.Height / 2-1 , this.Height, this.Height), 88f, -91.0f);
        break;
    case PipeTurn.Down:
        path.AddArc(new Rectangle(this.Width - 1 - this.Height * 3 / 2, this.Height / 2, this.Height, this.Height), 270.0f, 90.0f);
        break;
    default:
        path.AddLine(this.Width - this.Height, this.Height / 2, this.Width , this.Height / 2);
        break;
}

//畫虛線,關鍵用筆和路徑來
Pen pen = new Pen(this.flowColor, this.flowWidth);
pen.DashStyle = DashStyle.Custom;
pen.DashPattern = new float[]
{
    flowLength,flowLengthGap
};
pen.DashOffset = this.startOffset;
g.DrawPath(pen, path);

格式都是一樣的,掌握關鍵代碼,肝就對了。

End


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

-Advertisement-
Play Games
更多相關文章
  • 單欄位和多欄位重寫hashcode 在 Java 中,重寫 hashCode 方法的場景通常與對象的哈希值計算有關,特別是在使用哈希表(如 HashMap, HashSet 等)時。下麵是你提供的兩種 hashCode 實現的具體使用場景分析: 1. 第一種實現 @Override public b ...
  • OpenCV(Open Source Computer Vision Library)是一個開源的電腦視覺和機器學習軟體庫,旨在提供一個跨平臺的、易於使用的、快速執行的電腦視覺介面。如果只是簡單的使用,其實不必要像筆者這樣使用源代碼進行構建,直接使用官方提供的二進位安裝包即可。一般來說,需要從源 ...
  • 寫在前面 4002 字 | 陪伴 | 親密關係 | 患難與共 《理想雪》系列故事均為架空世界觀,所有人名、地名等與現實世界無任何關聯。 該系列只且僅只為了說明,小說作者在該情境下會誕生的想法和採取的行動,以及背後的世界觀、價值觀和人生觀。因此將具有強烈的個人風格。 未經授權,禁止轉載。僅供小範圍內閱 ...
  • 帶箭頭的直線就是有方向的直線,既可以用來表示矢量,也可以用來標記某個關鍵位置。manim中提供了4種常用的帶箭頭的直線模塊: Arrow:單箭頭的直線 DoubleArrow:雙箭頭的直線 LabeledArrow:帶標簽的直線 Vector:向量 其中,DoubleArrow,LabeledArr ...
  • 六,Spring Boot 容器中 Lombok 插件的詳細使用,簡化配置,提高開發效率 @目錄六,Spring Boot 容器中 Lombok 插件的詳細使用,簡化配置,提高開發效率1. Lombok 介紹2. Lombok 常用註解2.1 @ToString2.2 @Setter2.3 @Dat ...
  • Springboot黑馬點評(3)——優惠券秒殺 【還剩Redisson的最後兩節沒測試 後續補上】 另外,後期單獨整理一份關於分散式鎖筆記 1 優惠券秒殺實現 1.1 用戶-優惠券訂單設計 1.1.1 全局ID生成器 使用資料庫自增ID作為訂單ID存在問題 1.1.2 考慮全局唯一ID生成邏輯 時 ...
  • 原文地址https://blog.fanscore.cn/a/61/ 1. wssh 1.1 開發背景 公司內部的發佈系統提供一個連接到k8s pod的web終端,可以在網頁中連接到k8s pod內。實現原理大概為通過websocket協議代理了k8s pod ssh,然後在前端通過xterm.js ...
  • 我們在某寶或某多多上搶購商品時,如果只是下了訂單但沒有進行實際的支付,那在訂單頁面會有一個支付倒計時,要是過了這個時間點那麼訂單便會自動取消。在這樣的業務場景中,一般情況下就會使用到延時隊列。 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...