WPF工控組態軟體之冷卻塔和空氣壓縮機開發

来源:https://www.cnblogs.com/hsiang/archive/2022/10/19/16804733.html
-Advertisement-
Play Games

WPF以其豐富靈活的控制項樣式設計,相較於WinForm而言,一直是工控組態軟體的寵兒。經過上一篇文章的學習,已經對WPF開發工控組態軟體有了一個基本的瞭解, 今天繼續學習冷卻塔和空氣壓縮機的開發,僅供學習分享使用,如有不足之處,還請指正。 ...


WPF以其豐富靈活的控制項樣式設計,相較於WinForm而言,一直是工控組態軟體的寵兒。經過上一篇文章的學習,已經對WPF開發工控組態軟體有了一個基本的瞭解, 今天繼續學習冷卻塔和空氣壓縮機的開發,僅供學習分享使用,如有不足之處,還請指正。

設計知識點

關於本示例中,涉及的知識點,如下所示:

  • 自定義用戶控制項,用戶可以根據業務需要自定義控制項,將普通的控制項進行組合,封裝,以滿足特定的功能,並達到復用的目的。
  • WPF形狀,動畫,可以通過選擇,移動,變形等相關功能,改變控制項的呈現形狀。
  • 依賴屬性,WPF可以通過依賴屬性進行數據的綁定,實現UI與業務邏輯的解耦。

冷卻塔CoolingTower

冷卻塔組成拆解

關於冷卻塔的組成部分,經過拆解如下所示:

  1. 頂部一個經過旋轉和縮放的風扇(Path)
  2. 一個橢圓形(Ellipse)和一個路徑形狀(Path),使得橢圓看起來像立體一樣
  3. 一個不規則形狀多邊形(Ploygon)
  4. 一個四邊形(Border),兩個圓形指示燈(Border),兩個四邊形底座(Border),表示前景和側面。

經過以上分析,不難發現,複雜的圖形均是由簡單的圖形組合起來的。

冷卻塔示例截圖

冷卻塔正常運轉時,綠燈亮且風扇轉動,異常時紅燈亮,且風扇停止。截圖如下所示:

冷卻塔源碼

冷卻塔源碼分為三個組成部分,如下所示:

1. 控制項佈局

