AssayUserControl.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. namespace PaintDotNet.GeneralAnalysis.Countometer
  11. {
  12. public partial class AssayUserControl : UserControl
  13. {
  14. private Panel panel;
  15. private Brush ellipseBrush;
  16. private Brush bgBrush;
  17. private TextureBrush textureBrush;
  18. Bitmap bit;
  19. int x = 0;
  20. int y = 0;
  21. bool showRec = false;
  22. int width;
  23. int height;
  24. int rect_x1;
  25. int rect_y1;
  26. int rect_x2;
  27. int rect_y2;
  28. int rectWidth;
  29. int rectHeight;
  30. int item_w;
  31. int item_h;
  32. Rectangle rectangle;
  33. private Color m_bgColor = Color.Transparent;
  34. private Color m_ellipseColor = Color.Transparent;
  35. private Color m_lensColor = Color.Transparent;
  36. private Color m_gridsColor = Color.Black;
  37. private List<Dictionary<string, object>> m_rectList;
  38. public AssayUserControl(int MaxWidth, int MaxHeight, double pointX, double pointY, double rangeX, double rangeY, List<Dictionary<string, object>> rectList , int rangeRatio)
  39. {
  40. m_rectList = rectList;
  41. if(rangeRatio == 1)
  42. {
  43. if ((pointX * rangeX) < (pointY * rangeY))
  44. {
  45. height = MaxHeight;
  46. width = Convert.ToInt32((pointX * rangeX) * MaxHeight / (pointY * rangeY));
  47. }
  48. else
  49. {
  50. width = MaxWidth;
  51. height = Convert.ToInt32((pointY * rangeY) * MaxWidth / (pointX * rangeX));
  52. }
  53. }
  54. else
  55. {
  56. width = MaxWidth;
  57. height = MaxHeight;
  58. }
  59. rect_x1 = Convert.ToInt32((width - width / Math.Sqrt(2)) / 2);
  60. rect_y1 = Convert.ToInt32(((height - height / Math.Sqrt(2)) / 2));
  61. rect_x2 = Convert.ToInt32((width / Math.Sqrt(2)));
  62. rect_y2 = Convert.ToInt32((height / Math.Sqrt(2)));
  63. rectWidth = rect_x2;
  64. rectHeight = rect_y2;
  65. item_w = Convert.ToInt32(rectWidth / pointX);
  66. item_h = Convert.ToInt32(rectHeight / pointY);
  67. rect_x2 = Convert.ToInt32(item_w * pointX);
  68. rect_y2 = Convert.ToInt32(item_h * pointY);
  69. rect_x1 += (rectWidth - rect_x2) / 2;
  70. rect_y1 += (rectHeight - rect_y2) / 2;
  71. }
  72. public void initialize()
  73. {
  74. InitializeComponent();
  75. this.panel = new Panel();
  76. this.Size = new Size(width, height);
  77. ellipseBrush = new SolidBrush(m_ellipseColor);
  78. bgBrush = new SolidBrush(m_bgColor);
  79. this.panel.BackColor = m_lensColor;
  80. this.panel.Location = new Point(rect_x1, rect_y1);
  81. this.panel.Size = new Size(rect_x2 + 1, rect_y2 + 1);
  82. this.Controls.Add(this.panel);
  83. rectangle = new Rectangle(0, 0, rect_x2 + 1, rect_y2 + 1);
  84. bit = new Bitmap(item_w, item_h);
  85. Graphics g = Graphics.FromImage(bit);
  86. Pen gridsPen = new Pen(m_gridsColor); //网格颜色
  87. g.DrawRectangle(gridsPen, new Rectangle(0, 0, item_w, item_h));
  88. g.Dispose();
  89. textureBrush = new TextureBrush(bit);//使用TextureBrush可以有效bai减少窗体拉伸时的闪烁du
  90. this.Paint += new PaintEventHandler(this.Assay_Paint);
  91. this.panel.Paint += new PaintEventHandler(this.Rect_Paint);
  92. }
  93. public Color BgColor
  94. {
  95. set { this.m_bgColor = value; }
  96. }
  97. public Color LensBgColor
  98. {
  99. set { this.m_ellipseColor = value; }
  100. }
  101. public Color LensColor
  102. {
  103. set { this.m_lensColor = value; }
  104. }
  105. public Color GridsColor
  106. {
  107. set { this.m_gridsColor = value; }
  108. }
  109. private void Rect_Paint(object sender, PaintEventArgs e)
  110. {
  111. e.Graphics.FillRectangle(textureBrush, this.rectangle);
  112. for (int i = 0; i < m_rectList.Count; ++i)
  113. {
  114. DrawRectAssay(m_rectList[i]);
  115. }
  116. }
  117. public void DrawRectAssay(Dictionary<String, object> rect)
  118. {
  119. Graphics g = this.panel.CreateGraphics();
  120. Brush brush = new SolidBrush((Color)rect["color"]);
  121. g.FillRectangle(brush, new Rectangle((int)rect["x"] * item_w + 1, (int)rect["y"] * item_h + 1, item_w - 1, item_h - 1));
  122. ////// draw text
  123. //string text = rect["key"].ToString();
  124. //Font font = new Font("微软雅黑", 9, FontStyle.Bold); // 定义字体
  125. //SizeF sf = g.MeasureString(text, font);
  126. ////// 下面定义一个矩形区域
  127. //float rectWidth = item_w;
  128. //float rectHeight = item_h;
  129. //int x = (int)rect["x"] * item_w + 1 + Convert.ToInt32((item_w - sf.Width) / 2);
  130. //int y = (int)rect["y"] * item_h + 1 + Convert.ToInt32((item_h - sf.Height) / 2);
  131. ////// 声明矩形域
  132. /////
  133. //Brush whiteBrush = new SolidBrush(Color.White);
  134. //RectangleF textArea = new RectangleF(x, y, rectWidth, rectHeight);
  135. //g.DrawString(text, font, whiteBrush, textArea);
  136. }
  137. private void Assay_Paint(object sender, PaintEventArgs e)
  138. {
  139. e.Graphics.FillRectangle(bgBrush, new Rectangle(0, 0, width - 1, height - 1));
  140. e.Graphics.FillEllipse(ellipseBrush, 0, 0, width, height);
  141. e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(rect_x1, rect_y1, rect_x2, rect_y2));
  142. }
  143. }
  144. }