using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PaintDotNet.GeneralAnalysis.Countometer { public partial class AssayUserControl : UserControl { private Panel panel; private Brush ellipseBrush; private Brush bgBrush; private TextureBrush textureBrush; Bitmap bit; int x = 0; int y = 0; bool showRec = false; int width; int height; int rect_x1; int rect_y1; int rect_x2; int rect_y2; int rectWidth; int rectHeight; int item_w; int item_h; Rectangle rectangle; private Color m_bgColor = Color.Transparent; private Color m_ellipseColor = Color.Transparent; private Color m_lensColor = Color.Transparent; private Color m_gridsColor = Color.Black; private List> m_rectList; public AssayUserControl(int MaxWidth, int MaxHeight, double pointX, double pointY, double rangeX, double rangeY, List> rectList , int rangeRatio) { m_rectList = rectList; if(rangeRatio == 1) { if ((pointX * rangeX) < (pointY * rangeY)) { height = MaxHeight; width = Convert.ToInt32((pointX * rangeX) * MaxHeight / (pointY * rangeY)); } else { width = MaxWidth; height = Convert.ToInt32((pointY * rangeY) * MaxWidth / (pointX * rangeX)); } } else { width = MaxWidth; height = MaxHeight; } rect_x1 = Convert.ToInt32((width - width / Math.Sqrt(2)) / 2); rect_y1 = Convert.ToInt32(((height - height / Math.Sqrt(2)) / 2)); rect_x2 = Convert.ToInt32((width / Math.Sqrt(2))); rect_y2 = Convert.ToInt32((height / Math.Sqrt(2))); rectWidth = rect_x2; rectHeight = rect_y2; item_w = Convert.ToInt32(rectWidth / pointX); item_h = Convert.ToInt32(rectHeight / pointY); rect_x2 = Convert.ToInt32(item_w * pointX); rect_y2 = Convert.ToInt32(item_h * pointY); rect_x1 += (rectWidth - rect_x2) / 2; rect_y1 += (rectHeight - rect_y2) / 2; } public void initialize() { InitializeComponent(); this.panel = new Panel(); this.Size = new Size(width, height); ellipseBrush = new SolidBrush(m_ellipseColor); bgBrush = new SolidBrush(m_bgColor); this.panel.BackColor = m_lensColor; this.panel.Location = new Point(rect_x1, rect_y1); this.panel.Size = new Size(rect_x2 + 1, rect_y2 + 1); this.Controls.Add(this.panel); rectangle = new Rectangle(0, 0, rect_x2 + 1, rect_y2 + 1); bit = new Bitmap(item_w, item_h); Graphics g = Graphics.FromImage(bit); Pen gridsPen = new Pen(m_gridsColor); //网格颜色 g.DrawRectangle(gridsPen, new Rectangle(0, 0, item_w, item_h)); g.Dispose(); textureBrush = new TextureBrush(bit);//使用TextureBrush可以有效bai减少窗体拉伸时的闪烁du this.Paint += new PaintEventHandler(this.Assay_Paint); this.panel.Paint += new PaintEventHandler(this.Rect_Paint); } public Color BgColor { set { this.m_bgColor = value; } } public Color LensBgColor { set { this.m_ellipseColor = value; } } public Color LensColor { set { this.m_lensColor = value; } } public Color GridsColor { set { this.m_gridsColor = value; } } private void Rect_Paint(object sender, PaintEventArgs e) { e.Graphics.FillRectangle(textureBrush, this.rectangle); for (int i = 0; i < m_rectList.Count; ++i) { DrawRectAssay(m_rectList[i]); } } public void DrawRectAssay(Dictionary rect) { Graphics g = this.panel.CreateGraphics(); Brush brush = new SolidBrush((Color)rect["color"]); g.FillRectangle(brush, new Rectangle((int)rect["x"] * item_w + 1, (int)rect["y"] * item_h + 1, item_w - 1, item_h - 1)); ////// draw text //string text = rect["key"].ToString(); //Font font = new Font("微软雅黑", 9, FontStyle.Bold); // 定义字体 //SizeF sf = g.MeasureString(text, font); ////// 下面定义一个矩形区域 //float rectWidth = item_w; //float rectHeight = item_h; //int x = (int)rect["x"] * item_w + 1 + Convert.ToInt32((item_w - sf.Width) / 2); //int y = (int)rect["y"] * item_h + 1 + Convert.ToInt32((item_h - sf.Height) / 2); ////// 声明矩形域 ///// //Brush whiteBrush = new SolidBrush(Color.White); //RectangleF textArea = new RectangleF(x, y, rectWidth, rectHeight); //g.DrawString(text, font, whiteBrush, textArea); } private void Assay_Paint(object sender, PaintEventArgs e) { e.Graphics.FillRectangle(bgBrush, new Rectangle(0, 0, width - 1, height - 1)); e.Graphics.FillEllipse(ellipseBrush, 0, 0, width, height); e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(rect_x1, rect_y1, rect_x2, rect_y2)); } } }