控制項佈局主要是排版佈局形狀等內容。如下所示:

  1 <Viewbox HorizontalAlignment="Center" VerticalAlignment="Center">
  2     <Canvas Width="205" Height="245" Margin="5">
  3         <Polygon Points="0,65 40,10 205,10 180,65 " Fill="#EEE"></Polygon>
  4         <Path Data="M0 0 A60 15 0 0 0 120 0 L120 25 A60 15 0 0 1 0 25" Canvas.Left="45" Canvas.Top="15">
  5             <Path.Fill>
  6                 <LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
  7                     <GradientStop Color="#FFD6D6D6" Offset="0"></GradientStop>
  8                     <GradientStop Color="White" Offset="0.52"></GradientStop>
  9                     <GradientStop Color="#FFD6D6D6" Offset="1"></GradientStop>
 10                 </LinearGradientBrush>
 11             </Path.Fill>
 12         </Path>
 13         <Ellipse Width="120" Height="30" Fill="#CCC" Canvas.Left="45"></Ellipse>
 14         <Border Canvas.Left="52" Canvas.Top="-38">
 15             <Viewbox Width="105" Height="105" RenderTransformOrigin="0.5,0.5">
 16                 <Viewbox.RenderTransform>
 17                     <TransformGroup>
 18                         <RotateTransform Angle="20" x:Name="rt"></RotateTransform>
 19                         <ScaleTransform ScaleY="0.25"></ScaleTransform>
 20                     </TransformGroup>
 21                 </Viewbox.RenderTransform>
 22                 <Path Stretch="Fill" Data="M261.851429 528.822857c-43.885714-24.868571-84.845714-23.405714-121.417143 5.851429-35.108571 26.331429-49.737143 62.902857-43.885715 106.788571 5.851429 38.034286 19.017143 74.605714 40.96 108.251429 21.942857 35.108571 46.811429 59.977143 76.068572 74.605714 78.994286 40.96 147.748571 29.257143 207.725714-35.108571 19.017143-20.48 33.645714-43.885714 46.811429-73.142858 14.628571-32.182857 23.405714-61.44 24.868571-90.697142 0-14.628571 7.314286-21.942857 19.017143-21.942858s19.017143 5.851429 24.868571 16.091429c17.554286 51.2 14.628571 99.474286-10.24 143.36-24.868571 43.885714-21.942857 84.845714 4.388572 119.954286 26.331429 35.108571 62.902857 49.737143 106.788571 42.422857 38.034286-5.851429 74.605714-19.017143 108.251429-40.96 35.108571-21.942857 59.977143-46.811429 74.605714-76.068572 40.96-78.994286 29.257143-147.748571-36.571428-206.262857-20.48-19.017143-43.885714-35.108571-73.142858-48.274285-32.182857-14.628571-61.44-23.405714-90.697142-24.868572-14.628571 0-21.942857-7.314286-21.942858-19.017143s5.851429-20.48 17.554286-23.405714c20.48-7.314286 40.96-11.702857 62.902857-11.702857 27.794286 0 54.125714 7.314286 78.994286 20.48 43.885714 24.868571 84.845714 23.405714 121.417143-4.388572 35.108571-26.331429 49.737143-62.902857 43.885714-106.788571-5.851429-38.034286-19.017143-74.605714-40.96-108.251429-21.942857-35.108571-46.811429-59.977143-76.068571-74.605714-78.994286-40.96-147.748571-29.257143-207.725715 35.108572-19.017143 20.48-33.645714 45.348571-46.811428 73.142857-14.628571 32.182857-23.405714 62.902857-24.868572 90.697143 0 13.165714-7.314286 20.48-19.017142 21.942857s-20.48-5.851429-24.868572-16.091429c-7.314286-20.48-10.24-40.96-10.24-64.365714 0-27.794286 7.314286-54.125714 20.48-78.994286 24.868571-43.885714 21.942857-84.845714-4.388571-119.954286-26.331429-35.108571-61.44-49.737143-105.325715-43.885714-38.034286 5.851429-74.605714 19.017143-108.251428 40.96-35.108571 21.942857-59.977143 46.811429-76.068572 76.068572-40.96 78.994286-29.257143 147.748571 36.571429 207.725714 20.48 19.017143 45.348571 35.108571 73.142857 48.274286 32.182857 14.628571 61.44 21.942857 90.697143 23.405714 14.628571 0 21.942857 7.314286 21.942857 19.017143s-5.851429 20.48-17.554286 24.868571c-49.737143 17.554286-98.011429 14.628571-141.897142-10.24m279.405714-46.811428c8.777143 8.777143 11.702857 17.554286 11.702857 29.257142s-4.388571 21.942857-11.702857 30.72c-8.777143 7.314286-17.554286 11.702857-29.257143 11.702858s-21.942857-4.388571-30.72-11.702858c-8.777143-8.777143-11.702857-19.017143-11.702857-30.72s4.388571-21.942857 11.702857-29.257142c8.777143-8.777143 19.017143-13.165714 30.72-13.165715 11.702857 1.462857 20.48 4.388571 29.257143 13.165715z" Fill="#EEE">
 23                 </Path>
 24             </Viewbox>
 25         </Border>
 26         <Grid Width="180" Height="180" Canvas.Top="65" Background="#E0E0E0">
 27             <Border VerticalAlignment="Top" Height="140" BorderThickness="10">
 28                 <Border.Background>
 29                     <DrawingBrush TileMode="Tile" ViewportUnits="Absolute" Viewport="1,0,25,1">
 30                         <DrawingBrush.Drawing>
 31                             <GeometryDrawing>
 32                                 <GeometryDrawing.Pen>
 33                                     <Pen Brush="#EEE"></Pen>
 34                                 </GeometryDrawing.Pen>
 35                                 <GeometryDrawing.Geometry>
 36                                     <PathGeometry>
 37                                         <PathFigure>
 38                                             <LineSegment Point="10,0"></LineSegment>
 39                                             <LineSegment Point="10,10"></LineSegment>
 40                                         </PathFigure>
 41                                     </PathGeometry>
 42                                 </GeometryDrawing.Geometry>
 43                             </GeometryDrawing>
 44                         </DrawingBrush.Drawing>
 45                     </DrawingBrush>
 46                 </Border.Background>
 47             </Border>
 48             <Grid Height="50" VerticalAlignment="Bottom">
 49                 <Grid.ColumnDefinitions>
 50                     <ColumnDefinition></ColumnDefinition>
 51                     <ColumnDefinition></ColumnDefinition>
 52                 </Grid.ColumnDefinitions>
 53                 <Border Background="DarkGray" Grid.Column="0" Margin="5,0"></Border>
 54                 <Border Background="DarkGray" Grid.Column="1" Margin="5,0"></Border>
 55             </Grid>
 56          </Grid>
 57 
 58         <Grid Width="180" Height="180" Canvas.Top="65" Canvas.Left="180" Background="#E0E0E0">
 59             <Grid.RenderTransform>
 60                 <TransformGroup>
 61                     <ScaleTransform ScaleX="0.139"></ScaleTransform>
 62                     <SkewTransform AngleY="-65.5"></SkewTransform>
 63                 </TransformGroup>
 64             </Grid.RenderTransform>
 65             <Border VerticalAlignment="Top" Height="140" BorderThickness="10">
 66                 <Border.Background>
 67                     <DrawingBrush TileMode="Tile" ViewportUnits="Absolute" Viewport="1,0,25,1">
 68                         <DrawingBrush.Drawing>
 69                             <GeometryDrawing>
 70                                 <GeometryDrawing.Pen>
 71                                     <Pen Brush="#EEE"></Pen>
 72                                 </GeometryDrawing.Pen>
 73                                 <GeometryDrawing.Geometry>
 74                                     <PathGeometry>
 75                                         <PathFigure>
 76                                             <LineSegment Point="10,0"></LineSegment>
 77                                             <LineSegment Point="10,10"></LineSegment>
 78                                         </PathFigure>
 79                                     </PathGeometry>
 80                                 </GeometryDrawing.Geometry>
 81                             </GeometryDrawing>
 82                         </DrawingBrush.Drawing>
 83                     </DrawingBrush>
 84                 </Border.Background>
 85             </Border>
 86             <Grid Height="50" VerticalAlignment="Bottom">
 87                 <Grid.ColumnDefinitions>
 88                     <ColumnDefinition></ColumnDefinition>
 89                     <ColumnDefinition></ColumnDefinition>
 90                 </Grid.ColumnDefinitions>
 91                 <Border Background="DarkGray" Grid.Column="0" Margin="5,0"></Border>
 92                 <Border Background="DarkGray" Grid.Column="1" Margin="5,0"></Border>
 93             </Grid>
 94         </Grid>
 95 
 96         <Border Width="18" Height="18" CornerRadius="10" Canvas.Left="150" Canvas.Top="80">
 97             <Border.Background>
 98                 <RadialGradientBrush>
 99                     <GradientStop Color="Gray" Offset="0.6" x:Name="gsGreen"></GradientStop>
