BadgeInfoPanel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using Svg;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Uestc.Auto6.Dso.Core;
  12. using Uestc.Auto6.Dso.ComModel;
  13. using Uni_Trend.MSO7000X.UserControls;
  14. namespace Uestc.Auto6.Dso.U2
  15. {
  16. public partial class BadgeInfoPanel : UserControl
  17. {
  18. private Point _MousePreviousPoint;
  19. private Int32 _ScrollVal = 1;
  20. public BadgeInfoPanel()
  21. {
  22. InitializeComponent();
  23. PnlBadge.VerticalScroll.Enabled = false;
  24. SetScrollArrowSvg(BtnLeftArrow, true);
  25. SetScrollArrowState(BtnLeftArrow, false);
  26. SetScrollArrowSvg(BtnRightArrow, false);
  27. SetScrollArrowState(BtnRightArrow, false);
  28. ShowArrowButton(false);
  29. Application.AddMessageFilter(new MouseDragMsgFilter(PnlBadge, PnlBadge_MouseUp, PnlBadge_MouseDown, PnlBadge_MouseMove, PnlBadge_MouseWheel));
  30. PnlBadge.SizeChanged += (_, _) =>
  31. {
  32. if (PnlBadge.Controls.Count > 0)
  33. {
  34. PnlBadge.SuspendLayout();
  35. var dist = PnlBadge.Controls.Cast<Control>().Max(o => o.Right) - PnlBadge.Controls.Cast<Control>().Min(o => o.Left) + ChildWidthInterval;
  36. if((Program.Oscilloscope.View as DsoForm).WindowState != FormWindowState.Minimized)
  37. ShowArrowButton(dist > Width);
  38. PnlBadge.ResumeLayout();
  39. //var width = ((TableLayoutPanel)Parent).GetColumnWidths().Skip(1).Sum();
  40. ChangeHScrollRange(dist);
  41. }
  42. };
  43. }
  44. protected new Boolean DesignMode
  45. {
  46. get
  47. {
  48. Boolean rtnflag = false;
  49. #if DEBUG
  50. rtnflag = DesignTimeHelper.InDesignMode(this);
  51. #endif
  52. return rtnflag;
  53. }
  54. }
  55. protected override CreateParams CreateParams
  56. {
  57. get
  58. {
  59. CreateParams cp = base.CreateParams;
  60. cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
  61.                 return cp;
  62. }
  63. }
  64. private Int32 _ChildWidthInterval = 5;
  65. [Bindable(true), DefaultValue(5), Category("CatControl"), Description("子控件宽度间隔")]
  66. public Int32 ChildWidthInterval
  67. {
  68. get => _ChildWidthInterval;
  69. set
  70. {
  71. if (value >= 0 && _ChildWidthInterval != value)
  72. {
  73. _ChildWidthInterval = value;
  74. }
  75. }
  76. }
  77. [Bindable(true), DefaultValue(typeof(Color), "red"), Category("CatControl"), Description("控件背景色")]
  78. public new Color BackColor
  79. {
  80. get => TlpContainer.BackColor;
  81. set
  82. {
  83. TlpContainer.BackColor = value;
  84. PnlBadge.BackColor = value;
  85. base.BackColor = value;
  86. }
  87. }
  88. [Bindable(true), DefaultValue(0), Category("CatControl"), Description("左右箭头按钮宽度")]
  89. public int ScrollArrowWidth
  90. {
  91. get => BtnLeftArrow.Width;
  92. set
  93. {
  94. if (value >= 0 && BtnLeftArrow.Width != value)
  95. BtnLeftArrow.Width = BtnRightArrow.Width = value;
  96. }
  97. }
  98. [Bindable(true), DefaultValue(typeof(Color), "red"), Category("CatControl"), Description("左右箭头按钮背景色")]
  99. public Color ScrollArrowBackColor
  100. {
  101. get => BtnRightArrow.BackColor;
  102. set
  103. {
  104. BtnRightArrow.BackColor = value;
  105. BtnLeftArrow.BackColor = value;
  106. }
  107. }
  108. [Bindable(true), DefaultValue(typeof(Color), "red"), Category("CatControl"), Description("左右箭头按钮鼠标进入时背景色")]
  109. public Color ScrollArrowMouseInBackColor
  110. {
  111. get => BtnRightArrow.MouseinBackColor;
  112. set
  113. {
  114. BtnRightArrow.MouseinBackColor = value;
  115. BtnLeftArrow.MouseinBackColor = value;
  116. BtnRightArrow.PressedBackColor = value;
  117. BtnLeftArrow.PressedBackColor = value;
  118. }
  119. }
  120. public IBadgeView GetBadge(ChannelId id)
  121. {
  122. return PnlBadge.Controls.Cast<IBadgeView>().FirstOrDefault(ctrl => ctrl.Presenter.Id == id);
  123. }
  124. public IBadgeView GetBadge(IBadge badge)
  125. {
  126. return PnlBadge.Controls.Cast<IBadgeView>().FirstOrDefault(ctrl => ctrl.Presenter == badge);
  127. }
  128. public void AddBadge(Control badge)
  129. {
  130. if (badge is not IBadgeView bv)
  131. return;
  132. AddBadge(bv);
  133. }
  134. public void AddBadge(IBadgeView badge)
  135. {
  136. var c = badge as Control;
  137. Int32 dist = c.Width + ChildWidthInterval;
  138. Int32 pos = ChildWidthInterval;
  139. PnlBadge.SuspendLayout();
  140. if (PnlBadge.Controls.Count > 0)
  141. {
  142. pos = PnlBadge.Controls.Cast<Control>().Max(b => b.Right) + ChildWidthInterval;
  143. if (badge.Presenter is not null)
  144. {
  145. var badges = PnlBadge.Controls.Cast<Control>().Where(b => (b as IBadgeView).Presenter.Id > badge.Presenter.Id);
  146. if (badges.Any())
  147. {
  148. pos = badges.Min(b => b.Left);
  149. foreach (var b in badges)
  150. b.Left += dist;
  151. }
  152. }
  153. }
  154. c.Left = pos;
  155. c.Top = 3;
  156. c.Height = PnlBadge.Height - 6;
  157. c.Anchor = AnchorStyles.Top | AnchorStyles.Left;
  158. PnlBadge.Controls.Add(c);
  159. dist = PnlBadge.Controls.Cast<Control>().Max(o => o.Right) - PnlBadge.Controls.Cast<Control>().Min(o => o.Left) + ChildWidthInterval;
  160. if (dist > Width)
  161. ShowArrowButton(true);
  162. PnlBadge.ResumeLayout();
  163. ChangeHScrollRange(dist);
  164. SetScrollArrowState(BtnRightArrow, false);
  165. }
  166. public void RemoveBadge(ChannelId id)
  167. {
  168. var c = GetBadge(id);
  169. if (c is not null)
  170. RemoveBadge(c);
  171. }
  172. public void RemoveBadge(Control badge)
  173. {
  174. if (badge is not IBadgeView bv)
  175. return;
  176. RemoveBadge(bv);
  177. }
  178. public void RemoveBadge(IBadgeView badge)
  179. {
  180. var c = badge as Control;
  181. Int32 dist = c.Width + ChildWidthInterval;
  182. PnlBadge.SuspendLayout();
  183. var badges = PnlBadge.Controls.Cast<Control>().Where(o => (o as IBadgeView).Presenter.Id > badge.Presenter.Id);
  184. foreach (var b in badges)
  185. b.Left -= dist;
  186. PnlBadge.Controls.Remove(c);
  187. dist = PnlBadge.Controls.Cast<Control>().Max(o => o.Right) - PnlBadge.Controls.Cast<Control>().Min(o => o.Left) + ChildWidthInterval;
  188. if (dist <= Width)
  189. ShowArrowButton(false);
  190. PnlBadge.ResumeLayout();
  191. ChangeHScrollRange(dist);
  192. c.Dispose();
  193. }
  194. private void ChangeHScrollRange(Int32 dist)
  195. {
  196. var width = dist - PnlBadge.Width;
  197. PnlBadge.HorizontalScroll.Maximum = width > 0 ? width : 100;
  198. PnlBadge.HorizontalScroll.Minimum = 0;
  199. if (_ScrollVal > PnlBadge.HorizontalScroll.Maximum)
  200. {
  201. _ScrollVal = PnlBadge.HorizontalScroll.Maximum;
  202. PnlBadge.HorizontalScroll.Value = _ScrollVal;
  203. }
  204. }
  205. private void ShowArrowButton(Boolean value)
  206. {
  207. TlpContainer.SuspendLayout();
  208. if (!value)
  209. {
  210. _ScrollVal = 1;
  211. PnlBadge.HorizontalScroll.Value = _ScrollVal;
  212. }
  213. BtnLeftArrow.Visible = value;
  214. BtnRightArrow.Visible = value;
  215. TlpContainer.ResumeLayout();
  216. }
  217. private void BtnLeftArrow_Click(object sender, EventArgs e)
  218. {
  219. if (_ScrollVal > 1)
  220. {
  221. var orderbadges = PnlBadge.Controls.Cast<IBadgeView>().OrderBy(b => b.Presenter.Id);
  222. var badge = orderbadges.LastOrDefault(b => (b as Control).Left < ChildWidthInterval - 5) as Control;
  223. if (badge is not null)
  224. {
  225. _ScrollVal += badge.Left - ChildWidthInterval;
  226. if (_ScrollVal < 1)
  227. _ScrollVal = 1;
  228. }
  229. else
  230. _ScrollVal = 1;
  231. PnlBadge.HorizontalScroll.Value = _ScrollVal;
  232. }
  233. SetScrollArrowState(BtnLeftArrow, _ScrollVal <= 1);
  234. SetScrollArrowState(BtnRightArrow, false);
  235. }
  236. private void BtnRightArrow_Click(object sender, EventArgs e)
  237. {
  238. if (_ScrollVal < PnlBadge.HorizontalScroll.Maximum)
  239. {
  240. var orderbadges = PnlBadge.Controls.Cast<IBadgeView>().OrderBy(b => b.Presenter.Id);
  241. var badge = orderbadges.First(b => (b as Control).Left + 1 >= ChildWidthInterval) as Control;
  242. _ScrollVal += badge.Width + ChildWidthInterval;
  243. if (_ScrollVal > PnlBadge.HorizontalScroll.Maximum)
  244. _ScrollVal = PnlBadge.HorizontalScroll.Maximum;
  245. PnlBadge.HorizontalScroll.Value = _ScrollVal;
  246. }
  247. SetScrollArrowState(BtnRightArrow, _ScrollVal >= PnlBadge.HorizontalScroll.Maximum);
  248. SetScrollArrowState(BtnLeftArrow, false);
  249. }
  250. private Rectangle _DragBox;
  251. private Int64 _DragDropStamp;
  252. private void PnlBadge_MouseDown(object sender, MouseEventArgs e)
  253. {
  254. if (e.Button != MouseButtons.Left /*|| !BtnLeftArrow.Visible*/)
  255. return;
  256. //!!!Change focus to DsoBtmStrip
  257. if (ActiveControl is null)
  258. {
  259. SelectNextControl(ActiveControl, true, true, true, true);
  260. }
  261. //_MousePreviousPoint = Cursor.Position;
  262. _MousePreviousPoint = e.Location;
  263. Size ds = SystemInformation.DragSize;
  264. _DragBox = new Rectangle(new Point(e.X - (ds.Width / 2), e.Y - (ds.Height / 2)), ds);
  265. _DragDropStamp = DateTime.Now.Ticks + 10_000_000;
  266. }
  267. private void PnlBadge_MouseMove(object sender, MouseEventArgs e)
  268. {
  269. if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
  270. {
  271. if (_DragDropStamp < DateTime.Now.Ticks && _DragBox.Contains(e.X, e.Y))
  272. {
  273. _DragBox = Rectangle.Empty;
  274. return;
  275. }
  276. //Int32 px = Cursor.Position.X - _MousePreviousPoint.X;
  277. Int32 px = e.X - _MousePreviousPoint.X;
  278. if (px == 0)
  279. return;
  280. _ScrollVal -= px;
  281. if (_ScrollVal < 1)
  282. _ScrollVal = 1;
  283. else if (_ScrollVal > PnlBadge.HorizontalScroll.Maximum)
  284. _ScrollVal = PnlBadge.HorizontalScroll.Maximum;
  285. PnlBadge.HorizontalScroll.Value = _ScrollVal;
  286. _MousePreviousPoint = e.Location;// Cursor.Position;
  287. SetScrollArrowState(BtnRightArrow, _ScrollVal >= PnlBadge.HorizontalScroll.Maximum);
  288. SetScrollArrowState(BtnLeftArrow, _ScrollVal <= 1);
  289. }
  290. }
  291. private void PnlBadge_MouseUp(object sender, MouseEventArgs e)
  292. {
  293. //if (IsDragging)
  294. // IsDragging = false;
  295. _DragBox = Rectangle.Empty;
  296. }
  297. private void PnlBadge_MouseWheel(object sender, MouseEventArgs e)
  298. {
  299. Int32 px = e.Delta / 3;
  300. if (px == 0)
  301. return;
  302. _ScrollVal -= px;
  303. if (_ScrollVal < 1)
  304. _ScrollVal = 1;
  305. else if (_ScrollVal > PnlBadge.HorizontalScroll.Maximum)
  306. _ScrollVal = PnlBadge.HorizontalScroll.Maximum;
  307. PnlBadge.HorizontalScroll.Value = _ScrollVal;
  308. _MousePreviousPoint = Cursor.Position;
  309. SetScrollArrowState(BtnRightArrow, _ScrollVal >= PnlBadge.HorizontalScroll.Maximum);
  310. SetScrollArrowState(BtnLeftArrow, _ScrollVal <= 1);
  311. }
  312. protected override void OnLoad(EventArgs e)
  313. {
  314. base.OnLoad(e);
  315. PnlBadge.HorizontalScroll.Maximum = PnlBadge.Width;
  316. }
  317. private static void SetScrollArrowSvg(UestcIconButton btn, Boolean isLeft)
  318. {
  319. Single widthscale = 0.6F;
  320. btn.IconSize = new Size((Int32)(btn.Width * widthscale), btn.Height);
  321. btn.IconOffset = (Int32)(btn.Width * (1 - widthscale) / 2);
  322. String svgstring;
  323. if (isLeft)
  324. svgstring = Properties.Resources.ChnlInfoPanel_leftArrowSvg;
  325. else
  326. svgstring = Properties.Resources.ChnlInfoPanel_rightArrowSvg;
  327. btn.SVGPath = svgstring;
  328. }
  329. private static void SetScrollArrowState(UestcIconButton arrow, Boolean isEnd)
  330. {
  331. var color = isEnd ? Color.Gray : Color.White;
  332. if (arrow.SVGForeColor != color)
  333. {
  334. arrow.SVGForeColor = color;
  335. arrow.MouseinSvgForeColor = color;
  336. arrow.PressedSvgForeColor = color;
  337. }
  338. }
  339. }
  340. }