MatrixBlock.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Drawing.Drawing2D;
  11. using IntelligentControlForsx.Service;
  12. using IntelligentControlForsx.Common;
  13. using IntelligentControlForsx.Properties;
  14. namespace IntelligentControlForsx.MyControls
  15. {
  16. public partial class MatrixBlock : UserControl
  17. {
  18. public MatrixBlock()
  19. {
  20. InitializeComponent();
  21. // ControlStyles.UserPaint |
  22. this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);//开启双缓冲
  23. tip = new ToolTip();
  24. // 鼠标进入时显示tips
  25. tip.AutoPopDelay = 10000;
  26. tip.InitialDelay = 200;
  27. tip.ReshowDelay = 200;
  28. tip.ShowAlways = true;
  29. topLineColor= BottomLineColor = BackColor;
  30. }
  31. ToolTip tip;
  32. string deviceId;
  33. string deviceModel;
  34. string stationId;
  35. double windSpeed;
  36. double power;
  37. double status;
  38. bool isLock;
  39. long ts;
  40. // 条纹的颜色
  41. Color topLineColor,BottomLineColor;
  42. Font windturbineTextFont = new Font("微软雅黑", 7.5f);
  43. Font paramTextFont = new Font("微软雅黑", 6.5f);
  44. [Category("Xproperty"), Description("风机名称的字体"), DefaultValue(typeof(Font), "微软雅黑, 7.4f")]
  45. public Font WindTurbineFont
  46. {
  47. get
  48. {
  49. return windturbineTextFont;
  50. }
  51. set
  52. {
  53. windturbineTextFont = value;
  54. this.Invalidate(RecWindturbineText);
  55. }
  56. }
  57. [Category("Xproperty"), Description("风机参数的字体"), DefaultValue(typeof(Font), "微软雅黑, 6.4f")]
  58. public Font ParamTextFont
  59. {
  60. get
  61. {
  62. return paramTextFont;
  63. }
  64. set
  65. {
  66. paramTextFont = value;
  67. this.Invalidate(RecRight);
  68. }
  69. }
  70. [Browsable(true), Category("Xproperty")]
  71. public string DeviceId
  72. {
  73. get
  74. {
  75. return deviceId;
  76. }
  77. set
  78. {
  79. deviceId = value;
  80. Invalidate(RecWindturbineText);
  81. //lblDeviceId.Text = value;
  82. }
  83. }
  84. [Browsable(true), Category("Xproperty")]
  85. public double WindSpeed
  86. {
  87. get
  88. {
  89. return windSpeed;
  90. }
  91. set
  92. {
  93. windSpeed = value;
  94. Invalidate(RecSpeed);
  95. //lblWindSpeed.Text = Convert.ToDouble(value).ToString("0.00") + "m/s";
  96. }
  97. }
  98. [Browsable(true), Category("Xproperty")]
  99. public double Power
  100. {
  101. get
  102. {
  103. return power;
  104. }
  105. set
  106. {
  107. power = value;
  108. Invalidate(RecPower);
  109. //lblPower.Text = Convert.ToDouble(value).ToString("0.00") + "kw";
  110. }
  111. }
  112. [Browsable(true), Category("Xproperty")]
  113. public double Status
  114. {
  115. get
  116. {
  117. return status;
  118. }
  119. set
  120. {
  121. status = value;
  122. this.BackColor = getBackColor(value);
  123. topLineColor = getBackColor(value);
  124. Invalidate();
  125. }
  126. }
  127. public bool IsLock
  128. {
  129. set
  130. {
  131. BottomLineColor = value ? Color.Yellow : topLineColor;
  132. isLock = value;
  133. Invalidate();
  134. }
  135. get { return isLock; }
  136. }
  137. private double lokcValue;
  138. public double LockValue
  139. {
  140. get { return lokcValue; }
  141. set { lokcValue = value; }
  142. }
  143. public string DeviceModel
  144. {
  145. get
  146. {
  147. return deviceModel;
  148. }
  149. set
  150. {
  151. deviceModel = value;
  152. }
  153. }
  154. public string StationId
  155. {
  156. get
  157. {
  158. return stationId;
  159. }
  160. set
  161. {
  162. stationId = value;
  163. }
  164. }
  165. public long Ts
  166. {
  167. get
  168. {
  169. return ts;
  170. }
  171. set
  172. {
  173. ts = value;
  174. }
  175. }
  176. private Color getBackColor(double status)
  177. {
  178. int st = Convert.ToInt32(status);
  179. switch (st)
  180. {
  181. //0-停机-TJTS、 1-上电-SDTS、2-待机-DJTS、3-启动-QDTS、4-并网-BWTS、5-故障-GZTS、6-维护-WHTS、 7-离线-LXTS
  182. case 0: //停机状态
  183. return Color.FromArgb(176, 151, 63);
  184. case 1: //上电状态
  185. return Color.FromArgb(177, 14, 126);
  186. case 2: //待机状态
  187. return Color.FromArgb(15, 141, 106);
  188. case 3: //启动状态
  189. return Color.FromArgb(14, 72, 91);
  190. case 4: //并网状态
  191. return Color.FromArgb(15, 135, 170);
  192. case 5: //故障状态
  193. return Color.FromArgb(170, 15, 59);
  194. case 6: //维护状态
  195. return Color.FromArgb(204, 83, 51);
  196. case 7: //离线状态
  197. return Color.FromArgb(134, 150, 165);
  198. default:
  199. return BackColor;
  200. }
  201. }
  202. #region 区域
  203. private Rectangle RecTopLine
  204. {
  205. get
  206. {
  207. return new Rectangle(0, 0, this.Width, 2);
  208. }
  209. }
  210. private Rectangle RecWindturbineText
  211. {
  212. get
  213. {
  214. return new Rectangle(0, 2, Width * 4 / 9, (this.Height - 4)-12);
  215. }
  216. }
  217. private Rectangle RecRight
  218. {
  219. get
  220. {
  221. return new Rectangle(Width * 4 / 9, 2, Width * 5 / 9, this.Height - 4);
  222. }
  223. }
  224. private Rectangle RecSpeed
  225. {
  226. get
  227. {
  228. return new Rectangle(Width * 4 / 9, 2, Width * 5 / 9, (this.Height - 4) / 2);
  229. }
  230. }
  231. private Rectangle RecPower
  232. {
  233. get
  234. {
  235. // 长度总是不够
  236. //return new Rectangle(Width * 4 / 9, Height / 2, Width * 5 / 9, (this.Height - 4) / 2);
  237. return new Rectangle(12 + 3, Height / 2, Width - 15, (Height - 4) / 2);
  238. }
  239. }
  240. private Rectangle RecBottomLine
  241. {
  242. get
  243. {
  244. return new Rectangle(0, this.Height - 2, Width, 2);
  245. }
  246. }
  247. #endregion
  248. #region 事件
  249. public event EventHandler MyMouseDown;
  250. public event EventHandler MyMouseUp;
  251. private void MatrixBlock_DoubleClick(object sender, EventArgs e)
  252. {
  253. string windturbineId = CommonMethod.GetLongWindturbineId(this.deviceId);
  254. string powerStationId = this.stationId;
  255. NavService.Instance.NavForWindturbine("pictureBoxParamters", windturbineId, powerStationId);
  256. }
  257. #endregion
  258. private string getBeginTime(long ts)
  259. {
  260. if (ts < 10000)
  261. return "";
  262. DateTime dt = CommonMethod.ConvertIntDateTime(ts);
  263. return dt.ToString("yyyy-MM-dd hh:mm:ss");
  264. }
  265. private string getTimeSpan(long ts)
  266. {
  267. if (ts < 10000)
  268. return "";
  269. DateTime dt = CommonMethod.ConvertIntDateTime(ts);
  270. TimeSpan tts = DateTime.Now - dt;
  271. return (long)tts.TotalMinutes + "";
  272. }
  273. public bool testShow(double status1, bool autoFilter, double minSpeed = 0, double maxSpeed = 0, double minPower = 0, double maxPower = 0)
  274. {
  275. bool result = true;
  276. if (status1 == 9)
  277. {
  278. if (autoFilter)
  279. {
  280. if (minSpeed != 0 || maxSpeed != 0)
  281. {
  282. if (minPower == 0 && maxPower == 0)
  283. {
  284. if (windSpeed >= minSpeed && windSpeed <= maxSpeed)
  285. result = true;
  286. else
  287. result = false;
  288. }
  289. else
  290. {
  291. if (windSpeed >= minSpeed && windSpeed <= maxSpeed &&
  292. power >= minPower && power <= maxPower)
  293. result = true;
  294. else
  295. result = false;
  296. }
  297. }
  298. else
  299. {
  300. if (power >= minPower && power <= maxPower)
  301. result = true;
  302. else
  303. result = false;
  304. }
  305. }
  306. }
  307. else if (status1 == status)
  308. {
  309. if (autoFilter)
  310. {
  311. if (windSpeed >= minSpeed && windSpeed <= maxSpeed &&
  312. power >= minPower && power <= maxPower)
  313. result = true;
  314. else
  315. result = false;
  316. }
  317. }
  318. else
  319. {
  320. result = false;
  321. }
  322. return result;
  323. }
  324. protected override void OnPaint(PaintEventArgs e)
  325. {
  326. // 先在内存上创建一个Graphics对象,然后画出所有图形,之后绘制在控件上
  327. Rectangle rect = e.ClipRectangle;
  328. BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
  329. BufferedGraphics myBuffer = currentContext.Allocate(e.Graphics, e.ClipRectangle);
  330. Graphics g = myBuffer.Graphics;
  331. g.SmoothingMode = SmoothingMode.HighQuality;
  332. g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
  333. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  334. g.Clear(this.BackColor);
  335. // 顶部线
  336. SolidBrush tmpBrush = new SolidBrush(topLineColor);
  337. g.FillRectangle(tmpBrush, RecTopLine);
  338. // 文字格式
  339. StringFormat strFormat = new StringFormat();//文本格式
  340. strFormat.LineAlignment = StringAlignment.Center;//垂直居中
  341. strFormat.Alignment = StringAlignment.Center;//水平
  342. // id
  343. tmpBrush = new SolidBrush(Color.White);
  344. g.DrawString(DeviceId, WindTurbineFont, tmpBrush, RecWindturbineText, strFormat);
  345. // 风速和功率
  346. strFormat.Alignment = StringAlignment.Far;//水平居右
  347. g.DrawString(WindSpeed.ToString("F2") + "m/s", ParamTextFont, tmpBrush, RecSpeed, strFormat);
  348. g.DrawString(Power.ToString("F2") + "kWh", ParamTextFont, tmpBrush, RecPower, strFormat);
  349. // 底部线
  350. tmpBrush = new SolidBrush(BottomLineColor);
  351. g.FillRectangle(tmpBrush, RecBottomLine);
  352. // time 图标
  353. g.DrawImage(Resources.time, 1, Height - 13, 12, 12);
  354. base.OnPaint(e);
  355. myBuffer.Render(e.Graphics);
  356. myBuffer.Dispose();//释放资源
  357. g.Dispose();
  358. }
  359. }
  360. }