100                     <GradientStop Color="White"></GradientStop>
101                 </RadialGradientBrush>
102             </Border.Background>
103         </Border>
104         <Border Width="18" Height="18" CornerRadius="10" Canvas.Left="150" Canvas.Top="105">
105             <Border.Background>
106                 <RadialGradientBrush>
107                     <GradientStop Color="Gray" Offset="0.6" x:Name="gsRed"></GradientStop>
108                     <GradientStop Color="White"></GradientStop>
109                 </RadialGradientBrush>
110             </Border.Background>
111         </Border>
112     </Canvas>
113 </Viewbox>

2. 狀態管理

冷卻塔有兩個狀態,正常運行狀態和異常狀態,分別對應不同的呈現。如下所示:

 1 <VisualStateManager.VisualStateGroups>
 2     <VisualStateGroup>
 3         <VisualState x:Name="normalState">
 4             <Storyboard>
 5                 <DoubleAnimation Duration="0:0:0.5" From="0" To="-360" RepeatBehavior="Forever" Storyboard.TargetName="rt" Storyboard.TargetProperty="Angle"></DoubleAnimation>
 6                 <ColorAnimationUsingKeyFrames Storyboard.TargetName="gsGreen" Storyboard.TargetProperty="Color">
 7                     <DiscreteColorKeyFrame Value="Green" KeyTime="0"></DiscreteColorKeyFrame>
 8                 </ColorAnimationUsingKeyFrames>
 9             </Storyboard>
