CircleControl.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Drawing.Drawing2D;
  5. namespace SmartCoalApplication.Core.CustomControl
  6. {
  7. /// <summary>
  8. /// 位置导航的圆形控件
  9. /// </summary>
  10. public partial class CircleControl : UserControl
  11. {
  12. /// <summary>
  13. /// 半径
  14. /// </summary>
  15. private int radius;
  16. private int smallRadius;
  17. private int location;
  18. public int position = 0;
  19. private Pen internalpen1 = new Pen(Color.Pink);
  20. private Pen internalpen2 = new Pen(Color.Pink);
  21. private Pen internalpen3 = new Pen(Color.Pink);
  22. private Pen internalpen4 = new Pen(Color.Pink);
  23. private Pen internalpen5 = new Pen(Color.Pink);
  24. private Pen internalpen6 = new Pen(Color.Pink);
  25. private Pen internalpen7 = new Pen(Color.Pink);
  26. private Pen internalpen8 = new Pen(Color.Pink);
  27. private Pen outpen1 = new Pen(Color.Black);
  28. private Pen outpen2 = new Pen(Color.Black);
  29. private Pen outpen3 = new Pen(Color.Black);
  30. private Pen outpen4 = new Pen(Color.Black);
  31. private Pen outpen5 = new Pen(Color.Black);
  32. private Pen outpen6 = new Pen(Color.Black);
  33. private Pen outpen7 = new Pen(Color.Black);
  34. private Pen outpen8 = new Pen(Color.Black);
  35. private GraphicsPath graphicsPath = new GraphicsPath();
  36. GraphicsPath internalLeft = new GraphicsPath();
  37. GraphicsPath internalLeftTop = new GraphicsPath();
  38. GraphicsPath internalTop = new GraphicsPath();
  39. GraphicsPath internalRightTop = new GraphicsPath();
  40. GraphicsPath internalRight = new GraphicsPath();
  41. GraphicsPath internalRightBottom = new GraphicsPath();
  42. GraphicsPath internalBottom = new GraphicsPath();
  43. GraphicsPath internalLeftBottom = new GraphicsPath();
  44. GraphicsPath outLeft = new GraphicsPath();
  45. GraphicsPath outLeftTop = new GraphicsPath();
  46. GraphicsPath outTop = new GraphicsPath();
  47. GraphicsPath outRightTop = new GraphicsPath();
  48. GraphicsPath outRight = new GraphicsPath();
  49. GraphicsPath outRightBottom = new GraphicsPath();
  50. GraphicsPath outBottom = new GraphicsPath();
  51. GraphicsPath outLeftBottom = new GraphicsPath();
  52. public CircleControl(int radius)
  53. {
  54. SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
  55. InitializeComponent();
  56. this.Size = new Size(radius*2+2, radius*2+2);
  57. this.radius = radius;
  58. this.smallRadius = (radius - 10)/2;
  59. this.location = (radius * 2 - smallRadius * 2) / 2;
  60. this.Paint += new PaintEventHandler(this.Control_Paint);
  61. this.MouseMove += new MouseEventHandler(this.Control_Move);
  62. this.MouseLeave += new EventHandler(this.Control_Leave);
  63. this.MouseClick += new MouseEventHandler(this.Control_Click);
  64. this.MouseDown += new MouseEventHandler(this.Control_MouseDown);
  65. this.MouseUp += new MouseEventHandler(this.Control_MouseUp);
  66. //double d = smallRadius / Math.Tan(90.0-22.5);
  67. //double x = Math.Sqrt(Math.Pow(d,2) - Math.Pow(smallRadius, 2));
  68. //int x1 = (int)((smallRadius * 2 - x) / 2);
  69. double d = smallRadius * 2 * Math.Sin(22.5 * Math.PI / 180);
  70. double x1 = (smallRadius * 2 - d*2) / 2;
  71. float d1 = (float)d;
  72. float x2 = (float)x1;
  73. internalLeft.AddPie(location, location, smallRadius*2, smallRadius * 2, 157.5f, 45f);
  74. internalLeftTop.AddPie(location, location, smallRadius*2, smallRadius * 2, 202.5f, 45f);
  75. internalTop.AddPie(location, location, smallRadius * 2, smallRadius * 2, 247.5f, 45f);
  76. internalRightTop.AddPie(location, location, smallRadius * 2, smallRadius * 2, 292.5f, 45f);
  77. internalRight.AddPie(location, location, smallRadius * 2, smallRadius * 2, 337.5f, 45f);
  78. internalRightBottom.AddPie(location, location, smallRadius * 2, smallRadius * 2, 22.5f, 45f);
  79. internalBottom.AddPie(location, location, smallRadius * 2, smallRadius * 2, 67.5f, 45f);
  80. internalLeftBottom.AddPie(location, location, smallRadius * 2, smallRadius * 2, 112.5f, 45f);
  81. outLeft.AddPie(0, 0, radius*2, radius * 2, 157.5f, 45f);
  82. outLeftTop.AddPie(0, 0, radius * 2, radius * 2, 202.5f, 45f);
  83. outTop.AddPie(0, 0, radius * 2, radius * 2, 247.5f, 45f);
  84. outRightTop.AddPie(0, 0, radius * 2, radius * 2, 292.5f, 45f);
  85. outRight.AddPie(0, 0, radius * 2, radius * 2, 337.5f, 45f);
  86. outRightBottom.AddPie(0, 0, radius * 2, radius * 2, 22.5f, 45f);
  87. outBottom.AddPie(0, 0, radius * 2, radius * 2, 67.5f, 45f);
  88. outLeftBottom.AddPie(0, 0, radius * 2, radius * 2, 112.5f, 45f);
  89. graphicsPath.AddEllipse(0, 0, radius * 2, radius * 2);
  90. }
  91. public event EventHandler clicked;
  92. public event EventHandler mouseUp;
  93. public event EventHandler mouseDown;
  94. private void Control_MouseDown(object sender, MouseEventArgs e)
  95. {
  96. OutMouseUpOrDown(e);
  97. if (!graphicsPath.IsVisible(e.Location))
  98. {
  99. this.Refresh();
  100. this.position = 9;
  101. }
  102. else
  103. {
  104. if (mouseDown != null)
  105. {
  106. mouseDown(this, e);
  107. }
  108. Console.WriteLine(position.ToString());
  109. }
  110. }
  111. private void Control_MouseUp(object sender, MouseEventArgs e)
  112. {
  113. OutMouseUpOrDown(e);
  114. if (!graphicsPath.IsVisible(e.Location))
  115. {
  116. this.Refresh();
  117. this.position = 9;
  118. }
  119. else
  120. {
  121. if (mouseUp != null)
  122. {
  123. mouseUp(this, e);
  124. }
  125. Console.WriteLine(position.ToString());
  126. }
  127. }
  128. private void Control_Click(object sender, MouseEventArgs e)
  129. {
  130. InternalClickEvent(e);
  131. OutClickEvent(e);
  132. if (!graphicsPath.IsVisible(e.Location))
  133. {
  134. this.Refresh();
  135. this.position = 0;
  136. }
  137. else
  138. {
  139. if (clicked != null)
  140. {
  141. clicked(this, e);
  142. }
  143. Console.WriteLine(position.ToString());
  144. //MessageBox.Show("" + position);
  145. }
  146. }
  147. private void Control_Leave(object sender, EventArgs e)
  148. {
  149. InitPen();
  150. this.position = 0;
  151. }
  152. private void Control_Move(object sender, MouseEventArgs e)
  153. {
  154. InitPen();
  155. InternalMoveEvent(e);
  156. OutMoveEvent(e);
  157. if(!graphicsPath.IsVisible(e.Location))
  158. {
  159. InitPen();
  160. this.Refresh();
  161. }
  162. }
  163. private void Control_Paint(object sender, PaintEventArgs e)
  164. {
  165. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  166. e.Graphics.DrawPath(outpen1, outLeft);
  167. e.Graphics.DrawPath(outpen2, outLeftTop);
  168. e.Graphics.DrawPath(outpen3, outTop);
  169. e.Graphics.DrawPath(outpen4, outRightTop);
  170. e.Graphics.DrawPath(outpen5, outRight);
  171. e.Graphics.DrawPath(outpen6, outRightBottom);
  172. e.Graphics.DrawPath(outpen7, outBottom);
  173. e.Graphics.DrawPath(outpen8, outLeftBottom);
  174. e.Graphics.DrawPath(internalpen1, internalLeft);
  175. e.Graphics.DrawPath(internalpen2, internalLeftTop);
  176. e.Graphics.DrawPath(internalpen3, internalTop);
  177. e.Graphics.DrawPath(internalpen4, internalRightTop);
  178. e.Graphics.DrawPath(internalpen5, internalRight);
  179. e.Graphics.DrawPath(internalpen6, internalRightBottom);
  180. e.Graphics.DrawPath(internalpen7, internalBottom);
  181. e.Graphics.DrawPath(internalpen8, internalLeftBottom);
  182. //e.Graphics.DrawEllipse(internalpen, 0, 0, radius*2, radius*2);
  183. //e.Graphics.DrawEllipse(internalpen, location, location, smallRadius*2, smallRadius*2);
  184. }
  185. private void InitPen()
  186. {
  187. internalpen1 = new Pen(Color.Pink);
  188. internalpen2 = new Pen(Color.Pink);
  189. internalpen3 = new Pen(Color.Pink);
  190. internalpen4 = new Pen(Color.Pink);
  191. internalpen5 = new Pen(Color.Pink);
  192. internalpen6 = new Pen(Color.Pink);
  193. internalpen7 = new Pen(Color.Pink);
  194. internalpen8 = new Pen(Color.Pink);
  195. outpen1 = new Pen(Color.Black);
  196. outpen2 = new Pen(Color.Black);
  197. outpen3 = new Pen(Color.Black);
  198. outpen4 = new Pen(Color.Black);
  199. outpen5 = new Pen(Color.Black);
  200. outpen6 = new Pen(Color.Black);
  201. outpen7 = new Pen(Color.Black);
  202. outpen8 = new Pen(Color.Black);
  203. }
  204. private void InternalMoveEvent(MouseEventArgs e)
  205. {
  206. if (internalLeft.IsVisible(e.Location))
  207. {
  208. internalpen1 = new Pen(Color.Blue);
  209. this.Refresh();
  210. return;
  211. }
  212. if (internalLeftTop.IsVisible(e.Location))
  213. {
  214. internalpen2 = new Pen(Color.Blue);
  215. this.Refresh();
  216. return;
  217. }
  218. if (internalTop.IsVisible(e.Location))
  219. {
  220. internalpen3 = new Pen(Color.Blue);
  221. this.Refresh();
  222. return;
  223. }
  224. if (internalRightTop.IsVisible(e.Location))
  225. {
  226. internalpen4 = new Pen(Color.Blue);
  227. this.Refresh();
  228. return;
  229. }
  230. if (internalRight.IsVisible(e.Location))
  231. {
  232. internalpen5 = new Pen(Color.Blue);
  233. this.Refresh();
  234. return;
  235. }
  236. if (internalRightBottom.IsVisible(e.Location))
  237. {
  238. internalpen6 = new Pen(Color.Blue);
  239. this.Refresh();
  240. return;
  241. }
  242. if (internalBottom.IsVisible(e.Location))
  243. {
  244. internalpen7 = new Pen(Color.Blue);
  245. this.Refresh();
  246. return;
  247. }
  248. if (internalLeftBottom.IsVisible(e.Location))
  249. {
  250. internalpen8 = new Pen(Color.Blue);
  251. this.Refresh();
  252. return;
  253. }
  254. }
  255. private void OutMoveEvent(MouseEventArgs e)
  256. {
  257. if (outLeft.IsVisible(e.Location) && !internalLeft.IsVisible(e.Location))
  258. {
  259. outpen1 = new Pen(Color.Red);
  260. this.Refresh();
  261. return;
  262. }
  263. if (outLeftTop.IsVisible(e.Location) && !internalLeftTop.IsVisible(e.Location))
  264. {
  265. outpen2 = new Pen(Color.Red);
  266. this.Refresh();
  267. return;
  268. }
  269. if (outTop.IsVisible(e.Location) && !internalTop.IsVisible(e.Location))
  270. {
  271. outpen3 = new Pen(Color.Red);
  272. this.Refresh();
  273. return;
  274. }
  275. if (outRightTop.IsVisible(e.Location) && !internalRightTop.IsVisible(e.Location))
  276. {
  277. outpen4 = new Pen(Color.Red);
  278. this.Refresh();
  279. return;
  280. }
  281. if (outRight.IsVisible(e.Location) && !internalRight.IsVisible(e.Location))
  282. {
  283. outpen5 = new Pen(Color.Red);
  284. this.Refresh();
  285. return;
  286. }
  287. if (outRightBottom.IsVisible(e.Location) && !internalRightBottom.IsVisible(e.Location))
  288. {
  289. outpen6 = new Pen(Color.Red);
  290. this.Refresh();
  291. return;
  292. }
  293. if (outBottom.IsVisible(e.Location) && !internalBottom.IsVisible(e.Location))
  294. {
  295. outpen7 = new Pen(Color.Red);
  296. this.Refresh();
  297. return;
  298. }
  299. if (outLeftBottom.IsVisible(e.Location) && !internalLeftBottom.IsVisible(e.Location))
  300. {
  301. outpen8 = new Pen(Color.Red);
  302. this.Refresh();
  303. return;
  304. }
  305. }
  306. private void InternalClickEvent(MouseEventArgs e)
  307. {
  308. if (internalLeft.IsVisible(e.Location))
  309. {
  310. internalpen1 = new Pen(Color.Blue);
  311. this.Refresh();
  312. this.position = 1;
  313. return;
  314. }
  315. if (internalLeftTop.IsVisible(e.Location))
  316. {
  317. internalpen2 = new Pen(Color.Blue);
  318. this.Refresh();
  319. this.position = 2;
  320. return;
  321. }
  322. if (internalTop.IsVisible(e.Location))
  323. {
  324. internalpen3 = new Pen(Color.Blue);
  325. this.Refresh();
  326. this.position = 3;
  327. return;
  328. }
  329. if (internalRightTop.IsVisible(e.Location))
  330. {
  331. internalpen4 = new Pen(Color.Blue);
  332. this.Refresh();
  333. this.position = 4;
  334. return;
  335. }
  336. if (internalRight.IsVisible(e.Location))
  337. {
  338. internalpen5 = new Pen(Color.Blue);
  339. this.Refresh();
  340. this.position = 5;
  341. return;
  342. }
  343. if (internalRightBottom.IsVisible(e.Location))
  344. {
  345. internalpen6 = new Pen(Color.Blue);
  346. this.Refresh();
  347. this.position = 6;
  348. return;
  349. }
  350. if (internalBottom.IsVisible(e.Location))
  351. {
  352. internalpen7 = new Pen(Color.Blue);
  353. this.Refresh();
  354. this.position = 7;
  355. return;
  356. }
  357. if (internalLeftBottom.IsVisible(e.Location))
  358. {
  359. internalpen8 = new Pen(Color.Blue);
  360. this.Refresh();
  361. this.position = 8;
  362. return;
  363. }
  364. }
  365. private void OutClickEvent(MouseEventArgs e)
  366. {
  367. if (outLeft.IsVisible(e.Location) && !internalLeft.IsVisible(e.Location))
  368. {
  369. outpen1 = new Pen(Color.Red);
  370. this.Refresh();
  371. this.position = 9;
  372. return;
  373. }
  374. if (outLeftTop.IsVisible(e.Location) && !internalLeftTop.IsVisible(e.Location))
  375. {
  376. outpen2 = new Pen(Color.Red);
  377. this.Refresh();
  378. this.position = 10;
  379. return;
  380. }
  381. if (outTop.IsVisible(e.Location) && !internalTop.IsVisible(e.Location))
  382. {
  383. outpen3 = new Pen(Color.Red);
  384. this.Refresh();
  385. this.position = 11;
  386. return;
  387. }
  388. if (outRightTop.IsVisible(e.Location) && !internalRightTop.IsVisible(e.Location))
  389. {
  390. outpen4 = new Pen(Color.Red);
  391. this.Refresh();
  392. this.position = 12;
  393. return;
  394. }
  395. if (outRight.IsVisible(e.Location) && !internalRight.IsVisible(e.Location))
  396. {
  397. outpen5 = new Pen(Color.Red);
  398. this.Refresh();
  399. this.position = 13;
  400. return;
  401. }
  402. if (outRightBottom.IsVisible(e.Location) && !internalRightBottom.IsVisible(e.Location))
  403. {
  404. outpen6 = new Pen(Color.Red);
  405. this.Refresh();
  406. this.position = 14;
  407. return;
  408. }
  409. if (outBottom.IsVisible(e.Location) && !internalBottom.IsVisible(e.Location))
  410. {
  411. outpen7 = new Pen(Color.Red);
  412. this.Refresh();
  413. this.position = 15;
  414. return;
  415. }
  416. if (outLeftBottom.IsVisible(e.Location) && !internalLeftBottom.IsVisible(e.Location))
  417. {
  418. outpen8 = new Pen(Color.Red);
  419. this.Refresh();
  420. this.position = 16;
  421. return;
  422. }
  423. }
  424. private void OutMouseUpOrDown(MouseEventArgs e)
  425. {
  426. if (outLeft.IsVisible(e.Location) && !internalLeft.IsVisible(e.Location))
  427. {
  428. outpen1 = new Pen(Color.Red);
  429. this.Refresh();
  430. this.position = 9;
  431. return;
  432. }
  433. if (outLeftTop.IsVisible(e.Location) && !internalLeftTop.IsVisible(e.Location))
  434. {
  435. outpen2 = new Pen(Color.Red);
  436. this.Refresh();
  437. this.position = 10;
  438. return;
  439. }
  440. if (outTop.IsVisible(e.Location) && !internalTop.IsVisible(e.Location))
  441. {
  442. outpen3 = new Pen(Color.Red);
  443. this.Refresh();
  444. this.position = 11;
  445. return;
  446. }
  447. if (outRightTop.IsVisible(e.Location) && !internalRightTop.IsVisible(e.Location))
  448. {
  449. outpen4 = new Pen(Color.Red);
  450. this.Refresh();
  451. this.position = 12;
  452. return;
  453. }
  454. if (outRight.IsVisible(e.Location) && !internalRight.IsVisible(e.Location))
  455. {
  456. outpen5 = new Pen(Color.Red);
  457. this.Refresh();
  458. this.position = 13;
  459. return;
  460. }
  461. if (outRightBottom.IsVisible(e.Location) && !internalRightBottom.IsVisible(e.Location))
  462. {
  463. outpen6 = new Pen(Color.Red);
  464. this.Refresh();
  465. this.position = 14;
  466. return;
  467. }
  468. if (outBottom.IsVisible(e.Location) && !internalBottom.IsVisible(e.Location))
  469. {
  470. outpen7 = new Pen(Color.Red);
  471. this.Refresh();
  472. this.position = 15;
  473. return;
  474. }
  475. if (outLeftBottom.IsVisible(e.Location) && !internalLeftBottom.IsVisible(e.Location))
  476. {
  477. outpen8 = new Pen(Color.Red);
  478. this.Refresh();
  479. this.position = 16;
  480. return;
  481. }
  482. }
  483. }
  484. }