public void Draw(Graphics g) { if (ShowRulers) { GraphicsProperties gp = Program.SysConfig.GraphicsPropertiesManager.GetPropertiesByName("Ruler"); using (Pen pen = new Pen(Color.FromArgb(gp.Alpha, gp.Color), gp.PenWidth)) { //以下代码阅读可能带来不适 var baseMicron = BaseMajorValue * pictureBox.DigitalMagnification; micronPixels = pictureBox.MicronToPixel(baseMicron) * Program.SysConfig.Lense.FineAdjustment / 100f; PaintXAxis(g, pen, gp); PaintYAxis(g, pen, gp); } } }
/// <summary> /// Get GraphicsProperties by draw object name /// </summary> /// <param name="name"></param> /// <returns></returns> public GraphicsProperties GetPropertiesByName(string name) { GraphicsProperties propertie = null; switch (name) { case "Line": case "直线": propertie = properties[0]; break; case "Rectangle": case "矩形": propertie = properties[1]; break; case "Ellipse": case "椭圆": propertie = properties[2]; break; case "Polygon": propertie = properties[3]; break; case "Circle": propertie = properties[4]; break; case "Text": propertie = properties[5]; break; case "Ruler": propertie = properties[6]; break; default: propertie = properties[0]; break; } return(propertie); }
/// <summary> /// X轴绘制 /// </summary> /// <param name="g"></param> /// <param name="pen"></param> /// <param name="gp"></param> private void PaintXAxis(Graphics g, Pen pen, GraphicsProperties gp) { var startXPositive = pictureBox.Width / 2; var endXPositive = pictureBox.Width; var startY = pictureBox.Height / 2; var endY = pictureBox.Height / 2 - 10; double startXNegative = pictureBox.Width / 2; //micronPixels = CentimeterToPixel(1) * Program.SysConfig.Lense.FineAdjustment / 100f; int count = 0; for (double i = startXPositive; i < endXPositive + micronPixels; i += micronPixels) { var positiveNumber = count * baseMajorValue /* / (Program.SysConfig.Lense.Factor * pictureBox.Zoom)*/; //draw positive major ticks g.DrawLine(pen, (float)i, startY, (float)i, endY); Font font = new Font("Microsoft Sans Serif", 8.25f + gp.PenWidth / 10f); if (i != startXPositive) { PaintXDividePositiveAxis(g, pen, font, gp, count, i - micronPixels, i, startY, endY); } //draw positive minor ticks //g.DrawLine(pen, (float)(i + micronPixels / 2), startY, (float)(i + micronPixels / 2), startY - 5); string pns = positiveNumber < 10 ? positiveNumber.ToString("F1") : positiveNumber.ToString("F0"); var pSize = g.MeasureString(pns, font); if (positiveNumber > 0) { if (micronPixels < 80) { if (count % 2 == 0) { using (SolidBrush sb = new SolidBrush(Color.FromArgb(gp.Alpha, gp.Color))) g.DrawString(pns, font, sb, (float)(i - pSize.Width / 2), endY - pSize.Height); } } else { using (SolidBrush sb = new SolidBrush(Color.FromArgb(gp.Alpha, gp.Color))) g.DrawString(pns, font, sb, (float)(i - pSize.Width / 2), endY - pSize.Height); } } //draw negative major ticks g.DrawLine(pen, (float)startXNegative, startY, (float)startXNegative, endY); //draw negative minor ticks //g.DrawLine(pen, (float)(startXNegative - micronPixels / 2), startY, (float)(startXNegative - micronPixels / 2), startY - 5); var negativeNumber = /*-1 * */ positiveNumber; var nns = negativeNumber < 10 ? negativeNumber.ToString("F1") : negativeNumber.ToString("F0"); var nSize = g.MeasureString(nns, font); if (negativeNumber > 0) { if (micronPixels < 80) { if (count % 2 == 0) { using (SolidBrush sb = new SolidBrush(Color.FromArgb(gp.Alpha, gp.Color))) g.DrawString(nns, font, sb, (float)(startXNegative - nSize.Width / 2f), endY - nSize.Height); } } else { using (SolidBrush sb = new SolidBrush(Color.FromArgb(gp.Alpha, gp.Color))) g.DrawString(nns, font, sb, (float)(startXNegative - nSize.Width / 2f), endY - nSize.Height); } } //if (i != startXNegative) startXNegative -= micronPixels; PaintXDivideNegativeAxis(g, pen, font, gp, count, startXNegative, startXNegative + micronPixels, startY, endY); count++; font.Dispose(); } g.DrawLine(pen, 0, pictureBox.Height / 2, pictureBox.Width, pictureBox.Height / 2); }