10         </VisualState>
11         <VisualState x:Name="errorState">
12             <Storyboard>
13                 <ColorAnimationUsingKeyFrames Storyboard.TargetName="gsRed" RepeatBehavior="Forever" Storyboard.TargetProperty="Color" Duration="0:0:1">
14                     <DiscreteColorKeyFrame Value="Red" KeyTime="0:0:0.5"></DiscreteColorKeyFrame>
15                     <DiscreteColorKeyFrame Value="Gray" KeyTime="0:0:1"></DiscreteColorKeyFrame>
16                 </ColorAnimationUsingKeyFrames>
17             </Storyboard>
18         </VisualState>
19     </VisualStateGroup>
20 </VisualStateManager.VisualStateGroups>

3. 依賴屬性

在本示例中,依賴屬性主要用於控制狀態,如下所示:

 1 namespace WpfControl.UserControls
 2 {
 3     /// <summary>
 4     /// CoolingTower.xaml 的交互邏輯
 5     /// </summary>
 6     public partial class CoolingTower : UserControl
 7     {
 8         public RunningState RunningState
 9         {
10             get { return (RunningState)GetValue(RunningStateProperty); }
11             set { SetValue(RunningStateProperty, value); }
12         }
13 
14         // Using a DependencyProperty as the backing store for RunningState.  This enables animation, styling, binding, etc...
15         public static readonly DependencyProperty RunningStateProperty =
16             DependencyProperty.Register("RunningState", typeof(RunningState), typeof(CoolingTower), new PropertyMetadata(default(RunningState), RunningStateChangedCallback));
17 
18         private static void RunningStateChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
19         {
20             RunningState value = (RunningState)e.NewValue;
21             VisualStateManager.GoToState(d as CoolingTower, value == RunningState.Normal ? "normalState" : "errorState", false);
22         }
23         public CoolingTower()
24         {
25             InitializeComponent();
26         }
27     }
28 }

4. 調用示例

組件寫好後,調用非常簡單,如下所示:

 1 <Window x:Class="WpfControl.TestWindow2"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfControl"
 7         xmlns:uctrl="clr-namespace:WpfControl.UserControls"
 8         mc:Ignorable="d"
 9         Title="冷卻塔" Height="450" Width="800">
10     <Grid>
11         <Grid.ColumnDefinitions>
12             <ColumnDefinition></ColumnDefinition>
13             <ColumnDefinition></ColumnDefinition>
14         </Grid.ColumnDefinitions>
15         <uctrl:CoolingTower Grid.Column="0" RunningState="Error" Width="100" Height="100"></uctrl:CoolingTower>
16         <uctrl:CoolingTower Grid.Column="1" RunningState="Normal" Width="100" Height="100"></uctrl:CoolingTower>
17     </Grid>
18 </Window>

 

