代码如下。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Ink; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Animation; 12 using System.Windows.Shapes; 13 using System.Windows.Media.Imaging; 14 15 16 17 namespace customCom 18 { 19 20 public partial class RightInstrumentpanel : UserControl 21 { 22 private int iMaxValue = 30; //刻度最大值 23 private int iMinValue = 0; //刻度最小值 24 private int iMaxMarkScale = 10; //刻度进制 25 private int iMinMarkNumber = 5; //刻度数 26 private float iPointValue = 0; //液面初始化 27 private string iBackGroup = "RightInstrumentpanel_Background.png"; //背景图片 28 private double iBaseLength = 150; //基础边长 29 30 // 31 public RightInstrumentpanel() 32 { 33 InitializeComponent(); 34 MainPaint(); 35 } 36 //刻度最大值 37 public int MaxValue 38 { 39 get { return iMaxValue; } 40 set 41 { 42 iMaxValue = value; 43 MainPaint(); 44 } 45 } 46 47 //刻度最小值 48 public int MinValue 49 { 50 get { return iMinValue; } 51 set 52 { 53 iMinValue = value; 54 MainPaint(); 55 } 56 } 57 58 //刻度数 59 public int MinMarkNumber 60 { 61 get { return iMinMarkNumber; } 62 set 63 { 64 iMinMarkNumber = value; 65 MainPaint(); 66 } 67 } 68 69 //最大角度 70 public int MaxAngle 71 { 72 get { return iMinValue; } 73 set 74 { 75 iMinValue = value; 76 MainPaint(); 77 } 78 } 79 80 //指针值初始化 81 public float PointValue 82 { 83 get { return iPointValue; } 84 set 85 { 86 iPointValue = value; 87 MainPaint(); 88 } 89 } 90 91 92 public void MainPaint() 93 { 94 mainCanvas.Children.Clear(); 95 //与边框绑定 96 double iCurrentLength = mainControl.Width; 97 //加载背景图 98 mainCanvas.Background = new ImageBrush() 99 { 100 ImageSource = new BitmapImage(new Uri(iBackGroup, UriKind.RelativeOrAbsolute)) 101 }; 102 103 //加载大刻度 104 mainCanvas.Children.Clear(); 105 for (int i = 0; i <= (iMaxValue - iMinValue) / iMaxMarkScale; i++) 106 { 107 //加载刻度 108 Image imTemp = new Image(); 109 imTemp.Source = new BitmapImage(new Uri("RightInstrumentpanel_MarkImage.png", UriKind.RelativeOrAbsolute)); 110 mainCanvas.Children.Add(imTemp); 111 imTemp.Width = 30; 112 imTemp.Height = 20; 113 114 //刻度位置 115 TranslateTransform ttForm = new TranslateTransform(); 116 ttForm.X = 150 * iCurrentLength / iBaseLength; 117 ttForm.Y = (230 * i) * iCurrentLength / iBaseLength; 118 imTemp.RenderTransform = ttForm; 119 120 //加载刻度数值 121 Label imlabelTemp = new Label(); 122 imlabelTemp.Content =iMaxValue - iMaxMarkScale * i; 123 mainCanvas.Children.Add(imlabelTemp); 124 imlabelTemp.Height =20; 125 126 //刻度数值位置 127 ttForm = new TranslateTransform(); 128 ttForm.X = 170 * iCurrentLength / iBaseLength; 129 ttForm.Y = (230 * i) * iCurrentLength / iBaseLength; 130 imlabelTemp.RenderTransform = ttForm; 131 132 ////加载小刻度 133 //float fMinMarkMinAngle = iMaxValue + (iMaxValue-iMinValue)/ ((iMaxValue - iMinValue) / iMaxMarkScale) * i; 134 //float fMinMarkScale = (iMaxValue - iMinValue)/ ((iMaxValue - iMinValue) / iMaxMarkScale) / (iMinMarkNumber + 1); 135 //for (int j = 0; j < iMinMarkNumber; j++) 136 //{ 137 // if (i < (iMaxValue - iMinValue) / iMaxMarkScale) 138 // { 139 // //加载刻度 140 // Image imSmallTemp = new Image(); 141 // imSmallTemp.Source = new BitmapImage(new Uri("RightInstrumentpanel_MarkImage.png", UriKind.RelativeOrAbsolute)); 142 // mainCanvas.Children.Add(imSmallTemp); //图片大小 143 // imSmallTemp.Width = 20; 144 // imSmallTemp.Height = 10; 145 146 // //刻度位置 147 // TranslateTransform ttSmallForm = new TranslateTransform(); 148 // ttSmallForm.X = 150 * iCurrentLength / iBaseLength; 149 // ttSmallForm.Y = 6 * (fMinMarkMinAngle + fMinMarkScale * (j-26)); 150 151 // //调用函数 152 // TransformGroup tfSmallGroup = new TransformGroup(); 153 // tfSmallGroup.Children.Add(ttSmallForm); 154 // imSmallTemp.RenderTransform = tfSmallGroup; 155 // } 156 //} 157 158 //加载液面 159 Image imPoint = new Image(); 160 //调用液面图片 161 imPoint.Source = new BitmapImage(new Uri("RightInstrumentpanel_PointImage.png", UriKind.RelativeOrAbsolute)); 162 mainCanvas.Children.Add(imPoint); 163 imPoint.Width = 38; 164 165 //液面上下浮动位置 166 ScaleTransform stPoint = new ScaleTransform(); 167 stPoint.ScaleX = 1; 168 stPoint.ScaleY = (200*(iPointValue/iMaxValue))/290; 169 170 //液面坐标 171 TranslateTransform ttPointForm = new TranslateTransform(); 172 ttPointForm.X = 1; 173 ttPointForm.Y = 200 * (1 - iPointValue / iMaxValue); 174 175 //调用函数 176 TransformGroup tfPointGroup = new TransformGroup(); 177 tfPointGroup.Children.Add(stPoint); 178 tfPointGroup.Children.Add(ttPointForm); 179 imPoint.RenderTransform = tfPointGroup; 180 } 181 } 182 //绑定刻度与背景图 183 private void mainCanvas_SizeChanged(object sender, SizeChangedEventArgs e) 184 { 185 MainPaint(); 186 } 187 } 188 }
温度计边框绑定不上,拖动大小的时候刻度数值不能跟着温度计边框移动。小刻度数值不能正确排列,算法我实在弄不出来了。。。液面位置对应不到刻度上。三个问题。。。请大家帮帮我。恢复的时候请把源码给我。。。万分感谢。
如果不明白我就把源码发给你。
自己已经完了!