空氣壓縮機AirCompresser

空氣壓縮機功能拆解

關於空氣壓縮式機的組成部分,經過拆解如下所示:

  1. 左側一個四邊形(Path)
  2. 頂部一個四邊形(Path)
  3. 前景一個四邊形(Path),兩條線(Line)
  4. 兩個小圓點(Ellipse)
  5. 底部一個四邊形(Border)
  6. 右上角指示燈(Ellipse)

經過以上分析,不難發現,空氣壓縮機均是由簡單的圖形組合起來的。

空氣壓縮機示例截圖

空氣壓縮式機同樣包括兩種狀態,正常狀態,綠燈亮和異常狀態,紅燈閃爍。示例截圖如下所示:

空氣壓縮機源碼

空氣壓縮機源碼分為三個組成部分,如下所示:

1. 頁面佈局

頁面佈局主要進行控制項的排版,如下所示:

 1  <Viewbox Stretch="Fill">
 2     <Grid
              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 今天來實踐一下如何用Python對商品庫存繪製餅狀圖進行分析 一、知識點 文件讀寫 基礎語法 字元串處理 文件生成 數據構建 二、效果展示 一目瞭然 三、代碼展示 兄弟們學習python,有時候不知道怎麼學,從哪裡開始學。掌握了基本的一些語法或者做了兩個案例後,不知道下一步怎麼走,不知道如何去學習更 ...
  • 在python中沒有直接針對文件夾的操作方法,可以藉助模塊os,os.path和shutil來操作。在新建文件夾時可以創建一級文件,也可以創建多級文件。 判斷文件夾或者文件是否存在 判斷文件或者文件夾是否存在,可以使用os.path.exists()函數來判斷,其使用方法如下 os.path.exi ...
  • 1.題目名稱:批閱奏章某朝皇帝有大臣n名(1<=n<=1000),分別編號大臣1~n。某日皇帝身體抱恙,奏章堆積如山無法及時一一批閱,便命身旁內侍幫他把奏章按指定順序排序後再閱。於是皇帝親自挑選了幾個值得信賴的重臣併排好序,要求把他們的奏章按排好的順序放到前面,其他的按照編號升序排列即可。現在要求你 ...
  • 在某些情況下,你可能需要在Microsoft Word中插入上標和下標。例如,當你正在創建一個涉及科學公式的學術文件時。 ...
  • 在進行滲透過程中,也可以藉助一些商用或者非商業平臺來進行信息搜索及驗證,主要針對郵箱、IP、功能變數名稱、文件md5、apk文件等進行搜索,整理和完善線索信息。為了大家方便, 對目前公開的威脅情報平臺進行搜集,在實際使用過程中建議交叉搜索,以獲得更多的信息。這些平臺都需要實名認證,認證後即可進行查詢。 一、 ...
  • 1 設計模式概述 ​ 軟體設計模式(Software Design Pattern),俗稱設計模式,設計模式是一套被反覆使用的、多數人知曉的、經過分類編目的、代碼設計經驗的總結。它描述了在軟體設計過程中的一些不斷重覆發生的問題,以及該問題的解決方案。也就是說,它是解決特定問題的一系列套路,是前輩們的 ...
  • 1、統一數據返回 使用逆向工程來進行測試,實體,mapper等省略; 1.1 直接使用 RequestResoult 1.1.1 RequestResoult 請求結果返回實體 //統一返回 實體 類 @Data public class RequestResult<T> { //狀態碼 priva ...
  • synchronized作為Java程式員最常用同步工具,很多人卻對它的用法和實現原理一知半解,以至於還有不少人認為synchronized是重量級鎖,性能較差,儘量少用。 但不可否認的是synchronized依然是併發首選工具,連volatile、CAS、ReentrantLock都無法動搖s... ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...